Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical


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]
[Corion]: Discipulus: Notepad++, or Perl ;)
[marto]: xubuntu or lubuntu
[Corion]: :-D
[marto]: my desktop is a core2 duo, 120GB SSD, 4GB RAM, boots to (GUI) login in less than 10 seconds from cold start
[Discipulus]: mah, winbuntu 10...
[marto]: whent he boys start school I'll build myself as new machine, ryzen based
[Corion]: marto: That's cool! I think my (home) desktop spends 30 seconds in the BIOS...
[Corion]: marto: Heh - currently they seem promising, but I think I'll stay with nVidia+Intel for the time being, as I've been bitten too often by bad AMD drivers
Discipulus is (still?) not a big fan of notepad++
[marto]: Corion, I think in the past this was a big problem for them. GPU driver wise the improve all the time. I use the open source drivers on my machine (old R9 270, 2GB) and had no problems

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-07-27 08:56 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (407 votes). Check out past polls.