Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Static method vs virtual method

by vinoth.ree (Monsignor)
on May 09, 2009 at 08:44 UTC ( [id://763019]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

What is the difference between static method and virtual method in Perl package ?

Vinoth,G

Replies are listed 'Best First'.
Re: Static method vs virtual method
by moritz (Cardinal) on May 09, 2009 at 10:41 UTC
    In C++, a virtual method is one that can be redefined in inheriting classes. In that sense, all methods in Perl are virtual.

    In Java and C++ a method is called static, if it can be called on the class, not the object itself. In that sense, all methods in Perl are also static by default. You can explicitly disallow the static usage of a method with something like this:

    use Scalar::Util qw(blessed) sub non_static { my $self = shift; die "Don't use as a static method" unless defined blessed($self) ... }
Re: Static method vs virtual method
by lakshmananindia (Chaplain) on May 09, 2009 at 09:35 UTC

    Static methods expect a class name as the first argument. Generally, static methods ignore the first argument because they already know which class they are in. new() is implemented by almost all packages as a static method. Static methods aren't called from an instance of an object, but instead directly from the package name:

    Virtual methods or instance methods expect a reference to an object as the first argument. Static methods are class-wide; virtual methods are object-specific.

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


      Generally, static methods ignore the first argument because they already know which class they are in. new() is implemented by almost all packages as a static method.

      Yet, because of inheritance and possibility of being called on behalf of other package, well-behaving new() shouldn't ignore the first argument. In simplified case like this:

      sub new { my ($class, %opts) = @_; my $self = { %opts }; bless $self, $class; }

        Doesn't this advice directly contradict the advice of lakshmananindia, who states that new() should be static? It seems that there is a lack of consensus on whether new() should be static or virtual. This is a real problem, because no facility is provided to force the caller to invoke new() the correct way by producing an error if the invocation is wrong.

Re: Static method vs virtual method
by ww (Archbishop) on May 09, 2009 at 12:41 UTC
Re: Static method vs virtual method
by GrandFather (Saint) on May 10, 2009 at 23:58 UTC

    To the extent that those terms apply to Perl a 'virtual method' is called on an object instance using ->:

    $obj->methodName (...);

    A 'static method' is simply a sub in an OO package that provides functionality related to the package's object type, but that isn't called in the context of an object instance. new is of this nature - it is used to create a new instance of the package's object.


    True laziness is hard work
Re: Static method vs virtual method
by Anonymous Monk on May 09, 2009 at 08:55 UTC
    Perl doesn't have those.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-16 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found