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.]
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|