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

This code causes a syntax error:
#!/usr/bin/perl use strict; grep { NUM: for(0 .. $_) { last NUM if $_ > 3; } } 1 .. 10;
syntax error at ./test.pl line 4, near "NUM:" syntax error at ./test.pl line 8, near "}" Execution of ./test.pl aborted due to compilation errors.
One fix is to put something in front of the label:
#!/usr/bin/perl use strict; grep { 1; NUM: for(0 .. $_) { last NUM if $_ > 3; } } 1 .. 10;
Is there any way to start the block with a label?

Replies are listed 'Best First'.
Re: Labels at beginning of a block
by jwkrahn (Abbot) on Aug 19, 2011 at 19:56 UTC

    The label goes before the block, which you can't do with a grep (or map) block, and besides, you are using grep in void context which makes no sense in your example.    You probably need something like this:

    NUM: for my $i ( 1 .. 10 ) { for ( 0 .. $i ) { last NUM if $_ > 3; } }
Re: Labels at beginning of a block
by Anonymous Monk on Aug 20, 2011 at 03:03 UTC
    perl v5.14.1 has no problem with the syntax