in reply to Blessing with unknown classnames
G'day bliako,
There is no requirement for $class to be a real class, have length, or even be present. See bless for details.
The use of ref in your posted code is not the best choice: the doco explains issues. It would be much better to use the blessed function of the builtin module Scalar::Util.
Here's an example of usage. My perle alias captures all sorts of problems.
$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'
$ perle ' use Scalar::Util "blessed"; for (1, 2) { say "Run: $_"; my $ret = foo(); my $err = blessed $ret; if (defined $err) { say qq{Error "$err" detected}; say qq{Code: $ret->[0]}; say qq{Errstr: $ret->[1]}; } else { say qq{Success!}; say for @$ret; } } sub foo { state $error = 0; my $retval; if ($error) { $retval = bless [0, "error was ..."] => "YouveGotError"; } else { $retval = [42, 43]; } $error ^= 1; return $retval; } ' Run: 1 Success! 42 43 Run: 2 Error "YouveGotError" detected Code: 0 Errstr: error was ...
[Note: Between reading the OP and clicking "Comment on", there appears to have been two updates to the OP. There may be others. I'm replying to the post that ended with "Thanks LanX for looking this up on CB". I don't believe this changes the essence of my reply; just bear in mind what I'm replying to.]
— Ken
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Blessing with unknown classnames
by bliako (Abbot) on Apr 01, 2021 at 18:19 UTC | |
by kcott (Archbishop) on Apr 01, 2021 at 18:55 UTC | |
by bliako (Abbot) on Apr 01, 2021 at 19:42 UTC | |
by haukex (Archbishop) on Apr 02, 2021 at 09:32 UTC | |
by bliako (Abbot) on Apr 02, 2021 at 11:12 UTC | |
|