| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Output::Rewrite; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 21036 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 5 | 1 |  |  | 1 |  | 5 | use Carp; | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 429 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | tie *STDOUT, "Output::Rewrite"; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | my %rewrite_rule; | 
| 11 |  |  |  |  |  |  | my $modifiers = 'g'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub import { | 
| 15 | 1 |  |  | 1 |  | 7 | my $class = shift; | 
| 16 | 1 |  |  |  |  | 2 | my %fields = @_; | 
| 17 | 1 | 50 |  |  |  | 8 | if(ref $fields{rewrite_rule} eq 'HASH'){ | 
| 18 | 0 |  |  |  |  | 0 | my %new_rewrite_rule = %{$fields{rewrite_rule}}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 19 | 0 |  |  |  |  | 0 | rewrite_rule(%new_rewrite_rule); | 
| 20 |  |  |  |  |  |  | } | 
| 21 | 1 | 50 |  |  |  | 11 | if($fields{modifiers}){ | 
| 22 | 0 |  |  |  |  | 0 | modifiers($fields{modifiers}); | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub modifiers { | 
| 27 | 0 | 0 |  | 0 | 1 | 0 | if(defined($_[0])){ | 
| 28 | 0 |  |  |  |  | 0 | $modifiers = $_[0]; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | else{ | 
| 31 | 0 |  |  |  |  | 0 | return $modifiers; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub rewrite_rule { | 
| 36 | 0 |  |  | 0 | 1 | 0 | my %new_rewrite_rule; | 
| 37 | 0 | 0 |  |  |  | 0 | if(@_ == 1){ | 
| 38 | 0 |  |  |  |  | 0 | return $rewrite_rule{$_[0]}; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | else{ | 
| 41 | 0 |  |  |  |  | 0 | %new_rewrite_rule = @_; | 
| 42 |  |  |  |  |  |  | } | 
| 43 | 0 |  |  |  |  | 0 | %rewrite_rule = (%rewrite_rule, %new_rewrite_rule); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub _rewrite { | 
| 49 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 50 | 0 |  | 0 |  |  | 0 | my $string = shift || return; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 |  |  |  |  | 0 | while(my($from, $to) = each %rewrite_rule){ | 
| 53 | 0 | 0 |  |  |  | 0 | if(ref $to eq 'CODE'){ | 
| 54 | 0 |  |  |  |  | 0 | my $new_modifiers = $modifiers; | 
| 55 | 0 | 0 |  |  |  | 0 | $new_modifiers .= 'e' if($new_modifiers !~ /e/); | 
| 56 | 0 |  |  |  |  | 0 | eval "\$string =~ s/$from/&\$to()/$new_modifiers;"; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | else{ | 
| 59 |  |  |  |  |  |  | #print STDERR "\$string =~ s/$from/$to/$modifiers;\n"; | 
| 60 | 0 |  |  |  |  | 0 | eval "\$string =~ s/$from/$to/$modifiers;"; | 
| 61 |  |  |  |  |  |  | } | 
| 62 | 0 | 0 |  |  |  | 0 | croak "Output::Rewrite Rewrite error:\n" . $@ if $@; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 0 |  |  |  |  | 0 | return $string; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub TIEHANDLE { | 
| 70 | 1 |  |  | 1 |  | 2 | my $class = shift; | 
| 71 | 1 |  |  |  |  | 2 | my $form = shift; | 
| 72 | 1 |  |  |  |  | 2 | my $self; | 
| 73 | 1 |  |  |  |  | 19 | open($self, ">&STDOUT"); | 
| 74 |  |  |  |  |  |  | #$$self->{hoge} = 'fuga'; | 
| 75 | 1 |  |  |  |  | 6 | bless $self, $class; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub PRINT { | 
| 79 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 80 | 1 |  |  | 1 |  | 5 | no warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 211 |  | 
| 81 | 0 |  |  |  |  |  | my $string = join('', @_); | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 |  |  |  |  |  | print $self $self->_rewrite($string); | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub PRINTF { | 
| 87 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 88 | 0 |  |  |  |  |  | my $format = shift; | 
| 89 | 0 |  |  |  |  |  | $self->PRINT( $self->_rewrite( sprintf($format, @_) ) ); | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | sub WRITE { | 
| 93 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 94 | 0 |  |  |  |  |  | my $string = shift; | 
| 95 | 0 |  | 0 |  |  |  | my $length = shift || length $string; | 
| 96 | 0 |  | 0 |  |  |  | my $offset = shift || 0; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 |  |  |  |  |  | syswrite($self, $self->_rewrite($string), $length, $offset); | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =head1 NAME | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | Output::Rewrite - Rewrite your script output. | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =head1 VERSION | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | Version 0.03 | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =cut | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | use Output::Rewrite ( | 
| 117 |  |  |  |  |  |  | rewrite_rule => { | 
| 118 |  |  |  |  |  |  | hoge => "fuga", | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  | ); | 
| 121 |  |  |  |  |  |  | print "hoge hogehoge\n"; | 
| 122 |  |  |  |  |  |  | # fuga fugafuga | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | use Output::Rewrite ( | 
| 127 |  |  |  |  |  |  | rewrite_rule => { | 
| 128 |  |  |  |  |  |  | '(?<=\b)hoge(?=\b)' => "fuga", | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | ); | 
| 131 |  |  |  |  |  |  | print "hoge hogehoge\n"; | 
| 132 |  |  |  |  |  |  | # fuga hogehoge | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | use Output::Rewrite ( | 
| 137 |  |  |  |  |  |  | rewrite_rule => { | 
| 138 |  |  |  |  |  |  | '(\d)' => '$1!', | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  | ); | 
| 141 |  |  |  |  |  |  | print "1234 I love Marine Corps!\n"; | 
| 142 |  |  |  |  |  |  | # 1!2!3!4! I love Marine Corps! | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | use Output::Rewrite( | 
| 146 |  |  |  |  |  |  | modifiers => q/msgi/, | 
| 147 |  |  |  |  |  |  | rewrite_rule => { | 
| 148 |  |  |  |  |  |  | '(?-i)Sensitive' => 'SENSITIVE', | 
| 149 |  |  |  |  |  |  | 'NoN sEnsItivE' => 'NON SENSITIVE', | 
| 150 |  |  |  |  |  |  | }, | 
| 151 |  |  |  |  |  |  | ); | 
| 152 |  |  |  |  |  |  | #or | 
| 153 |  |  |  |  |  |  | use Output::Rewrite; | 
| 154 |  |  |  |  |  |  | Output::Rewrite::rewrite_rule( | 
| 155 |  |  |  |  |  |  | '(?-i)Sensitive' => 'SENSITIVE', | 
| 156 |  |  |  |  |  |  | 'NoN sEnsItivE' => 'NON SENSITIVE', | 
| 157 |  |  |  |  |  |  | ); | 
| 158 |  |  |  |  |  |  | Output::Rewrite::modifiers('msgi'); | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Output::Rewrite helps you to rewrite your script output. | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | When you print(or write, syswrite, printf) to STDOUT, Output::Rewrite hooks output and rewrite this. | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | Set rewrite rule(regex) and regex modifiers(i,g,m,s,x) when you load this module, | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | use Output::Rewrite ( | 
| 172 |  |  |  |  |  |  | modifiers => 'ig', | 
| 173 |  |  |  |  |  |  | rewrite_rule => { | 
| 174 |  |  |  |  |  |  | 'from' => 'to', | 
| 175 |  |  |  |  |  |  | } | 
| 176 |  |  |  |  |  |  | ); | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | or with Output::Rewrite::rewrite_rule() and Output::Rewrite::modifiers(). | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | use Output::Rewrite; | 
| 181 |  |  |  |  |  |  | Output::Rewrite::modifiers('ig'); | 
| 182 |  |  |  |  |  |  | Output::Rewrite::rewrite_rule( | 
| 183 |  |  |  |  |  |  | 'from' => 'to', | 
| 184 |  |  |  |  |  |  | ); | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | This module ties STDOUT so you must use carefully. | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =head2 rewrite_rule | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | Accessor for rewrite rule. | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | Output::Rewrite::rewrite_rule( | 
| 196 |  |  |  |  |  |  | 'from' => 'to', | 
| 197 |  |  |  |  |  |  | 'from' => 'to', | 
| 198 |  |  |  |  |  |  | ); | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =head2 modifiers | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | Accessor for substitution modifiers.(i,g,m,s,x) | 
| 204 |  |  |  |  |  |  | Default is 'g'. | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | Output::Rewrite::modifiers('msgi'); | 
| 207 |  |  |  |  |  |  | my $modifiers = Output::Rewrite::modifiers; | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | If you want to apply modifiers only one time, you can use (?imsx-imsx) instead of this. | 
| 210 |  |  |  |  |  |  | For example: | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | use Output::Rewrite( | 
| 213 |  |  |  |  |  |  | modifiers => q/msgi/, | 
| 214 |  |  |  |  |  |  | rewrite_rule => { | 
| 215 |  |  |  |  |  |  | '(?-i)Sensitive' => 'SENSITIVE', | 
| 216 |  |  |  |  |  |  | 'NoN sEnsItivE' => 'NON SENSITIVE', | 
| 217 |  |  |  |  |  |  | }, | 
| 218 |  |  |  |  |  |  | ); | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | =head1 AUTHOR | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | Hogeist, C<<  >>, L | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | =head1 BUGS | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | Please report any bugs or feature requests to | 
| 228 |  |  |  |  |  |  | C, or through the web interface at | 
| 229 |  |  |  |  |  |  | L. | 
| 230 |  |  |  |  |  |  | I will be notified, and then you'll automatically be notified of progress on | 
| 231 |  |  |  |  |  |  | your bug as I make changes. | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | =head1 SUPPORT | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | perldoc Output::Rewrite | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | You can also look for information at: | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | =over 4 | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | L | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | L | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | =item * RT: CPAN's request tracker | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | L | 
| 254 |  |  |  |  |  |  |  | 
| 255 |  |  |  |  |  |  | =item * Search CPAN | 
| 256 |  |  |  |  |  |  |  | 
| 257 |  |  |  |  |  |  | L | 
| 258 |  |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  | =back | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 264 |  |  |  |  |  |  |  | 
| 265 |  |  |  |  |  |  | Copyright 2006 Hogeist, all rights reserved. | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 268 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 269 |  |  |  |  |  |  |  | 
| 270 |  |  |  |  |  |  | =cut | 
| 271 |  |  |  |  |  |  |  | 
| 272 |  |  |  |  |  |  | 1; # End of Output::Rewrite |