How do you understand this trick?

my $self; { my %hash; $self = bless(\%hash, $pkg); }

This is a quote from HTML/Template.pm, why not just bless $self,$pkg?

+5
source share
3 answers

I think the intention was to limit the area to a %hashclosing block. This can be rewritten as:

my $self = bless {}, $pkg;
+4
source
 $hash  =   {};
 ^^^^^      ^^
referrer referent

In the following statement:

$self = bless( $hash, $pkg );

blessdenotes the referent (anonymous hash) to which it relates $hashas an object $pkg(HTML :: Template class). It does not change the variable $hash.

The function blessreturns a link to a blissful anonymous hash. $selftherefore, it becomes a reference to the blessed anonymous hash (referent).

, bless , , , , :

$self = bless( $self, $pkg );

$self - undef. . :

$self = bless( undef, $pkg );

.

+1

$self - undef - :

my $self ={}; { $self = bless($self, $pkg); }

:

$self = bless( {}, $pkg);

, :

undef, Can't bless non-reference value at .... , . Perl . . perlobj manpage.

0

All Articles