Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

invocant

Greetings:

When you invoke a method, the first argument to the method equal to the meth
od'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-Programmi
ng Chapter (ch. 12),  where accessor methods are being generated automatical
ly; 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

Report this thread to moderator Post Follow-up to this message
Old Post
Matthew Sacks
06-03-05 01:56 AM


Re: invocant
Matthew Sacks wrote:
>
> Greetings:
>
> When you invoke a method, the first argument to the method equal to the me
thod'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 th
e 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 Cha
pter (ch. 12),  where accessor methods are being generated automatically; in this ex
ample, it seems that the invocant is explicity placed into the first argument.  If y
ou
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 ;-)....

Report this thread to moderator Post Follow-up to this message
Old Post
Wiggins d'Anconia
06-03-05 01:56 AM


Re: invocant
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

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff 'japhy' Pinyan
06-03-05 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:44 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.