CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Crazy! Class method invocation with ->



May 29, 2002, 9:40 AM

Post #1 of 3 (2417 views)
Crazy! Class method invocation with -> Can't Post

I recently came across this syntax:


use Xerox_Machine;


This is just an example by the way, I don't know of a Xerox_Machine module in real life. Now, as is standard, inside is the a bunch of code that is all preceded by a "package Xerox_Machine;". So assuming that Xerox_Machine is a properly written object-oriented module, you would need to create an object of that class to implement it's method's properly. Otherwise, you could run it's subs by saying:


It surprised me that Perl would allow you to replace an object on the left side of the dereference operator with a class name. I haven't seen any documentation on that.

I tested this out with some random modules and it works. Can anyone else verify? I'm running v5.6.1 Binary Build 626 from IndigoSTAR.




May 29, 2002, 1:32 PM

Post #2 of 3 (2415 views)
Re: [Coderifous] Crazy! Class method invocation with -> [In reply to] Can't Post

What you described is actually the normal way of invoking a class method. The :: syntax is more likely to be used for calling functions in another package without importing them into your namespace. The difference between the two ways of invocation is that when a subroutine is called as a class method, the name of the class is passed as the first argument. (Just as an object reference is passed as the first argument when you invoke a subroutine as an object method.)

package Foo; 
sub subroutine { print "sub: @_\n"; }

package main;
Foo::subroutine( "arg" );
Foo->subroutine( "arg" );

Thus, the above code will print:

sub: arg 
sub: Foo arg

Hope this makes things clearer.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo


May 29, 2002, 6:39 PM

Post #3 of 3 (2412 views)
Re: [mhx] Crazy! Class method invocation with -> [In reply to] Can't Post

Tongue I made an amateur mistake and posted before researching. Early in the OO chapter from the Camel, it explains this syntax. I guess I just skipped that page from early on. Read it this afternoon during the ride home and had to smack myself.

It makes sense, I just don't remember ever seeing it before and so it seemed strange to me. Thanks for the clarification though, Marcus.

By the way, I just landed an excellent job using Perl all day long. It's a 25,000 dollar increase from what I'm making currently. Not too shabby. Cool

Thanks again!



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives