Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Calling subroutine with \& and passing variables

by Discipulus (Monsignor)
on Oct 28, 2017 at 18:34 UTC ( #1202240=note: print w/replies, xml ) Need Help??


in reply to Calling subrutine with \& and passing variables

Hello IB2017,

this is covered in the Tk::callbacks basically you can pass any arguments you want as in: -dropcommand => [\&accept_drop, $drop, $another, $more] and they arrive in the sub accept_drop in @_ as always.

The same can be accomplished with:

[ \&subname, arg1, arg2, ..] # or [ sub { ... },arg1, arg2, ..]

Note the [..] around sub and args: if more than one element is passed Tk will expect an anonymous array with the code reference as first element and arguments following.

Also note that the contents of the [..] are evaluated by perl when the callback is created.

PS see it in action:

perl -MTk -e "tkinit->Button(-text=>'print',-command=>[sub{print join'-',@_},1, 2,3])->pack;MainLoop"

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Calling subroutine with \& and passing variables
by IB2017 (Acolyte) on Oct 29, 2017 at 01:48 UTC

    Thank you for your suggestions. But I had already tried both your solutions and they did not work. See the following - code which I slighly modified to use your proposed solution \&subname, arg1, arg2, .. . PS: the code should print in the subrutine the variable $SomeVariable.

    #!/usr/local/bin/perl -w use Tk; use Tk::DropSite; use strict; use vars qw($top $drop); $top = new MainWindow; $top->Label(-text => "The drop area:")->pack; $drop = $top->Scrolled('Listbox', -scrollbars => "osoe", )->pack; my $SomeVariable="xxx"; # The variable I want to pass to the subrutine $drop->DropSite (-dropcommand => [\&accept_drop, $drop, $SomeVariable], -droptypes => ($^O eq 'MSWin32' ? 'Win32' : ['KDE', 'XDND', 'Sun']) ); MainLoop; sub accept_drop { my($widget, $selection, $SomeVariable) = @_; print $SomeVariable;#printing the variable passed from the main my $filename; eval { if ($^O eq 'MSWin32') { $filename = $widget->SelectionGet(-selection => $selection, 'STRING'); } else { $filename = $widget->SelectionGet(-selection => $selection, 'FILE_NAME'); } }; if (defined $filename) { $widget->insert(0, $filename); } } __END__

      Of course it works! It was the order of the passed variables that was wrong! For the sake of it, here it is the working code.

      #!/usr/local/bin/perl -w use Tk; use Tk::DropSite; use strict; use vars qw($top $drop); $top = new MainWindow; $top->Label(-text => "The drop area:")->pack; $drop = $top->Scrolled('Listbox', -scrollbars => "osoe", )->pack; my $SomeVariable="xxx"; $drop->DropSite (-dropcommand => [\&accept_drop, $drop, $SomeVariable], -droptypes => ($^O eq 'MSWin32' ? 'Win32' : ['KDE', 'XDND', 'Sun']) ); MainLoop; sub accept_drop { my ($drop, $SomeVariable, $selection) = @_; print "This is my variable passed from the main: $SomeVariable\n"; my $filename; eval { if ($^O eq 'MSWin32') { $filename = $drop->SelectionGet(-selection => $selection, 'STRING'); } else { $filename = $drop->SelectionGet(-selection => $selection, 'FILE_NAME'); } }; if (defined $filename) { $drop->insert(0, $filename); } } __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1202240]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-12-18 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (495 votes). Check out past polls.

    Notices?