in reply to Where can I learn more about blessed data?

I can't tell if blessed or unblessed

To determine if an object has been blessed or not, use the Scalar::Util blessed function.

use strict; use Scalar::Util 'blessed'; if ( defined blessed($some_scalar) ) { # ...do something... }

Replies are listed 'Best First'.
Re^2: Can I learn more about blessed data?
by Discipulus (Canon) on Jan 08, 2021 at 08:12 UTC
    Hello Bod,

    > To determine if an object has been blessed or not..

    If it is a perl object is was already blessed. To see if a datastructure is blessed you can also use ref

    perl -E "say ref {a=>33}" HASH perl -E "say ref bless{a=>33},'Some::Package'" Some::Package

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      To determine if any reference is blessed or not, I would highly recommend Ref::Util's is_blessed_ref() subroutine. That is precisely what it is designed for.

      I strongly suspect that determining the blessedness of a given reference is not what SergioQ was actually enquiring about, however.

      Update: sample test set to show it in action. You can throw anything at it and it won't complain - just return true if the arg is a blessed ref and false otherwise.

      use strict; use warnings; use Ref::Util 'is_blessed_ref'; use Test::More tests => 9; ok ! is_blessed_ref (undef), 'No: undef'; ok ! is_blessed_ref (0), 'No: false'; ok ! is_blessed_ref (1), 'No: true'; my ($x, @x, %x); ok ! is_blessed_ref (\$x), 'No: scalar ref'; ok ! is_blessed_ref (\@x), 'No: array ref'; ok ! is_blessed_ref (\%x), 'No: hash ref'; bless \$x, 'Foo'; bless \@x, 'Foo'; bless \%x, 'Foo'; ok is_blessed_ref (\$x), 'Yes: blessed scalar ref'; ok is_blessed_ref (\@x), 'Yes: blessed array ref'; ok is_blessed_ref (\%x), 'Yes: blessed hash ref';

      🦛