Ask a Question related to PERL Miscellaneous, Design and Development.
-
Kim Jonguk #1
question about 'bless'
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. :)
Kim Jonguk Guest
-
how to "bless" while "strict"?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm working through perlboot right now and have run up against a minor challenge. Unlike the... -
Thanks and God bless!
Goodday, This might seem very deplorable for a person that you do not know but as the title implies ,I am Omoh Abacha, the Second son of the late... -
PURCHASE, GOD BLESS YOU.
GOOD DAY, This is to inform you that I’m very interested in your property . I live in UK and investing on properties abroad and on fixed assets... -
Brian McCauley #2
Re: question about 'bless'
Kim Jonguk <kju@ajou.ac.kr> writes:
Can you tell us what parts (if any) of the reference manual> 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.
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\\
Brian McCauley Guest
-
Anno Siegel #3
Re: question about 'bless'
Kim Jonguk <kju@ajou.ac.kr> wrote in comp.lang.perl.misc:
It's an idiom that makes the new() method usable as both an object> 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.
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
Anno Siegel Guest



Reply With Quote

