Ok.. your use of @_ is just plain wrong, which is your main problem... we'll forgive it since you are learning :)
You use strict and warnings, that is good.
You use @_ so strict doesn't complain when you don't declare a variable, that is bad.
@_ is a special variable that Perl uses to provide you with the arguments that were passed to a subroutine. (which is why strict ignores it). What is happening is that you set @_ to two values, you then call handle_fix. Perl then silently replaces the values in @_ with that arguments passed to the subroutine (empty array), and runs the sub. On return Perl restores the old values of @_ and merrily continues along, making you angry :)
I would change the code to something more like...
se strict;
use warnings;
#why negate, reverse the operations. Also, specifying STDIN is more se
+lf documenting.
my $infile = @ARGV ? shift(@ARGV) : <STDIN>;
my $outfile = @ARGV ? shift(@ARGV) : <STDIN>;
handlefix($infile,$outfile);
open(INFILE, '<', $infile) or die "\nCan't open $infile: $!\n";
open(OUTFILE, '>', $outfile) or die "\nCan't open $outfile: $!\n";
print OUTFILE
map { my $s=$_; $s=~s/\s*#.*$//; $s }
(grep { !/^\s*#/ } <INFILE>), "\n" ;
#close INFILE && close OUTFILE; close returns a value... if the first
+close fails Perl will ignore second close
close INFILE;
close OUTFILE;
sub handlefix {
for(@_){
chomp($_);
$_=~s/"//g;
$_=~s/\//\\/g;
}
}
Now... keep in mind iterating through @_ and changing $_ directly WILL change the arguments passed to your sub, but is generally not the best practice. It is not obvious that the args are being changed and later that can bite you or someone else maintaining the code... a better way might be...
sub handlefix {
my @return;
for(@_){
my $val = $_; #decouple the value from the argument variable
chomp($val);
$val =~ s/"//g;
$val =~ s/\//\\/g;
}
return @return;
}
($infile,$outfile) = handlefix($infile,$outfile); #more obvious what i
+s happening
Hope that helps
- Ant
- Some of my
best work - (1 2 3)
|