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

I would see codes written like this:
$submit->bind('<1>' => sub { Submit($p1, $p2, $p3, $p4, $p5); } );
The above codes I understand, knowing how many parameters and how to intercept the params in a subroutine.



However I dont understand this code:
$yscroll=$mw->Scrollbar()->pack(-fill=>'y', -side=>'right',); $yscroll->configure( -command => [ \&ScrollAll, $yscroll, [$p1, $p2, $ +p3, $p4, $p5]]); sub ScrollAll { my ($sb, $wigs, @args) = @_; foreach my $w (@$wigs) { $w->yview(@args); } }


Can anyone explain how the params are passed when it is constructed in this manner:
-command => [ \&ScrollAll, $yscroll, [$p1, $p2, $p3, $p4, $p5]]
In the above sub's code my ($sb, $wigs, @args) = @_;
What is assigned to $sb?
What is assigned to $wigs?
Is there any other way of passing params when used with -command?
And can that like be written like:
-command => sub { subname ($p1, $p2, $p3, $p4, $p5); }
thanks

Replies are listed 'Best First'.
Re: TK param passing
by Discipulus (Canon) on Apr 24, 2018 at 07:11 UTC
    Hello jsteng,

    When you specify a callback (this is the magic word to search for) for a Tk action and this callback need arguments you need to pass an anonymous array with the function and it's arguments

    -command => [ \&subname, arg1, arg2 ... ]

    See it explained in it's own doc: Tk::callbacks

    In your example, if I understand it correctly $yscroll arrives as $b in the sub and the anonymous array [$p1, $p2, $p3, $p4, $p5] arrives as $wigs in the sub.

    Anyway, as AnonymousMonk telegraphically stated, use Data::Dump and it's dd method to dump what @_ is inside of your sub.

    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.
Re: TK param passing
by Anonymous Monk on Apr 24, 2018 at 02:08 UTC
    Try It To See?