in reply to Re: Sub Return Value Help
in thread Sub Return Value Help

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: Sub Return Value Help
by BurnOut (Beadle) on Aug 01, 2005 at 23:00 UTC

    Actually you can call a sub like so: sub_name()

    Behold the difference in the output of the following snippet

    print "Now testing with the ampersand\n"; &ampersand('arg1', 'arg2'); print "Now testing with the ampersand, but with 'blank' arguments\n"; &ampersand_blank_arg; print "Now testing without the ampersand\n"; no_ampersand('arg1', 'arg2'); sub ampersand { &two; } sub ampersand_blank_arg { &two(); } sub no_ampersand { two(); } sub two { print "@_\n"; } __OUTPUT___ Now testing with the ampersand arg1 arg2 Now testing with the ampersand, but with 'blank' arguments Now testing without the ampersand

    The side effect obviously visible with using the ampersand is that it passes the @_ from the sub that called it. And of course, this could easily be thwarted by just putting the empty parens behind the sub name (as demonstrated by ampersand_blank_arg). But I also read on this site ((tye)Re: A question of style) that using the ampersand screws with prototypes.

    In the beginning, I used them all the time. Then I pulled my hair out for about a week because I accidentally happened upon the @_ problem. I then started to put the empty parens behind them. But I'm human. I forgot sometimes and I'd kick my self everytime I found one that I missed. Then I found out that you don't need the ampersand if you use the parens and if you forget the parens, you get a bareword error. I love it when Perl tells me I've screwed up.

Re^3: Sub Return Value Help
by blazar (Canon) on Aug 02, 2005 at 08:43 UTC
    The &-form of sub call is obsolete and should never be used nowadays unless when one really know what he's doing.
    Lies! There is no other way to call a sub. Stop trying to confuse the newbie.
    Have you ever given a peek into perldoc perlsub? Since Perl5 is out there, that is...
    Update: re-reading the code I realize that obviously the OP's problem may just be that he calls his sub out of the while loop, then he uses the return value of that sub, saved into a variable, every time it is needed inside the loop. OTOH there are IMHO bigger problems with his code, as signalled above.
    Translation: The problem was too tough for you, so you resorted to attacking it for stylistic differeneces. Shame on you.
    Not only, I can't really speak nor read English, and I just cut and pasted portions of other nodes that at a first sight seemed loosely related with the subject matter discussed here and assembled them into a fake reply...