Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: tie'ing a scalar as an array?

by ruoso (Curate)
on Jul 19, 2006 at 00:15 UTC ( [id://562166]=note: print w/replies, xml ) Need Help??


in reply to Re^2: tie'ing a scalar as an array?
in thread tie'ing a scalar as an array?

You still have tied($arr_ref) to get your scalar reference

daniel

Replies are listed 'Best First'.
Re^4: tie'ing a scalar as an array?
by rvosa (Curate) on Jul 19, 2006 at 04:54 UTC
    Indeed I do. Mmmmm, I can work with that. Thanks!
Re^4: tie'ing a scalar as an array?
by rvosa (Curate) on Jul 19, 2006 at 07:50 UTC
    This more or less does the trick:
    package TwoFaced; use strict; my $instance_counter = 0; my $instance_array = []; sub new { my $class = shift; my @array; tie @array, $class, @_; return bless \@array, $class; } sub method { print "Method call\n"; return; } sub TIEARRAY { my ( $class, @args ) = @_; my $self = $instance_counter++; return bless \$self, $class; } sub FETCH { my ( $self, $i ) = @_; return $instance_array->[ $$self ]->[$i]; } sub STORE { my ( $self, $i, $val ) = @_; if ( not $val eq 'a' ) { $instance_array->[ $$self ]->[$i] = $val; return $self; } else { die "Illegal value: $val\n"; } } sub FETCHSIZE { my ( $self ) = @_; return scalar @{ $instance_array->[ $$self ] }; } sub DESTROY { print "Contents at death:\n"; print "\t", $_, "\n" for @{ $instance_array->[ $$_[0] ] }; } package main; my $t = new TwoFaced; print "it's an array!\n" if UNIVERSAL::isa($t, 'ARRAY'); # prints "it' +s an array!"; $t->[0] = 'b'; print ${ tied @$t }, $t->[0]; # prints "0b" print $t->method; # prints "Method call" $t->[0] = 'a'; # dies "Illegal value: a" # prints "Contents at death: ..." during object destruction
    ...except the destructor is called twice. I presume this is for the @array and the tied object respectively?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://562166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found