> If a blessed object kept track of whether it was associated with a package of that name, and the information went into the Dumper output, then when the string was evaled, if there was no package of the right name loaded, it could point that out (optional extra parameter to bless, or something?).
OK if you only want to be warned if the package doesn't exist, you can also override bless locally inside a package to do the check for you.
please note that blessing will autovivify the class, i.e. the package TST1:: will "exist".
use strict;
use warnings;
use Data::Dumper;
my $obj = bless {}, "TST1";
my $str1 = Dumper $obj;
my $str2 = q($VAR1 = bless( {}, 'TST2' ););
package Bless::Safe;
use subs qw/bless/;
sub bless ($;$) {
my ($obj,$class) = @_;
die "Can't bless into non-existent Package $class"
unless exists $main::{"${class}::"};
}
our $VAR1;
eval($str1) or $@ && die $@; # OK
eval($str2) or $@ && die $@; # FAILS
Can't bless into non-existent Package TST2 at d:/tmp/pm/override_bless.pl line 21.
HTH! :)
updates
fixed bug in $@ check
added prototype
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.