For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > invocant









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author invocant
Matthew Sacks

2005-06-02, 8:56 pm



Greetings:

When you invoke a method, the first argument to the method equal to the method's invocant is automatically inserted.

i) Is this essentially the same as a C++ "this" pointer?

ii) Apparently, the first argument can be a reference to its object, or the object.

iii) There is an example in the Camel book, in the Object-Oriented-Programming Chapter (ch. 12), where accessor methods are being generated automatically; in this example, it seems that the invocant is explicity placed into the first argument. If you pu
t in the this pointer, does PERL know not to put in another one?

matthew s


---------------------------------
Yahoo! Mail
Stay connected, organized, and protected. Take the tour
Wiggins d'Anconia

2005-06-02, 8:56 pm

Matthew Sacks wrote:
>
> Greetings:
>
> When you invoke a method, the first argument to the method equal to the method's invocant is automatically inserted.
>


Correct.

> i) Is this essentially the same as a C++ "this" pointer?
>


Yes, the Perl idiom generally looks like:

sub method {
my $self = shift;
my @args = @_;
...
}

Where the first line, $self = shift, removes the first argument and
stores it to $self for future access. Then the rest of the passed
argumetns show up in the rest of @_, storing them to another location is
by choice.

perldoc -f shift

> ii) Apparently, the first argument can be a reference to its object, or the object.
>


In Perl those are really the same. An object is really just a blessed
reference,

perldoc -f bless

Though there can be the distinction of a class method vs. an object
method, where the first argument won't be a reference/object, but
instead the name of the class. This is basically how a constructor is
written, though any other class method would do the same.

> iii) There is an example in the Camel book, in the Object-Oriented-Programming Chapter (ch. 12), where accessor methods are being generated automatically; in this example, it seems that the invocant is explicity placed into the first argument. If you

put in the this pointer, does PERL know not to put in another one?
>


My camel is still packed so I can't address this specific issue. Perl
automatically adds the first argument whenever a subroutine is called in
method format. It won't check the other arguments to see if they are
references/class names, so for sure it can't tell. Having said that
depending on the method used for accessor generation it might need this
for some reason.

You might also be interested in the OOP docs that come with Perl,

perldoc perl

For a list of those.

http://danconia.org

p.s. Perl is the language, perl is the interpreter, PERL does not
compute ;-)....
Jeff 'japhy' Pinyan

2005-06-02, 8:56 pm

On Jun 2, Matthew Sacks said:

> When you invoke a method, the first argument to the method equal to the
>method's invocant is automatically inserted.


That sounds about right. The first argument to a method is either a class
name or the object, depending on whether it's a class method or an object
method:

my $foo = SomeClass->new('blah');

The arguments to the 'new' method are ('SomeClass', 'blah').

$foo->some_method(10, 50);

The arguments to the 'some_method' method are ($foo, 10, 50).

> i) Is this essentially the same as a C++ "this" pointer?


Yes; the first argument to an object method is the object, which is
synonymous to the 'this' pointer in C++.

> ii) Apparently, the first argument can be a reference to its object, or
>the object.


Uh, that sounds wrong. Objects *are* references in Perl, just a special
kind of reference. An object is a reference that has been "blessed" into
a particular package/class/namespace:

package SomeClass;

sub new {
my $pkg = shift;
# $pkg is the class to bless into... it is not
# NECESSARILY SomeClass, because a class could
# be inheriting its new() method from SomeClass

my $obj = bless [@args], $pkg;
return $obj;
}

A reference to an object is not necessarily an object. That is:

$foo->some_method(...);

works, but

my $ref_to_foo = \$foo;
$ref_to_foo->some_method(...);

does NOT work.

> iii) There is an example in the Camel book, in the
>Object-Oriented-Programming Chapter (ch. 12), where accessor methods are
>being generated automatically; in this example, it seems that the
>invocant is explicity placed into the first argument. If you put in the
>this pointer, does PERL know not to put in another one?


I don't have my Camel at hand. Is this an AUTOLOADing issue?

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com