Request for help with search & replace script

E

Ethan

Hi. I'm trying to write a script that will search through files in a
website and replace certain strings with others (in some cases just
delete them). My overall goal is to convert a JSP site to plain html
removing all the JSP code and adding some SSIs.

I adapted the subroutine below which was posted on 10/8 to devshed.com
by "raklet". When I run it there are no error messages and the print
statements work as expected. However when I go to look at the files,
in some files it appears that nothing has happened, and in other cases
all data in the file has been removed.

I would be grateful for any comments or information on why this isn't
working.

Alternately, if anyone could point me in the direction of a better
solution altogether that would be great too.

I'm not a Perl expert sorry to say.

Thanks,

E

#----------------------------------------------------------

sub doReplace
{
my @outLines; #Data we are going to output
my $line; #Data we are reading line by line

# Only parse files that end in .htm
if ( $File::Find::name =~ /\.htm$/ )
{
print "\nprocessing $_\n";

open (FILE, $File::Find::name ) or
die "Cannot open file: $!";

while ( $line = <FILE> )
{
$line =~ s/\.jsp$/.htm/i;
$line =~ s/<jsp:setProperty.+\>//i;
$line =~ s/<jsp:useBean.+\>//i;
push(@outLines, $line);
}
close FILE;

open ( OUTFILE, ">$File::Find::name" ) or
die "Cannot open file: $!";

print ( OUTFILE @outLines );
close ( OUTFILE );

undef( @outLines );
}
}
 
T

Tad McClellan

Ethan said:
Hi. I'm trying to write a script that will search through files in a
website and replace certain strings with others (in some cases just
delete them).

I adapted the subroutine below which was posted on 10/8 to devshed.com
by "raklet".

I would be grateful for any comments or information on why this isn't
working.


Too much "greed" perhaps...

Alternately, if anyone could point me in the direction of a better
solution altogether that would be great too.


Perl has features that support in-place editing for you. Let
perl do the administrative housekeeping stuff for you.

I'm not a Perl expert sorry to say.


I don't think raklet is either.

#----------------------------------------------------------

sub doReplace
{
my @outLines; #Data we are going to output
my $line; #Data we are reading line by line

# Only parse files that end in .htm
if ( $File::Find::name =~ /\.htm$/ )
{
print "\nprocessing $_\n";

open (FILE, $File::Find::name ) or
die "Cannot open file: $!";

while ( $line = <FILE> )
{
$line =~ s/\.jsp$/.htm/i;
$line =~ s/<jsp:setProperty.+\>//i;
^^
^^ should be non-greedy?
$line =~ s/<jsp:useBean.+\>//i;
^
^ useless use of backslash

I am suspicious of programmers who don't know what needs
backslashing and what doesn't need backslashing...

push(@outLines, $line);
}
close FILE;

open ( OUTFILE, ">$File::Find::name" ) or
die "Cannot open file: $!";

print ( OUTFILE @outLines );
close ( OUTFILE );

undef( @outLines );


I am suspicious of programmers that don't know how scoping works...



My rewrite:

# untested!
sub doReplace {
return unless /\.htm$/; # save a level of indent
local @ARGV = $_; # load filename for inplace edit
local $^I = ''; # enable inplace editing. No backup!
while ( <> ) {
s/\.jsp$/.htm/i;
s/<jsp:setProperty.+?>//i;
s/<jsp:useBean.+?>//i;
print;
}
}


You can read up on Perl's variables such as @ARGV and $^I in:

perldoc perlvar
 
T

Tore Aursand

sub doReplace
{
my @outLines; #Data we are going to output
my $line; #Data we are reading line by line

# Only parse files that end in .htm
if ( $File::Find::name =~ /\.htm$/ )
{
print "\nprocessing $_\n";

Think about just skipping the files you don't want to process instead, as
it (IMO) eases the readability;

sub doReplcae {
next unless ( $File::Find::name =~ /\.htm$/ );
# ...
while ( $line = <FILE> )

You shouldn't have declared $line outside of this scope;

while ( my $line = <FILE> ) {
# ...
}

In this case, you don't even need $line, _and_ it eases the readability;

while ( <FILE> ) {
s/<jsp:setProperty.+>//;
s/<jsp:useBean.+>//;
push( @outLines, $_ );
}
$line =~ s/<jsp:setProperty.+\>//i;
$line =~ s/<jsp:useBean.+\>//i;

You're escaping characters that don't need escaping;
undef( @outLines );

No need to, really. Perl takes care of cleaning up your mess (most of the
time). :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top