Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: Re: OO Perl: calling a constructor within a class

by gbarr (Monk)
on Sep 26, 2001 at 20:49 UTC ( #114860=note: print w/replies, xml ) Need Help??


in reply to Re: Re: OO Perl: calling a constructor within a class
in thread OO Perl: calling a constructor within a class

In general using this code can cause an error, unless you can be sure that if $me is a reference that it is a blessed reference.

It is this that led to people doing

if(ref($me) && UNIVERSAL::isa($me,'my_class_name')) {

which is ugly, IMO.

The Scalar::Util package, that as of 5.7.2 is part of the perl distribution, contains a sub blessed() to get around this

if(blessed($me) && $me->isa('my_class_name')) {

Replies are listed 'Best First'.
Re: Re: Re: Re: OO Perl: calling a constructor within a class
by runrig (Abbot) on Sep 26, 2001 at 22:35 UTC
    if(ref($me) && UNIVERSAL::isa($me,'my_class_name')) {

    If you're doing a functional call, then there's no reason for the 'ref($me) &&' in that expression, though I think you get a warning on earlier versions of perl (not sure how early) if '$me' is undefined, so you might say:

    if(defined($me) && UNIVERSAL::isa($me,'my_class_name')) {
    Then again, you could just leave out the first part and turn off warnings locally :-)

    Update: Correction by tye duly noted.
    If I could transfer all the votes/XP from this node to yours I would...(maybe it could be a feature that our overworked fearless leader could implement someday :)

      No, you need the ref($me) to ensure that $me isn't just some string because UNIVERSAL::isa would then try to use that string as a package name to see if the named package inherits from your class. So to check whether $me is an object, you have to do both.

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://114860]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2021-05-16 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (151 votes). Check out past polls.

    Notices?