Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Bug in script, regex help req extreme urgent

by sid.verycool (Novice)
on Mar 10, 2013 at 12:40 UTC ( #1022660=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Bug in script, regex help req extreme urgent
in thread Bug in script, regex help req extreme urgent

So now i have 2 scripts, one which is doing all work correctly but does NOT USE LINE BY LINE match the code is as follows

#!/usr/bin/perl -w BEGIN {undef $/;} # I TRIED 1ST my $match = "^module.*?$ARGV[2].*?([\\(;])"; 2ND my $mat +ch = "\^module.*?$ARGV[2].*?([\\(;])"; 3RD my $match = "\\^module.*?$ +ARGV[2].*?([\\(;])" #"(?<=module )$ARGV[2]" my $match = "(?<=module )$ARGV[2].*?([\\(;])"; #print "$match"; my $filename = $ARGV[0]; open (INFILE, "<", $filename) or die "Failed to read file $filename +: $! \n"; $string = <INFILE>; close INFILE; #I TRIED "$string =~ s/^$match/module $ARGV[1]$1/sg;"; $string =~ s/$match/$ARGV[1]$1/sg; #print "$1"; open OUTFILE, ">$ARGV[0]" || die "Failed to create $ARGV[0]\n"; print OUTFILE ($string); close OUTFILE;
this will do well until in the comment// some1 writes our pattern. Now to come up with a new script which GOES LINE BY LINE courtesy @CountZero i have
use Modern::Perl; my $filename = $ARGV[0]; die "Usage: modulenamechanger.pl filename newmodulename oldmodulename\ +n" unless @ARGV == 3; open my $INFILE, "<", $filename or die "Failed to read file $filename +: $!"; say "Now parsing $filename"; my @file = <$INFILE>; close $INFILE; open my $OUTFILE, '>', $filename or die "Failed to create $filename"; my $match = qr "module\s*($ARGV[2])"; say "Matching: $match"; for my $line (@file) { if ( $line =~ m(^//) ) { print $OUTFILE $line; next; } $line =~ s/$match/module $ARGV[1]/; print $OUTFILE $line; }
But my problems with this are as follows: 1. NO modern perl 2.This code didnt work for me 3.I dont want to change the pattern regex my $match = "(?<=module )$ARGV[2].*?([\\(;])";
(it may cause some other effect because im not 100% sure about its intent and why it was written in that way, so lets keep it that way only). 4. I didnt find any else portion in your code @zerocount 5.Also there is some problem in parsing, i have modified your code but still not getting desired o/p. Please see the below code, i cant find out whats wrong ,i tried various print statements as well to see but in vain
#!/usr/bin/perl -w BEGIN {undef $/;} #use Modern::Perl; my $filename = $ARGV[0]; die "Usage: modulenamechanger.pl filename newmodulename oldmodulename\ +n" unless @ARGV == 3; open my $INFILE, "<", $filename or die "Failed to read file $filename +: $!"; print "Now parsing $filename\n"; my @file = <$INFILE>; close $INFILE; open my $OUTFILE, '>', $filename or die "Failed to create $filename"; # WITHOUT ABANSAL CONCEPT my $match = qr "module\s*($ARGV[2])"; my $match = "(?<=module )$ARGV[2].*?([\\(;])"; print "Matching: $match\n"; for my $line (@file) { print "READING LINE $line \n"; if ( $line =~ m(^//) ) { print "INSIDE IF\n"; print $OUTFILE $line; next; } else { print "INSIDE ELSE \n BEFORE change:\t $line\n"; $line =~ s/$match/moduleXXX $ARGV[1]/; print "AFTER :\t $line\n"; print $OUTFILE $line; next; } }
The output is
my.pl f7 NEW OLD Now parsing f7 Matching: (?<=module )OLD.*?([\(;]) READING LINE //Verilog HDL for "tt", "hh" "functional" // if i write the word module OLD(Y, A, B ); here the script goofs up `timescale 1ps/10fs module OLD(Y, A, B ); output Y; input A; input B; endmodule INSIDE IF


Comment on Re^3: Bug in script, regex help req extreme urgent
Select or Download Code
Re^4: Bug in script, regex help req extreme urgent
by Lotus1 (Chaplain) on Mar 10, 2013 at 14:03 UTC
    BEGIN {undef $/;} ... print "Now parsing $filename\n"; my @file = <$INFILE>;

    If you are reading line by line or into an array like this then you need to get rid of the undef $/ since that will make it read the whole file into the first element of the array.

Re^4: Bug in script, regex help req extreme urgent
by CountZero (Bishop) on Mar 10, 2013 at 14:06 UTC
    OK, let's address this item by item.
    1. Modern::Perl: you can replace this by use strict; use warnings; and replace say by print + "\n".
    2. What version of Perl are you using?
    3. You can use any regex that works (but see point 5).
    4. My code does not need an else part.
    5. Of course it does not work, the regex you use is wrong. Use the following regex: my $match = qr "module\s*($ARGV[2])\b";. That one works, trust me. It is similar to my previous once, but will reject modulenames that are not exactly the same, but only start with the string you want to replace. It will accept "OLD" but will reject strings like "OLDER", "OLDENBERG", "OLDUVAI",...

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-09-16 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (157 votes), past polls