Brandon McCaig wrote:
On Sun, Jun 23, 2013 at 12:02:38PM +0300, Shlomi Fish wrote:
"last" is not a function (a.k.a "subroutine") - it cannot be.
It is a special statement which is handled in a special way by
the Perl interpreter. "redo" and "next" are not functions
either for a similar reason.

I think we can all agree that last is not a function/subroutine,
but at the same time it does behave like one in several ways. :)

No, it doesn't.


For example, you can follow it with parenthesis if you want
(albeit, there's no good reason to do that).

#!/usr/bin/perl

use strict;
use warnings;

while(1) {
     last();
}

The parentheses in this case do nothing, and they certainly don't imply that last is a function.

$ perl -le'
use warnings;
use strict;
for (1..2) {
    ();
    }
'
$


__END__

Similarly, you can even wrap it up in a subroutine and it still
works as intended (so obviously it understands just how to find
the right enclosing block even through subroutine calls).

#!/usr/bin/perl

use strict;
use warnings;

while(1) {
     my_last;
}

sub my_last {
     last;
}

That won't work:

$ perl -le'
use warnings;
use strict;
while () {
    my_last;
    }
sub my_last {
    last;
    }
'
Bareword "my_last" not allowed while "strict subs" in use at -e line 5.
Execution of -e aborted due to compilation errors.


And even if you fix that bareword you still get a warning:

$ perl -le'
use warnings;
use strict;
while () {
    my_last();
    }
sub my_last {
    last;
    }
'
Exiting subroutine via last at -e line 8.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to