Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

anonymous hashes as arguments in Tk

by dimmesdale (Friar)
on Jul 10, 2002 at 23:07 UTC ( #180905=perlquestion: print w/replies, xml ) Need Help??

dimmesdale has asked for the wisdom of the Perl Monks concerning the following question:

I have a Tk callback where I give it as arguments an anonymous hash, like so:

$canvas->bind($dk_img, "<Button-1>", [\&frst_clk, { deck => $deck, waste => $waste, img => $dk_img }]);

Well, after some experimentation, I realized the placement of how I get at the arguments is VERY important (i.e., if I don't shift the first arg off--an implicit Tk object, in this case a canvas--getting at the other one is a mess).

However, even after this, I cannot seem to get at the other argument. I'm getting unitialized errors, so I 'Data::Dumped' it, resulting in this:

$VAR1 = { 'deck' => bless( { 'next' => 0, 'num' => 0, 'max' => 52, 'cards' => [ bless( { 'state' => 'up', 'where' => $VAR1->{ +'deck'}, 'value' => 'j', 'suit' => 's' }, 'Card' ), ...more blessed cards... bless( { 'state' => 'down', 'where' => $VAR1->{ +'deck'}, 'value' => 5, 'suit' => 'c' }, 'Card' ) ] }, 'Pile' ), 'img' => 12, 'waste' => bless( { 'next' => 0, 'num' => 0, 'max' => 52 }, 'Pile' ) };

Well, I know I have what I want, so its a tricky matter of dereferencing, or what not. I figured it was an anonymous array, so I tried:

$_->{deck} ## to no avail, so then I tried: $_{deck} ## then: ${@_{deck}} ## and a bunch of other big and baggy monsters ...

How do I get at the data?

(incidentally, this is the result of a rewrite of some code I posted before about a solitaire program, viewable on my sketch pad...soon)

Replies are listed 'Best First'.
Re: anonymous hashes as arguments in Tk
by svad (Pilgrim) on Jul 11, 2002 at 00:08 UTC
    if you've 'shift'ed your Tk object, then you get your data as $_[0]->{deck} and alike instead of just $_->{deck}

    Otherwise show your code and I'll try to figure out what namely is wrong.

Re: anonymous hashes as arguments in Tk
by stefp (Vicar) on Jul 11, 2002 at 00:09 UTC
    my answer was bogus. Please vote for node deletion. Odd, I am downvoted since I removed the bogus solution and I posted the disclaimer, when "bogus answer" was upvoted So this node is here to say so let's try to make something out of it . How can a node with removal asked by the author itself can stay. Thanks to ichimunki, I got the answer. NodeReaper loose in the monastery says it all:
    In order for a node to be reaped it needs to have a negative reputation, 5 deletes votes from the nodes to consider and 2 less keep votes.

    I got as of today 4 keep votes, and this number cannot go down. so that node is here to say to haunt my memory and say "please think thrice before hitting submit"

    Update: tye asks at (tye)Re: why a nodelet can be kept against author wish? not to erase my mistakes but to mark them as mistakes and comment it so people can learn from them.

    So after much fuss, here is my contraption^Wcontrition: I proposed to use tied to access some data structures when there were not ties involved but mere class instances.


      Unless we timed it just right, we don't know what the bogus answer was or why it was bogus.

      Having the original answer available with an explanation of its bogosity could be quite illuminating, or at least entertaining on the cheap. I'd be inclined to ++ such a node on the grounds of cluefulness...


Re: anonymous hashes as arguments in Tk
by herveus (Parson) on Jul 11, 2002 at 15:00 UTC

    Try doing Dumper(@_) before you do anything else in your routine. See exactly what is being passed. Then, you can figure out what you need to do to get to your hash.

    I don't remember the Tk details off the top of my head, and I'm too lazy to go look them up before posting...

    Are you using Games::Cards? Among other things, you will find Tk implementations of Klondike and Freecell that may have stealable^Wreusable code fragments.


Re: anonymous hashes as arguments in Tk
by dimmesdale (Friar) on Jul 11, 2002 at 17:53 UTC

    I don't know what I was thinking. The correct way is to say $_[0] instead of just $_ (i.e., $_[0]->{hash_key})

    My mistake. Thanks for the help.

    Try doing Dumper(@_) before you do anything else in your routine

    Sorry about any confusion, but the rather cryptic (because of line breaks, what not) block of text in the code tags (with $VAR1) is the text resulting from a print Dumper @_statement.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://180905]
Approved by Rex(Wrecks)
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2022-05-17 00:53 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (65 votes). Check out past polls.