in reply to Class / package weak association
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
HTH! :)
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Class / package weak association
by dd-b (Pilgrim) on Jun 10, 2021 at 21:32 UTC | |
by LanX (Saint) on Jun 11, 2021 at 11:07 UTC |