tied hash

K

Konrad Eisele

I have a tied hash, say %a. On a tied 'STORE' I want to get the return
values of the STORE function. is that possible? a naive aproach would be
@a = ($a{'key'} = 'value'); however this returns still the tied FETCH
of $a{'key'}. How do i have to reorder the expression??

-- Konrad




%a = ();
tie %a,'Ta';
@a = ($a{'key'} = 'value');


package Ta;
use Tie::Hash;
@ISA = qw(Tie::StdHash);

sub STORE {
return [1,2,3];
}




I'd like to get @a == [1,2,3]
 
P

Paul Lalli

Konrad said:
I have a tied hash, say %a. On a tied 'STORE' I want to get the return
values of the STORE function. is that possible? a naive aproach would be
@a = ($a{'key'} = 'value'); however this returns still the tied FETCH
of $a{'key'}. How do i have to reorder the expression??

-- Konrad

%a = ();
tie %a,'Ta';
@a = ($a{'key'} = 'value');


package Ta;
use Tie::Hash;
@ISA = qw(Tie::StdHash);

sub STORE {
return [1,2,3];
}

I'd like to get @a == [1,2,3]
From perldoc perltie:
Don't worry about returning a value from STORE -- the semantic of
assignment returning the assigned value is implemented with FETCH

It sounds to me like what you're asking for isn't possible. Assignment
is defined to return the value of FETCH'ing, not STORE'ing.

It might be a good idea at this point to ask yourself why you feel the
need to do this in the first place. Perhaps if you tell us what your
end goal is, someone can suggest a better method than attempting to
return a value from STORE()

Paul Lalli
 
A

attn.steven.kuo

Konrad said:
I have a tied hash, say %a. On a tied 'STORE' I want to get the return
values of the STORE function. is that possible? a naive aproach would be
@a = ($a{'key'} = 'value'); however this returns still the tied FETCH
of $a{'key'}. How do i have to reorder the expression??

-- Konrad




%a = ();
tie %a,'Ta';
@a = ($a{'key'} = 'value');


package Ta;
use Tie::Hash;
@ISA = qw(Tie::StdHash);

sub STORE {
return [1,2,3];
}




I'd like to get @a == [1,2,3]


Couldn't you avoid the STORE operation entirely
and assign to @a first? If you want it on
one line, then try something like:

tie %foo, 'Ta';
$foo{bar} = [ my @a = ([1, 2, 3]) ]->[0];
 
A

Anno Siegel

Konrad Eisele said:
I have a tied hash, say %a. On a tied 'STORE' I want to get the return
values of the STORE function. is that possible? a naive aproach would be
@a = ($a{'key'} = 'value'); however this returns still the tied FETCH
of $a{'key'}. How do i have to reorder the expression??

-- Konrad




%a = ();
tie %a,'Ta';
@a = ($a{'key'} = 'value');

When this happens, @Ta::ISA is not yet set.
package Ta;
use Tie::Hash;
@ISA = qw(Tie::StdHash);

This happens too late. Wrap "BEGIN {}" around it.
sub STORE {
return [1,2,3];
}

Your example program doesn't use your STORE method.

But even if it did, what is returned by an assignment is always the
variable on the left hand side (with the new value already in it).
If that happens to be tied, or, in your example, happens to be a value
in a tied hash, that doesn't change that fact. The value you see will
be the value stored (as read back by READ), no matter what STORE returns.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top