How do you call the part with the underscore in thisshift->_date_header('Date',@_)

G

gauthier

Looking into HTTP::Headers.pm, I am trying to understand what does the
underscore in line 267 mean.

sub date { shift->_date_header('Date',@_); }

Thefile version/date I used is : # $Id: Headers.pm,v 1.64 2005/12/08
12:11:48 gisle Exp $

I thought it was an overload but after reading a bit it does not look
like this. Overload dont seem to use an underscore as a valid
operator. The only code I could find in my Camel book is the "use
fields" page 846 of third edition. But in this code, it does not have
shift, it has only the object ref:

$rock->{_Pet_pid} = 1234; #private attribute

I have not found documentation about private attributes either.

Question 1:
How do you call this part ->_ within the next line:
sub date { shift->_date_header('Date',
@_); }

Question 2:
In this line:
sub date { shift->_date_header('Date',
@_); }
Is the shift representing the first argument of the array @_ from the
inheritance?
Thanks.
 
B

Ben Morrow

Quoth (e-mail address removed):
Looking into HTTP::Headers.pm, I am trying to understand what does the
underscore in line 267 mean.

sub date { shift->_date_header('Date',@_); }

It's part of the method name. $obj->_date_header(...) is calling a
method called '_date_header' on $obj. If you look through
HTTP/Headers.pm, you will find a definition of 'sub _date_header'.

The initial underscore means nothing to Perl. However, it is a common
convention to use a leading underscore to indicate private methods that
other people should not call.
I thought it was an overload but after reading a bit it does not look
like this. Overload dont seem to use an underscore as a valid
operator. The only code I could find in my Camel book is the "use
fields" page 846 of third edition. But in this code, it does not have
shift, it has only the object ref:

$rock->{_Pet_pid} = 1234; #private attribute

I have not found documentation about private attributes either.

Perl (or rather, the common implementation of Perl objects as blessed
hashrefs) doesn't support the notion of 'private attributes'. Again, a
leading underscore is used to indicate to the reader that this attribute
should not be tampered with.
Question 2:
In this line:
sub date { shift->_date_header('Date',
@_); }
Is the shift representing the first argument of the array @_ from the
inheritance?

Yes. In a method call the first argument will be the object or class the
method is invoked on.

Ben
 
J

Jim Gibson

Looking into HTTP::Headers.pm, I am trying to understand what does the
underscore in line 267 mean.

sub date { shift->_date_header('Date',@_); }

It is part of the name '_date_header'. The "public" object method
'date' calls the "private" object method _date_header, passing it the
same argument list. The first argument to date is the object reference,
which is shifted off the front of the array @_. The remaining arguments
in @_ are passed to _date_header.

Putting an underscore at the beginning of a method name is a common way
of differentiating private methods, which should not be called from
outside the module, to public ones that can be called. Perl doesn't
have the concept of public and private methods, so authors use a
convention like this to indicate which methods are "public" and which
"private".
 
G

gauthier

After reading your replies, it became so clear that I was frustrated I
did not get it by myself.

Thanks to all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top