question about 'bless'

K

Kim Jonguk

Hi.

i'm reading a book about OOP with perl.
in constructor, for example

sub new
{
my ($class, %args) = @_;
...
bless { ... }, ref($class) || $class;
}

i'm wondering this. "ref($class) || $class".
i cannot understand this phrase.
plz, explain to me.

thanks for reading. :)
 
B

Brian McCauley

Kim Jonguk said:
i'm reading a book about OOP with perl.
in constructor, for example

sub new
{
my ($class, %args) = @_;
...
bless { ... }, ref($class) || $class;
}

i'm wondering this. "ref($class) || $class".
i cannot understand this phrase.

Can you tell us what parts (if any) of the reference manual
descriptions of the bless() funtion, ref() function and the ||
operator you can't understand?

Without this information we are just likely to repeat what the manual
says and you'll be no better off.

I shall, therefore, proceded on the assumuption that you've read and
understood those manual entries.

The first argument passed to a subroutine being called as a method is
an object reference for an instance method call and a string
containing the class name for a class method call.

A constructor is usually called as a class method.

The variable $class, in this case, therefore contains the class name.

However the author of the above code obviously wanted to allow new to
be callable as an instance method too. In that case $class would
contain an object reference and ref($class) would return the class
name.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
A

Anno Siegel

Kim Jonguk said:
Hi.

i'm reading a book about OOP with perl.
in constructor, for example

sub new
{
my ($class, %args) = @_;
...
bless { ... }, ref($class) || $class;
}

i'm wondering this. "ref($class) || $class".
i cannot understand this phrase.
plz, explain to me.

It's an idiom that makes the new() method usable as both an object
method and a class method. In a class-method call, (the normal way
to call new()), $class contains a string with a class name. In that
case, "ref( $class)" is "" (a false value), and the result of "ref( $class)
|| $class" is $class itself. If you call new() through an object,
$class will contain that object. Then, "ref( $class)" is the name
of the object's class. In both cases, bless() is called with a
class name, as it should be.

That said, in my opinion, using new() as an object method is not a
good idea. If "$object->new( ...)" creates an entirely independent
object, the presence of "$object" in the call is confusing, since it has
no consequences. If "$object->new( ...)" creates a clone of object,
then call the method "clone" or "copy", but not "new".

So, while the construct works and gives the new() method a spurious
universality, I'd just replace ""ref($class) || $class" with "$class".

Anno
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top