tied hash

Discussion in 'Perl Misc' started by Konrad Eisele, Jul 27, 2005.

  1. 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]
     
    Konrad Eisele, Jul 27, 2005
    #1
    1. Advertising

  2. Konrad Eisele

    Paul Lalli Guest

    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
     
    Paul Lalli, Jul 27, 2005
    #2
    1. Advertising

  3. Konrad Eisele

    Guest

    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
     
    , Jul 27, 2005
    #3
  4. Konrad Eisele

    Anno Siegel Guest

    Konrad Eisele <> 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.
     
    Anno Siegel, Jul 28, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    538
    red floyd
    Nov 10, 2011
  2. Dan
    Replies:
    1
    Views:
    93
    Ben Morrow
    Nov 21, 2003
  3. Dan Anderson

    What is a tied hash?

    Dan Anderson, Nov 21, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    296
    Tad McClellan
    Nov 21, 2003
  4. Replies:
    14
    Views:
    250
    Tomi Häsä
    Jan 10, 2005
  5. bernd
    Replies:
    0
    Views:
    625
    bernd
    Apr 24, 2012
Loading...

Share This Page