perl: changing between line by line and as a whole -
i'm starting work perl , need edit text. need perl read input line line , need perl read input whole. know can set like:
$/ = undef; or like:
{ local $/; $myfile= <$myfile>; } but im not sure how have if, in same script, want change reading "whole" "line line" or viceversa. is, imagine script starts as:
use warnings; use strict; $filename = shift; open f, $filename or die "usa: $0 filename\n"; while(<f>) { } and make replaces (s///;). , need go on edition reading whole. write:
{ local $/; $filename = <f>; } but need go on reading line line....
somebody can explain me logic behind in order learn how 'change' 1 mode another, keeping last edited version of input? thanks
ok, sorry. try focus on y instead of x. instance, need edit text , make replacement on portion of text delimited 2 words. imagine want replace forms of "dog" "cat", on "dogs" bewtween word "hello". input:
hello dog dog dog hello dog dog my output:
hello cat cat cat hello dog my script:
use warnings; use strict; $file = shift; open $file, $file or die "usa: $0 filename\n"; { local $/; $file = <$file>; {$file =~ s{dog}{cat}g} until ($file =~ m/hello/); } print $file; but replaced "dogs"
i tried other stratagey:
use warnings; use strict; $file = shift; open $file, $file or die "usa: $0 filename\n"; { local $/; $file = <$file>; while ($file =~ m{hello(.*?)hello}sg) { $text = $1; $text =~ s{dog}{cat}g; } } print $file; but in case no replacement...
default, line line, mode again outside code block:
{ local $/; $filename = <f>; } $/ global variable dynamically scoped in example.
Comments
Post a Comment