Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

what is difference between calling the function in Perl/Tk in the following ways

by kiruthika.bkite (Scribe)
on Apr 15, 2010 at 04:04 UTC ( #834824=perlquestion: print w/ replies, xml ) Need Help??
kiruthika.bkite has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am learning Perl/Tk.
And I am developing one interface in Tk.
In this while clicking one button I want to call one function to perform operations.
And we can call the function in the following two ways.
(-command => [\&function,\$arg1,\$arg2]); (-command=>sub {&function($arg1,$arg2)});

Any one can explain me what is the difference between in the above two ways.

Thanks in advance.

Comment on what is difference between calling the function in Perl/Tk in the following ways
Download Code
Re: what is difference between calling the function in Perl/Tk in the following ways
by ikegami (Pope) on Apr 15, 2010 at 04:18 UTC

    Any one can explain me what is the difference between in the above two ways.

    In the 1st, you pass a code ref and args to pass to the code ref.

    In the 2nd, you pass a code ref but no args. Instead, you rely on a closure to pass data.

    I presume you're really asking which one you should use. Whichever you prefer. The latter can be simpler and more natural in Perl, but the former is required for other languages where the latter is impossible to do.

Re: what is difference between calling the function in Perl/Tk in the following ways
by runrig (Abbot) on Apr 15, 2010 at 06:37 UTC
    In the first one, you have to dereference the args in the function. In the second one, you don't. You'll have to write the function accordingly depending on which way you do it. It doesn't really matter which way you do it. I like to do it the second way, especially if I at all think I might reuse the function outside of Tk. Some people make the mistake of doing:
    (-command => [\&function, $arg1, $arg2]);
    Which you seem to have recently done.
Re: what is difference between calling the function in Perl/Tk in the following ways
by Sinistral (Prior) on Apr 15, 2010 at 12:36 UTC

    As a pieced of additional information, if you're learning Perl/Tk, I suggest you run, do not walk, over to the TkDocs Site and peruse their Tk Tutorial. Because Tk has been around so long, there are many ways to generate interfaces. Many of these ways have been supplanted by newer, better ways that better fit with the underlying operating system user interface guidelines for your particular platform.

    The Tk Tutorial is current, up to date, and as a bonus gives you direct comparisons with Tcl, Perl, Python, and Ruby.

Re: what is difference between calling the function in Perl/Tk in the following ways
by choroba (Abbot) on Apr 15, 2010 at 12:45 UTC
    A colleague of mine recomends the first approach. The second one sometimes leads to memory leaks, if the button itself is passed as one of the parameters to the sub.
      I'd expect a memory link from code like:
      my $button; $button = $frame->Button(... , -command => sub { SomeFunction($button) + });
      But it should only matter if the button is created more than once (and I'd only worry about that if it's created many times). On a single-window app where you only create widgets once, it really doesn't matter. If it does matter, then you can probably use Scalar::Util::weaken() to get around it (but by then I'd agree that it's probably easier to use the first method).
      I don't see how one could leak and not the other. You could always change
      (-command=>sub {&function($arg1,$arg2)});
      to
      (-command=>[sub {&function($arg1,$arg2)}]);
      but I suspect that's already happening internally.
        Both these two would leak, I fear. The only safe way is to call
        -command=>[\&function,\$arg1,\$arg2]
        It's the circular reference preventing garbage collection that would cause the leak. The same sort of leak that happens when you repeatedly create anonymous recursive subs like:
        my $fact; $fact = sub { my $n = shift; return $n if $n <= 1; return $n*fact($n-1); }
        Hmm, you're right, the other one would probably leak also if they both contain $button. (update: oops...looking at wrong thread since the OP didn't even have a circular reference in the example).
Re: what is difference between calling the function in Perl/Tk in the following ways
by Marshall (Prior) on Apr 16, 2010 at 13:16 UTC
    I don't use either one of the above.

    For a simple command with no args to the sub, I often use:
    -command => \&some_sub_name

    For a command with args to a sub, I often use:
    -command => sub { some_sub_name ($arg1, $arg2) }

    The previous is an anon sub that calls some_sub_name(...)

    There is no need for the '&' below and I would recommend against it.
    -command=>sub { &some_sub_name($arg1,$arg2) }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://834824]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-04-19 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls