in reply to Perl/Tk binding Tab

#!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $entry1 = $mw->Entry()->pack(); my $entry2 = $mw->Entry()->pack(); my $entry3 = $mw->Entry()->pack(); my %after = ($entry1 => $entry2, $entry2 => $entry1, ); $mw -> bind('all','<Tab>',sub{ ($after{$_[0]} // $_[0])->focus; } ); $mw->MainLoop(); exit(0);

Replies are listed 'Best First'.
Re^2: Perl/Tk binding Tab
by Anonymous Monk on Oct 31, 2018 at 17:25 UTC

    It works, thank you. Can you explain this. I am not sure I understand it: ($after{$_[0]} // $_[0])->focus;

      // is the defined-or operator. If the left part is undefined, take the right part else take the left part.

      my $c = $a // $b;

      Is like

      my $c = defined $a ? $a : $b;

      but easier and chainable

      my $x = $a // function ($b) // $ENV{FOO} // $self->bar // 0;

      Enjoy, Have FUN! H.Merijn
      Hi, it may help to know that the calling widget is the first argument $_[0] pass to any Tk callback. This code may show you what is happening. Notice how the Entry widget changes with each Tab.
      #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $entry1 = $mw->Entry()->pack(); my $entry2 = $mw->Entry()->pack(); my $entry3 = $mw->Entry()->pack(); my %after = ($entry1 => $entry2, $entry2 => $entry1, ); $mw -> bind('all','<Tab>',sub{ print "@_\n"; ($after{$_[0]} // $_[0])->focus; } ); $mw->MainLoop(); exit(0);

      I'm not really a human, but I play one on earth. ..... an animated JAPH