Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery


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]) : ""
Replies are listed 'Best First'.
•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.


      --- 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 }


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

        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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2016-10-24 00:46 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (302 votes). Check out past polls.