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:
match at begin ^ not working for me

 



sid.verycool
New User

Mar 9, 2013, 2:19 AM

Post #1 of 5 (928 views)
match at begin ^ not working for me Can't Post

Hi all, i need to debug this script and make it work according to my requirement. This is an urgent requirement, i need to fix this asap.</p> The concept of the script was to replace "oldname" written after the word "module" with "newname" This worked well until the word module came in the comment.

CONTENT OF SCRIPT <code> #!/usr/bin/perl -w

BEGIN {undef $/;}


# I TRIED 1ST my $match = "^module.*?$ARGV[2].*?([\\(;])"; 2ND my $match = "\^module.*?$ARGV[2].*?([\\(;])"; 3RD my $match = "\\^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 ALSO TRIED "$string =~ s/^$match/module $ARGV[1]$1/sg;";

$string =~ s/$match/module $ARGV[1]$1/sg;


open OUTFILE, ">$ARGV[0]" || die "Failed to create $ARGV[0]\n";
print OUTFILE ($string);
close OUTFILE;<\code>

what this script does to input file is : CONTENT OF BEFORE SCIPT IS RUN ON FILE <code>//Verilog HDL for "tt", "hh" "functional"
// if i write the word here the script goofs up
`timescale 1ps/10fs


module OLD(Y, A, B );
output Y;
input A;
input B;

endmodule
</code> NOW I RUN <code>script.pl FILE NEW OLD</code> now the CONTENT OF FILE BECOMES <code>//Verilog HDL for "tt", "hh" "functional"
// if i write the word here the script goofs up
`timescale 1ps/10fs


module NEW(Y, A, B );
output Y;
input A;
input B;

endmodule
</code> which is good but if i write the word "module" in the comment line i.e. CONTENT OF BEFORE SCrIPT IS RUN ON FILE which goofs up
<code>
//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 );
output Y;
input A;
input B;

endmodule </code>

Now the contents become <code>//Verilog HDL for "tt", "hh" "functional"
// if i write the word module NEW(Y, A, B );
output Y;
input A;
input B;

endmodule
</code> which is unacceptable to me. plz help guys,


BillKSmith
Veteran

Mar 9, 2013, 9:15 AM

Post #2 of 5 (913 views)
Re: [sid.verycool] match at begin ^ not working for me [In reply to] Can't Post

I have copied the code portion of your post and replaced
the angle bracket code-tags with square bracket tags to
make it legible. I plan to delete it after you edit yours.

I am working your problem and will get back to you later.


CONTENT OF SCRIPT

Code
 #!/usr/bin/perl -w  

BEGIN {undef $/;}


# I TRIED 1ST my $match = "^module.*?$ARGV[2].*?
([\\(;])"; 2ND my $match = "\^module.*?$ARGV[2].*?
([\\(;])"; 3RD my $match = "\\^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 ALSO TRIED "$string =~ s/^$match/module $ARGV[1]$1/sg;";

$string =~ s/$match/module $ARGV[1]$1/sg;


open OUTFILE, ">$ARGV[0]" || die "Failed to create $ARGV[0]\n";
print OUTFILE ($string);
close OUTFILE;


what this script does to input file is : CONTENT OF BEFORE SCIPT IS RUN ON FILE

Code
//Verilog HDL for "tt", "hh" "functional"  
// if i write the word here the script goofs up
`timescale 1ps/10fs


module OLD(Y, A, B );
output Y;
input A;
input B;

endmodule

NOW I RUN

Code
script.pl FILE NEW OLD

now the CONTENT OF FILE BECOMES

Code
//Verilog HDL for "tt", "hh" "functional"  
// if i write the word here the script goofs up
`timescale 1ps/10fs


module NEW(Y, A, B );
output Y;
input A;
input B;

endmodule

which is good but if i write the word "module" in the comment line
i.e. CONTENT OF BEFORE SCrIPT IS RUN ON FILE which goofs up

Code
  
//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 );
output Y;
input A;
input B;

endmodule



Now the contents become

Code
//Verilog HDL for "tt", "hh" "functional"  
// if i write the word module NEW(Y, A, B );
output Y;
input A;
input B;

endmodule

which is unacceptable to me. plz help guys,
Good Luck,
Bill

(This post was edited by BillKSmith on Mar 9, 2013, 9:37 AM)


Kenosis
User

Mar 9, 2013, 9:25 AM

Post #3 of 5 (911 views)
Re: [sid.verycool] match at begin ^ not working for me [In reply to] Can't Post

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)


FishMonger
Veteran / Moderator

Mar 9, 2013, 10:20 AM

Post #4 of 5 (901 views)
Re: [BillKSmith] match at begin ^ not working for me [In reply to] Can't Post

FYI, the OP has cross posted this question on perlmonks and possibly other locations.

http://perlmonks.org/?node_id=1022549


Kenosis
User

Mar 9, 2013, 10:35 AM

Post #5 of 5 (895 views)
Re: [FishMonger] match at begin ^ not working for me [In reply to] Can't Post

Good catch, FishMonger. I wondered with the OPs use of <p> and <code> tags...

 
 


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

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