[OT] Should exist / does exist?

dakkar dakkar at thenautilus.net
Wed Dec 6 21:00:17 GMT 2006


Andy Armstrong wrote:
> In other words it'd be nice if the callback closure had something
> approaching normal loop semantics: next, redo and last would DWIM.

They do. They raise a warning, but they work.

 #!/usr/bin/perl
 use strict;
 use warnings;

 sub scan {
     my $sub=shift;
     for my $t (@_) {
         $sub->($t);
     }
 }

 my @a=(1,2,3,4,5);
 my $done;
 scan(sub {
          if ($_[0]==1) {
              print "1\n";
              next;
          }
          if ($_[0]==2 && !$done) {
              print "2\n";
              $done=1;
              redo;
          }
          if ($_[0]==2 && $done) {
              print "2!\n";
              last;
          }
          print $_[0],"\n";
 }, at a);

produces:

 1
 Exiting subroutine via next at /tmp/t.pl line 17.
 2
 Exiting subroutine via redo at /tmp/t.pl line 22.
 2!
 Exiting subroutine via last at /tmp/t.pl line 26.

-- 
	Dakkar - <Mobilis in mobile>
	GPG public key fingerprint = A071 E618 DD2C 5901 9574
	                             6FE2 40EA 9883 7519 3F88
	                    key id = 0x75193F88


More information about the london.pm mailing list