Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)


In reply to Re: Invalid argument by suaveant
in thread Invalid argument by Andrew_Levenson

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found