Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Method names with colons in them?

by dmitri (Priest)
on Jul 08, 2005 at 13:26 UTC ( [id://473407]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Brethren,

I have the following piece of code:

ABC::XYZ->new->$autoloaded;

Obviously, method specified in $autoloaded is handled by AUTOLOAD. This works well if the value of the variable is something like 'abc', but does not work if the value is 'abc::xyz', as this results in the following run-time error:

Can't locate object method "xyz" via package "abc::xyz"

However, I still want to get AUTOLOAD called even if there are colons in the value of $autoloaded. Is there any way to do it?

P.S. I am using 5.6.1.

Update Thank you all for your responses! I think I now understand this a bit better.

Replies are listed 'Best First'.
Re: Method names with colons in them?
by artist (Parson) on Jul 08, 2005 at 13:34 UTC
    Of course, you are allowed experiment and try new things, but why you need to have method with colons in it? It is syntax preserved for modules etc.

    From perlmod:The symbol table for a package happens to be stored in the hash of that name with two colons appended. You can refer to variables and filehandles in other packages by prefixing the identifier with the package name and a double colon: $Package::Variable.

    --Artist
Re: Method names with colons in them?
by VSarkiss (Monsignor) on Jul 08, 2005 at 14:48 UTC
      Actually, the error message is misleading (the 'def' package is called, not 'def::xyz'--update: the correct error message appears in perl 5.8.4, but not 5.6.1), and AUTOLOAD doesn't get called at all. The package called is 'def', and if you comment out the 'sub xyz...', you still get the error (maybe you could consider it a bug...I haven't read the docs lately, though):
Re: Method names with colons in them?
by Fletch (Bishop) on Jul 08, 2005 at 14:47 UTC

    Method names with colons are taken as a hint to start looking for the method in the named class (e.g. the call $bar->ABC::xyz() means to call the xyz method on $bar, but start looking in package ABC rather than in whatever class $bar isa). In your example it's looking for the method xyz starting in (the probably non-existent) package abc.

    --
    We're looking for people in ATL

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://473407]
Approved by neniro
Front-paged by tlm
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found