Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Invalid argument

by suaveant (Parson)
on Dec 01, 2006 at 20:26 UTC ( [id://587292]=note: print w/replies, xml ) Need Help??


in reply to Invalid argument

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)

Replies are listed 'Best First'.
Re^2: Invalid argument
by Andrew_Levenson (Hermit) on Dec 01, 2006 at 20:31 UTC
    Wow, I ask for advice and get an entire mini-tutorial.
    THAT'S why I love it here.

    Thanks for the schooling, I never knew that you could specify variables to be used in a subroutine!
      my and local work in any scope...
      my $test = 1; print "$test\n"; { my $test = 2; print "$test\n"; test(3); } print "$test\n"; sub test { my($test) = @_; # parens make list assignment, otherwise you get the + count of items in @_ print "$test\n"; } #prints... 1 2 3 1
      You can also do something like
      while(my $var = shift @ARGV) { print "$var\n"; # var is scoped only in the while loop }

                      - Ant
                      - Some of my best work - (1 2 3)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-23 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found