Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Why is this incorrect?

by kettle (Beadle)
on Mar 17, 2006 at 02:10 UTC ( #537367=perlquestion: print w/replies, xml ) Need Help??

kettle has asked for the wisdom of the Perl Monks concerning the following question:

I am using a couple of subroutines in a basic script to parse a text file:

#!/usr/bin/perl -w use strict; my $text, $ques, $ans; open(IN, "<$ARGV[0]")||die"Couldn't open $ARGV[0]: $!\n"; while(<IN>){ $text = &do_one_thing($_); ($ques, $ans) = &do_another_thing($text); print $ques." :: ".$ans."\n"; } close(IN); sub $do_one_thing{ my $line=uc($_); $line =~ s/this/that/g; return($line); } sub $do_another_thing{ my $another_line, $one, $two; $another_line = $_; ($one, $two) = $another_line =~ /(.[^\*]*)\*(.*)/; return($one, $two); }


So my question is, why do $ques and $ans remain lowercase; more generally, why does the second subroutine again use $_ from the main body of the program, rather than the local $_ (which I'm guessing ought to be $text)? I can rearrange the script pretty simply to make it work (by not using the extra variable) which I did, but it made me curious as to why it doesn't work the way I've written it above...

thanks!

Replies are listed 'Best First'.
Re: Why is this incorrect?
by rafl (Friar) on Mar 17, 2006 at 02:18 UTC

    $_ is a global.

    Beside that to get the first parameter you passed to your subs you should use

    my $line = lc $_[0]; # and $another_line = $_[0];

    as it is the first element in the @_ array, which contains all arguments to the sub.

    Beside that you should not use $ in sub names, you should not use & in function calls and you should use warnings.

    Cheers, Flo

      you can override the $_ for the subrouting by declaring it with 'local'. This may make things easier in the future for you.
      I guess I should look this up, but why should '&' not be used in function calls? thanks for the other advice!
Re: Why is this incorrect?
by japhy (Canon) on Mar 17, 2006 at 02:19 UTC
    You've failed to show working code. And your parameter passing to your functions is wrong. You need to get the arguments from the @_ array, not from $_.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Why is this incorrect?
by spiritway (Vicar) on Mar 17, 2006 at 04:13 UTC

    In looking at your code, the first thing I noticed was your line 3:

    my $text, $ques, $ans;

    Right away you should have gotten an error about globals needing explicit package name. To declare multiple variables you need to enclose them in parenthese:

    my ($text, $ques, $ans);

    The same problem exists on line 20:

    my $another_line, $one, $two;

    Next, you have:

    sub $do_one_thing{...;

    which is also an error. You need to remove the sigils from this and the subsequent sub.

    From what you've shown, I can't imagine this program producing any output.

      you're right, it probably doesn't produce any output. i didn't check it. the actual script that I am using is much longer, and I didn't want copy the whole thing - just to ask about what I didn't understand. (sorry for not checking). anyway, despite that I did get the answer to the question I was asking, so thanks a bunch! i was going to add that i don't really know what I'm doing yet, but i suppose an explicit statement was unnecessary!
Re: Why is this incorrect?
by trammell (Priest) on Mar 17, 2006 at 02:23 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://537367]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2021-04-17 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?