Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

package & class problem

by isha (Sexton)
on Jan 16, 2008 at 07:15 UTC ( [id://662629]=perlquestion: print w/replies, xml ) Need Help??

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

I have a package test.pm in which there are two packages test & feat.

feat package have a method new(feat is a class) & a method mymethod.

how can i call a method "mymethod" which is a class feat's method, in another temp.pm file without using fully qualified name i.e. feat::mymethod()?

Replies are listed 'Best First'.
Re: package & class problem
by ikegami (Patriarch) on Jan 16, 2008 at 08:01 UTC
    If it's a method, feat::mymethod() would be wrong. That would call it as a function.

    If it's a class method, feat->mymethod();
    If it's an instance method, $obj->mymethod();

Re: package & class problem
by Joost (Canon) on Jan 16, 2008 at 22:26 UTC
    In perl, methods are always called with either a class or an object as its first parameter, usually by writing:
    $class_or_object->method(other arguments);
    $class_or_object here can be a real object (an instance of a a class) or a class name (IOW, a string containing the package name designating the class), or a class name as a bare word.

    Note the -> arrow. That means that the function is going to be looked up and called as a method. If you call the function directly, as in MyPackage::some_method() the call will not behave as a method but just like a plain function call. In other words; perl distinguishes between methods and functions only by the way they are called.

    To answer your question directly: feat->mymethod() should work.

    Note that more-or-less officially, all-lowercase module/package names are reserved for pragmas, which are very uncommon outside the base distribution, so while nothing aside from a potential clash with an existing pragma will stop you from using all-lowercase names, it's generally frowned upon. It also makes it harder to spot class names.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found