http://www.perlmonks.org?node_id=680420

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

So I swear I have seen this before... But I can't remember under what conditions it happens, so I can't avoid it.

I have a sub for which all parameters are optional. And sometimes... when I call it with no parameters, the parameters of the proc it was called from show up as it's parameters...

Anyone know under what circumstances this happens, and of course how to avoid it from happening?

rpelak

Replies are listed 'Best First'.
Re: phantom subroutine parameter values
by blahblahblah (Priest) on Apr 15, 2008 at 03:08 UTC
    This will happen when you call a sub with an ampersand but without any parentheses, like the following:

    ⊂

    Doing so makes the calling sub's @_ visible to called subroutine. To avoid this, either omit the ampersand or include empty parens when calling a sub with no args. See perlsub for more info.

      Thanks guys. I knew it was something simple... That explains why a lot of my old code doesn't have ampersands... Sucks be forced to program in tcl for a few years... rpelak
        uh?! it seems a feature! in any case i like calling with the & 'cause i spot easy subroutine's calling.

        if i understand :
        sub refinemydata { my $data = shift; &is_refined:return?&rerefine;}
        could be a valid syntax too ?
        something new

        Lor*
Re: phantom subroutine parameter values
by GrandFather (Saint) on Apr 15, 2008 at 03:06 UTC

    Most likely you are using & to call the sub: &subToBeCalled. Don't do that! ;)


    Perl is environmentally friendly - it saves trees