Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to call Class method inside an object?

by chorg (Monk)
on Apr 27, 2001 at 18:24 UTC ( [id://76107]=perlquestion: print w/replies, xml ) Need Help??

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

I am haveing a temporary (I hope ) brain meltdown. I have a class in which I am inheriting from Ima::DBI. I want to call some class methods in the module, to hide my implementation. What is the syntax for that?
_______________________________________________
"Intelligence is a tool used achieve goals, however goals are not always chosen wisely..."
  • Comment on How to call Class method inside an object?

Replies are listed 'Best First'.
Re: How to call Class method inside an object?
by dws (Chancellor) on Apr 27, 2001 at 18:38 UTC
    "Class methods" can mean two things, depending on which flavor of OO you grew up with.

    In the Smalltalk world, "class methods" are methods that are called without needing to have an instance of the class.   $baz = Foo::bar(); Methods invoked thusly don't have access to any instance data, but do have access to "class globals".

    Other schools of OO use the term to mean methods that are called via an instance of the class.   $baz = $instanceOfFoo->bar(); (Astute readers will note that may CGI.pm methods "switch hit", and can be called either way.)

    From your question, I'm guessing you mean the latter usage, and that you're looking for a way of doing "protected" methods, which are instance methods that are only visible (and hence callable) from an instance of the class, and not for anyone outside of the class.

    Perl doesn't directly support protected methods. But there are conventions. One convention is to prepend '_' to the method name to indicate that it's for internal use.

      I prefer the Smalltalk usage because it is extremely descriptive of what is happening.

      In Smalltalk, classes are objects. As objects they have methods. Those methods are the class methods.

      Instances of the class are objects. As objects they have methods. Those methods are instance methods.

      Since the class and the instance are different objects, and moreover different kinds of objects, it comes as no surprise that they have different methods.

      None of this is applicable in Perl, of course, since Perl packages are not objects. (Simon Cozens wanted to make that possible, but last I heard it got vetoed as potentially breaking too much old code.) But in languages where classes are themselves objects, the Smalltalk terminology is how you should think about things. And people who come from languages like that (for instance merlyn) tend in Perl to like distinguishing between methods that are meant to be called through the name of the class (eg new) and those that are called from an instance (everything else).

      Which is why I, along with merlyn, dislike the construct you sometimes see of:

      my $pkg = ref($self) || $self;
      Its purpose is to blur the distinction between methods of the class and methods of instances of the class, and we see no reason that that distinction is a bad thing to have.
        What distinction is that? Unless you're carrying around instance data pointed to by your blessed referent (which is the problem with CGI.pm), what's the effective difference?
        package MyClass; sub new { my $class = shift; bless(\$class, $class); } sub do_it { print "Doing something!\n"; } package main; my $c = MyClass->new(); my $class = 'MyClass'; $c->do_it(); $class->do_it();
        Now I'm open to the argument that having good access to data in $self is a big help, but method dispatch works either way. (I just re-read perltootc last night.)
Re: How to call Class method inside an object?
by merlyn (Sage) on Apr 27, 2001 at 18:35 UTC
    Perl doesn't really have a distinction between class methods and instance methods as an implementation: only as a convention. What doesn't work about simply using $self->method(@args)?

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

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

    No recent polls found