Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

blessed

by demerphq (Chancellor)
on Dec 05, 2002 at 15:39 UTC ( #217784=snippet: print w/ replies, xml ) Need Help??

Description: Similar to ref, but only returns the name if the item is a blessed class. In fact there are two subs, one for when you consider qr// constructs to be blessed/objects and one for when you don't. The reason the latter is provided is because a qr// is not really a user defined class type. For some uses this may be a required distinction (I have needed both at times).

Update: For most uses you are probably better off stealing GBARR's solution as posted by jmcnamara in a reply to this node. Thanks jmcnamara and gbarr. :-)

use overload; # required for either

# qr// constructs are NOT considered blessed objects.
sub blessed($) {

   (ref($_[0]) && (index(overload::StrVal($_[0]),'=')>=0) 
    && overload::StrVal($_[0])!~/^Regexp=SCALAR/) 
      ? ref($_[0]) : ""
} 

# qr// constructs ARE considered blessed objects
sub blessed($) {
   (ref($_[0]) && (index(overload::StrVal($_[0]),'=')>=0)) 
      ? ref($_[0]) : ""
} 
Comment on blessed
Download Code
•Re: blessed
by merlyn (Sage) on Dec 05, 2002 at 16:14 UTC
      Funnily enough i was just coming back to put a link to that module into my description. Thanks. Also I just ran the full blessed.t from the Scalar::Util and both of my snippets pass all tests there (they arent that rigorous though, for instance no checks of qr// to be seen at all, nor blessed qr//'s)

      Incidentally for the average Win32:: user this module isnt much help as it requires a compiler. (Oops, last time I checked ppm didnt have a version available, they do now.)

      Nevertheless, for people who dont need all that Scalar::Util provides this will work just as well.

      Cheers,

      --- demerphq
      my friends call me, usually because I'm late....


        Incidentally for the average Win32:: user this module isnt much help as it requires a compiler.

        Scalar::Util also contains pure Perl versions of the functions for systems without a C compiler. Here is the Perl version of blessed() directly from the source:

        # The code beyond here is only used if the XS is not installed # Hope nobody defines a sub by this name sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) } sub blessed ($) { local($@, $SIG{__DIE__}, $SIG{__WARN__}); length(ref($_[0])) ? eval { $_[0]->a_sub_not_likely_to_be_here } : undef }

        --
        John.

        That's why I opened up my repository. One PPM of Scalar::Util coming right up.

        http://crazyinsomniac.perlmonk.org/perl/ppm/Scalar-List-Utils.ppd

        update: I guess I ought to write PPM::Repositories or PPM::RepositoryList and release it on CPAN.


        MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
        ** The Third rule of perl club is a statement of fact: pod is sexy.

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2014-08-29 03:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls