Home > Archive > PERL Miscellaneous > July 2005 > tied hash
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Konrad Eisele 2005-07-27, 5:05 pm |
| 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]
| |
| Paul Lalli 2005-07-27, 5:05 pm |
| Konrad Eisele wrote:
> 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
| |
| attn.steven.kuo@gmail.com 2005-07-27, 5:05 pm |
| Konrad Eisele wrote:
> 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];
--
Hope this helps,
Steven
| |
| Anno Siegel 2005-07-28, 9:09 am |
| Konrad Eisele <konrad@gaisler.com> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
|
|
|
|
|