Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Bug in script, regex help req extreme urgent

by jaredor (Deacon)
on Mar 09, 2013 at 16:22 UTC ( #1022589=note: print w/ replies, xml ) Need Help??


in reply to Bug in script, regex help req extreme urgent

I like the test harness of 2teez, it is easily adapted for a perl -p command line. So I'll reuse it for an old sed-der comment/observation:

For mass transformations of text, trade off flexible regex for static strings when possible.

while (<DATA>) { s/\b module \s+ OLD \b/module NEW/xms; print $_; }
__DATA__ //Verilog HDL for "tt", "hh" "functional" // if i write the word module here the script goofs up `timescale 1ps/10fs module OLD(Y, A, B ); // This is a great module! output Y; input A; input B; endmodule
//Verilog HDL for "tt", "hh" "functional" // if i write the word module here the script goofs up `timescale 1ps/10fs module NEW(Y, A, B ); // This is a great module! output Y; input A; input B; endmodule

Of course, this means that whatever whitespace separation you used to have between the keyword "module" and the module name is gone, but whitespace niceties usually aren't a big deal with mechanically gunkulated code. On the other hand, you don't have to ignore any lines that have commenting on them.

This is a line oriented solution, so slurping in a file and transforming that long string will require using the "g" flag and listening to sages such as choroba. For line-oriented text, I believe line oriented processing gives the fewest surprises (e.g., a surprise such as having a comment change affect code manipulation). However if the two adjacent words you are keying off of can be separated by a newline, then you have to use something like the fancier regex solutions suggested.

One thing left to worry about is whether "module OLD" in a comment should be transformed to "module NEW". But if you keep in mind the old maxim, "All comments lie," then you shouldn't worry too much ;-)


Comment on Re: Bug in script, regex help req extreme urgent
Select or Download Code
Re^2: Bug in script, regex help req extreme urgent
by sid.verycool (Novice) on Mar 09, 2013 at 18:37 UTC
    1 confession, this code was written my 1 of my colleagues who left and i have to fix it. I'm trying to understand what this script exactly wants (specially the regex in my $string), so that any of my changes dont break original intent of script. Plz help me in understanding its objective and how can i transform it to read thru line by line, which is i think the easier way (atleast for a newbie like me) and yes if in the comment we change OLD to new it wud b gr8 because that is also wrong (although comments lie ;) shell#> script.pl file NEW OLD
    #!/usr/bin/perl -w BEGIN {undef $/;} my $match = "module.*?$ARGV[2].*?([\\(;])"; my $filename = $ARGV[0]; open (INFILE, "<", $filename) or die "Failed to read file $filename +: $! \n"; $string = <INFILE>; close INFILE; $string =~ s/$match/module $ARGV[1]$1/sg; open OUTFILE, ">$ARGV[0]" || die "Failed to create $ARGV[0]\n"; print OUTFILE ($string); close OUTFILE;
      we change OLD to new it wud b gr8 because that is also wrong

      Can not understand this language, sorry. Maybe you need to provide a better example?.

      If you want a likely story to explain things, then here's mine: Your colleague wanted to change an entire module definition file "in place" but didn't want to use a temporary file.

      On a Unix-type command line, you can do the same thing with

      perl -pie 's/\b module \s+ OLD \b/module NEW/xms;' file_name

      But back to your script. Your colleague didn't have to slurp the file into a single variable, he or she could have read the file into an array. The amount of memory taken up would have roughly been the same and the text processing could then have been done line-by-line.

      I don't know what relevant CPAN modules are out there to make in-place text file processing easier, but I'd bet there are some. For your emergency need right now the other excellent answers in this thread should give you enough to deliver something.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022589]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-08-01 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls