| 1 | package Output::Rewrite; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | tie *STDOUT, "Output::Rewrite"; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | my %rewrite_rule; | |||||||||||||||||||
| 11 | 2 | my $modifiers = 'g'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=14.2
| |||||||||||||||||||||
| 15 | 1 | my $class = shift; | |||||||||||||||||||
| 16 | 1 | my %fields = @_; | |||||||||||||||||||
| 17 | 1 | ▶ | if(ref $fields{rewrite_rule} eq 'HASH'){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | 0 | my %new_rewrite_rule = %{$fields{rewrite_rule}}; | |||||||||||||||||||
| 19 | 0 | rewrite_rule(%new_rewrite_rule); | |||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | 1 | ▶ | if($fields{modifiers}){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 0 | modifiers($fields{modifiers}); | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 0 | ▶ | sub modifiers { | ||||||||||||||||||
|
Subroutine
modifiers: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 27 | 0 | ▶ | if(defined($_[0])){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 0 | $modifiers = $_[0]; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | else{ | ||||||||||||||||||||
| 31 | 0 | return $modifiers; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | sub rewrite_rule { | ||||||||||||||||||
|
Subroutine
rewrite_rule: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 36 | 0 | my %new_rewrite_rule; | |||||||||||||||||||
| 37 | 0 | ▶ | if(@_ == 1){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 0 | return $rewrite_rule{$_[0]}; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | else{ | ||||||||||||||||||||
| 41 | 0 | %new_rewrite_rule = @_; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | 0 | %rewrite_rule = (%rewrite_rule, %new_rewrite_rule); | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | sub _rewrite { | ||||||||||||||||||
|
Subroutine
_rewrite: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 49 | 0 | my $self = shift; | |||||||||||||||||||
| 50 | 0 | ▶ | my $string = shift || return; | ||||||||||||||||||
|
Condition0%0/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | while(my($from, $to) = each %rewrite_rule){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 0 | ▶ | if(ref $to eq 'CODE'){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 0 | my $new_modifiers = $modifiers; | |||||||||||||||||||
| 55 | 0 | ▶ | $new_modifiers .= 'e' if($new_modifiers !~ /e/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 0 | eval "\$string =~ s/$from/&\$to()/$new_modifiers;"; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | else{ | ||||||||||||||||||||
| 59 | #print STDERR "\$string =~ s/$from/$to/$modifiers;\n"; | ||||||||||||||||||||
| 60 | 0 | eval "\$string =~ s/$from/$to/$modifiers;"; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | 0 | ▶ | croak "Output::Rewrite Rewrite error:\n" . $@ if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | return $string; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 2 | ▶ | sub TIEHANDLE { | ||||||||||||||||||
|
Subroutine
TIEHANDLE: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 2 | my $class = shift; | |||||||||||||||||||
| 71 | 2 | my $form = shift; | |||||||||||||||||||
| 72 | 2 | my $self; | |||||||||||||||||||
| 73 | 2 | open($self, ">&STDOUT"); | |||||||||||||||||||
| 74 | #$$self->{hoge} = 'fuga'; | ||||||||||||||||||||
| 75 | 2 | bless $self, $class; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | sub PRINT { | ||||||||||||||||||
|
Subroutine
PRINT: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||
| 80 | 2 | ▶ | no warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 81 | 0 | my $string = join('', @_); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | print $self $self->_rewrite($string); | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | sub PRINTF { | ||||||||||||||||||
|
Subroutine
PRINTF: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 87 | 0 | my $self = shift; | |||||||||||||||||||
| 88 | 0 | my $format = shift; | |||||||||||||||||||
| 89 | 0 | $self->PRINT( $self->_rewrite( sprintf($format, @_) ) ); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | sub WRITE { | ||||||||||||||||||
|
Subroutine
WRITE: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 93 | 0 | my $self = shift; | |||||||||||||||||||
| 94 | 0 | my $string = shift; | |||||||||||||||||||
| 95 | 0 | ▶ | my $length = shift || length $string; | ||||||||||||||||||
|
Condition0%0/3
shift() || length $string
MC/DC0%0/2
shift() || length $string
shift()
length $string
Truth table0%0/3
A || B
A= shift()B= length $string
| |||||||||||||||||||||
| 96 | 0 | ▶ | my $offset = shift || 0; | ||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 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 | 2 | 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<< <mahito at cpan.org> >>, L<http://www.ornithopter.jp/> | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =head1 BUGS | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | Please report any bugs or feature requests to | ||||||||||||||||||||
| 228 | C<bug-output-rewrite at rt.cpan.org>, or through the web interface at | ||||||||||||||||||||
| 229 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Output-Rewrite>. | ||||||||||||||||||||
| 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<http://annocpan.org/dist/Output-Rewrite> | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | =item * CPAN Ratings | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | L<http://cpanratings.perl.org/d/Output-Rewrite> | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | =item * RT: CPAN's request tracker | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Output-Rewrite> | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | =item * Search CPAN | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | L<http://search.cpan.org/dist/Output-Rewrite> | ||||||||||||||||||||
| 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 | 2 | 1; # End of Output::Rewrite | |||||||||||||||||||