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

In reply to Re^3: Bug in script, regex help req extreme urgent by sid.verycool
in thread Bug in script, regex help req extreme urgent by sid.verycool

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.