my $self; { my %hash; $self = bless(\%hash, $pkg); }
It's quoted from HTML/Template.pm,why not simply bless $self,$pkg?
I think the intention was to limit the scope of %hash to the enclosing block. This can be rewritten as:
my $self = bless {}, $pkg;
$hash = {};
^^^^^ ^^
referrer referent
In the following statement:
$self = bless( $hash, $pkg );
bless marks the referent (the anonymous hash) to which $hash refers, as being an object of $pkg (the HTML::Template class). It does not alter the $hash variable.
The bless function returns a reference to the blessed anonymous hash. $self therefore becomes a reference to the blessed anonymous hash (the referent).
It is important to remember that if the the first argument to the bless function is a reference, it is the thing to which it refers that is blessed, not the variable itself:
$self = bless( $self, $pkg );
$self doesn't refer to anything - it is undef. There is no referent to bless. It is equivalent to attempting this:
$self = bless( undef, $pkg );
Bless My Referents provides a great introduction to the subject.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With