Tie::Alias -- Why does it bless() the original variable?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Dear Perl community,

   Recently I've been playing around with the Pure-Perl Tie::Alias module.

   What surprises me is that making an alias of a variable bless()es the or
iginal variable (if that variable is an array or hash, that is).

   Let me explain with a sample program:

# File:  tie-alias_test.pl

use strict;
use warnings;

use Tie::Alias;

sub doubleScalarValue($)
   my $scalar;
   Tie::Alias::alias $scalar => $_[0];
   $scalar *= 2;

sub doubleArrayValues(\@)
   my @array;
   Tie::Alias::alias \@array => $_[0];
   $_ *= 2  foreach @array;

sub doubleHashValues(\%)
   my %hash;
   Tie::Alias::alias \%hash => $_[0];
   $_ *= 2  foreach values %hash;

### Main code begins here: ###

my $value = 7;
my @valueArray = (1, 2, 3);
my %valueHash = (one => 1, two => 2, three => 3);

use Data::Dumper;
print "\nBEFORE:\n",
      Dumper($value, \@valueArray, \%valueHash);


print "\nAFTER:\n",
      Dumper($value, \@valueArray, \%valueHash);


This program produces the output:

$VAR1 = 7;
$VAR2 = [
$VAR3 = {
          'three' => 3,
          'one' => 1,
          'two' => 2

$VAR1 = '14';
$VAR2 = bless( [
               ], 'Tie::Alias::Array' );
$VAR3 = bless( {
                 'three' => '6',
                 'one' => '2',
                 'two' => '4'
               }, 'Tie::Alias::Hash' );

   Note that before the function calls, Data::Dumper shows \@valueArray and
 \%valueHash as regular references to an array and a hash (as you'd expect)
.  But after the function calls (where the functions use Tie::Alias to crea
te an alias to operate on), \@valueArray is now an array reference bless()e
d into a 'Tie::Alias::Array', and \%valueHash is now a hash references bles
s()ed into a 'Tie::Alias::Hash'.

   Why is that?  Am I calling Tie::Alias::alias() incorrectly?  (I notice t
hat the same problem does not happen to the $value scalar variable.)

   I'm a bit concerned that arrays and hashes are bless()ed as a side-effec
t.  I might have code later on that expects a pure array or hash reference,
 and I don't want the fact that they're now a 'Tie::Alias::Array' or 'Tie::
Alias::Hash' reference to make the code do something I didn't account for.

   Thanks for any help!

   -- Jean-Luc

Re: Tie::Alias -- Why does it bless() the original variable?

Quoted text here. Click to load it

The reason for this (as can be determined easily by looking at the code)
is that Tie::Alias::alias passes the object supposed to be aliased to
each of the TIE* constructors which use it as 'tie object' in case it
can be blessed, ie, is a reference. Since your scalar in
doubleScalarValue isn't a reference, the module creates one instead. If
it wasn't using the passed objects, it would need to create a suitable
something in order to store a reference to them.

This is a somewhat idiosyncratic design descision which should be

Site Timeline