Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Unexpected value in incoming variable.

by SavannahLion (Pilgrim)
on Jan 10, 2004 at 06:26 UTC ( [id://320305] : perlquestion . print w/replies, xml ) Need Help??

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

I'm exporting some functions into a seperate module and I'm getting some awkward behavior. I spent a lot of time boiling it down to figure out what was happening. For a long time, I kept thinking I wasn't passing scalars back and forth correctly :(

I boiled it down to an example in these two modules (they don't reflect the actual code). For sake of argument, they're just entitled module1 and module2:

use strict; use lib ('./'); require ''; Alice->mTest2 ("Some stuff", "and something else"); 1;
the other module contains:
use strict; package Alice; sub mTest2 { my ($junk, $and) = @_; print "$junk=$junk\n\$and=$and"; }

Naturally, I would expect it to print out
Some stuff
and something else.

Instead I get:

Some stuff

Ok, after doing some searching and a bit of experimentation, I can see where a person would want to know the context the function is called. I fried my brain and all I can come up with is a shift on the @_ to look for the object name. If it matches, then proceed to get the rest of the array. If it doesn't match, then assume it's a variable I want to keep and proceed accordingly. But this seems really sloppy when all I really care about is the variables I want to deal with, not the package name.

I guess what I'm asking is, can someone clarify if this is some sort of error on my part, or is this perfect behavior (which I assume it is)? I tried to flip through the Camel to see if this is mentioned, but I can't find it :(
Gah! I need coffee...

Thanks for your patience.

Prove your knowledge @ HLPD

Replies are listed 'Best First'.
Re: Unexpected value in incoming variable.
by Coruscate (Sexton) on Jan 10, 2004 at 06:39 UTC

    Within a package, the first argument/parameter passed to a method is the package name, unless you are dealing with an object, in which case the first argument returned is the object itself. So to get your wanted results, you just need to add another variable capture:

    package Alice; use strict; sub mTest2 { my ($self, $junk, $and) = @_; print "$junk=$junk\n\$and=$and"; }

      Within a package, the first argument/parameter passed to a method is the package name

      This is of course only true if you call the sub via the class syntax (either class->method or method class (how did you think new class worked?))

      If you just want a namespace and don't want to bother with package/object syntax then use double colons to delimit packages for your function call, as in Alice->method(); becomes Alice::method().
Re: Unexpected value in incoming variable.
by Anonymous Monk on Jan 10, 2004 at 12:03 UTC
    I think "module" is reserved for files ending in .pm, where the package declaration matches the filename.