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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Coderifous
journeyman

May 29, 2002, 9:40 AM

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

I recently came across this syntax:


Code
  

use Xerox_Machine;



Xerox_Machine->purge_papers();




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 Xerox_Machine.pm 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:

Xerox_Machine::purge_papers();

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.

Mystified,

--jim


mhx
Enthusiast

May 29, 2002, 1:32 PM

Post #2 of 3 (1248 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.)


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

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


Thus, the above code will print:


Code
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



Coderifous
journeyman

May 29, 2002, 6:39 PM

Post #3 of 3 (1245 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!

--jim

 
 


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

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