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: Intermediate: Re: [sid.verycool] match at begin ^ not working for me: Edit Log



Kenosis
User

Mar 9, 2013, 9:25 AM


Views: 912
Re: [sid.verycool] match at begin ^ not working for me

Consider processing your file a line at a time, instead of slurping all of it into a variable for processing:

Code
use strict; 
use warnings;

my $old = pop;
my $new = pop;

while (<>) {
s/^(module\s+)\Q$old\E/$1$new/;
print;
}

Usage: perl script.pl inFile 'new' 'old' [>outFile]

The "new" and "old" arguments passed to the script are popped off @ARGV for use later in the substitution regex. Since the word "module" starts a line that you want to update, the s/^ ... notation forces a match at the beginning of the line. quotemeta (\Q ... \E) is used on $old in case it contains any regex characters. The substitution is implicitly done on the default scalar $_ and that scalar is also implicitly printed.

The optional last parameter allows you to direct output to a file.

Hope this helps!

Edit: Updated script for "new" "old" parameters.


(This post was edited by Kenosis on Mar 9, 2013, 10:34 AM)


Edit Log:
Post edited by Kenosis (User) on Mar 9, 2013, 9:29 AM
Post edited by Kenosis (User) on Mar 9, 2013, 9:31 AM
Post edited by Kenosis (User) on Mar 9, 2013, 9:31 AM
Post edited by Kenosis (User) on Mar 9, 2013, 9:32 AM
Post edited by Kenosis (User) on Mar 9, 2013, 9:32 AM
Post edited by Kenosis (User) on Mar 9, 2013, 10:30 AM
Post edited by Kenosis (User) on Mar 9, 2013, 10:31 AM
Post edited by Kenosis (User) on Mar 9, 2013, 10:33 AM
Post edited by Kenosis (User) on Mar 9, 2013, 10:34 AM
Post edited by Kenosis (User) on Mar 9, 2013, 10:34 AM


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

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