For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > August 2004 > passing hashes to functions









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]

 

Author passing hashes to functions
Ananth Chellappa

2004-08-03, 8:56 pm

More a perl question than a perl Tk question.

I tried
-command => [\&display, $filename, $namelength, %fet_choices]

with a widget; and the "display" function gets its arguments as :

my ($filename, $namelen, %fet_hash_ref) = @_;

but this doesn't work. There are no errors, but the "values" of the
local hash - within the function - are always zero. Any idea why?

This did work for me :

for the widget :
-command => [\&display, $filename, $namelength, \%fet_choices]

and "display" :

sub display(){
my ($filename, $namelen, $fet_hash_ref) = @_;



Using a reference worked without problems. Anyone with insight into
this, do educate me.

Thanks,
Ananth

Marc Dashevsky

2004-08-03, 8:56 pm

In article <410FF993.3010000@prism.gatech.edu>, gtg563c@prism.gatech.edu says...
> More a perl question than a perl Tk question.
>
> I tried
> -command => [\&display, $filename, $namelength, %fet_choices]
>
> with a widget; and the "display" function gets its arguments as :
>
> my ($filename, $namelen, %fet_hash_ref) = @_;
>
> but this doesn't work. There are no errors, but the "values" of the
> local hash - within the function - are always zero. Any idea why?


Because %fet_choices is evaluated at the time the widget's
-command option is being evaluated, and the hash is probably
empty at that time. The following delays %fet_choices
evaluation until the -command option is invoked:

-command => sub{display($filename, $namelength, %fet_choices)}

> This did work for me :
>
> for the widget :
> -command => [\&display, $filename, $namelength, \%fet_choices]
>
> and "display" :
>
> sub display(){
> my ($filename, $namelen, $fet_hash_ref) = @_;
>
> Using a reference worked without problems. Anyone with insight into
> this, do educate me.


A reference to a hash is the same regardless of the hash's
contents. Each time display() is called, the current contents
of the hash are available via the reference.

--
Go to http://MarcDashevsky.com to send me e-mail.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com