CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [poly4life] Can I do an if-then-else regex mixing match and substitution?: Edit Log



FishMonger
Veteran / Moderator

Mar 25, 2011, 2:53 PM


Views: 5776
Re: [poly4life] Can I do an if-then-else regex mixing match and substitution?

Is this what you're after?


Code
use warnings; 
use strict;
use File::Slurp;

my $data = read_file('test.txt');

if ( $data =~ /Foo/ ) {
$data =~ s/Hello World/Hello Earth/;
print $data;
}
else {
print "Foo not found\n";
}


You can also do the substitution in the conditional test.

Code
if ( $data =~ /Foo/ and $data =~ s/Hello World/Hello Earth/ ) { 
print $data;
}



(This post was edited by FishMonger on Mar 25, 2011, 2:57 PM)


Edit Log:
Post edited by FishMonger (Veteran) on Mar 25, 2011, 2:57 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives