bliako has asked for the wisdom of the Perl Monks concerning the following question:
What happens when a ref is blessed into a non-existent class. Afterall bless's 2nd argument is a string and not a package qualifier.
bless [1,2,3] => 'nonexistent'Some (irrelevant) info:
my use case is for "tagging" basic Perl data structures.
For example, a sub normally returns an arrayref. But in case of error it returns again an arrayref as: [code,errstr]. How can I differentiate the 2 cases? (Well the obvious way would be to create a proper Error class and return that in case of errors), but just for fun:
sub foo { if( $error ){ return bless [0, "error was ..."] => 'YouveGotError' } return [42,43] } my $ret = foo(); die "@$ret" if ref($ret) eq 'YouveGotError';
Thanks LanX for looking this up on CB and Corion for pointing to %main:: (I was looking in %INC)
2' Edit: here is another use-case for distinguishing between scalar(ref)s
sub fortunecookie { my $cookie = ...; if( ! $cookie ){ my $errstr = "failed to get a cookie because ..."; return bless \$errstr => 'YouveGotError' } return \$cookie } # ok a bit awkward with the stringref but it demonstrates that one can + do this with scalars too
blw bli ako
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Blessing with unknown classnames
by kcott (Archbishop) on Apr 01, 2021 at 17:35 UTC | |
by bliako (Monsignor) on Apr 01, 2021 at 18:19 UTC | |
by kcott (Archbishop) on Apr 01, 2021 at 18:55 UTC | |
by bliako (Monsignor) on Apr 01, 2021 at 19:42 UTC | |
by haukex (Archbishop) on Apr 02, 2021 at 09:32 UTC | |
| |
Re: Blessing with unknown classnames (updated x 2)
by LanX (Saint) on Apr 01, 2021 at 16:44 UTC | |
by choroba (Cardinal) on Apr 01, 2021 at 23:44 UTC | |
by Discipulus (Canon) on Apr 02, 2021 at 07:45 UTC | |
by LanX (Saint) on Apr 02, 2021 at 08:46 UTC | |
by LanX (Saint) on Apr 03, 2021 at 01:04 UTC | |
by LanX (Saint) on Apr 03, 2021 at 01:46 UTC | |
by LanX (Saint) on Apr 02, 2021 at 00:04 UTC | |
Re: Blessing with unknown classnames
by perlfan (Vicar) on Apr 02, 2021 at 05:27 UTC |