in reply to What's the point of a labeled block without a loop?

Some folks like to use labeled blocks as an odd form of commenting. However, if you look at how Test::More implements SKIP blocks, you'll be rather horrified at the how it works :) (if the SKIP value is true, there's a last SKIP call).

sub skip { my($why, $how_many) = @_; my $tb = Test::More->builder; unless( defined $how_many ) { # $how_many can only be avoided when no_plan is in use. _carp "skip() needs to know \$how_many tests are in the block" unless $tb->has_plan eq 'no_plan'; $how_many = 1; } for( 1..$how_many ) { $tb->skip($why); } local $^W = 0; last SKIP; }

That relies on the SKIP label in the test, even though you don't see the last call.

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re^2: What's the point of a labeled block without a loop?
by JohnMG (Beadle) on Dec 18, 2005 at 01:49 UTC

    Egads.

    Although I haven't looked inside Test::More, I think I see what you're talking about. I tried this:

    #!/usr/bin/perl use warnings; use strict; sub tear_us_outta_here { print "Getting out of loop labeled FOO.\n"; last FOO; } FOO: for ( 1 .. 10 ) { print "At $_.\n"; if ( $_ == 4 ) { tear_us_outta_here(); } }
    ~~~ yields ~~~
    At 1. At 2. At 3. At 4. Getting out of loop labeled FOO. Exiting subroutine via last at ./foo.pl line 8.
    That looks kinda' weird and scary to me... :)

    Heh,.. for this sort of stuff, we should have a Halloween Perl-ghost-story-hour every October:

    {dark room with flashlight shining up on an upright dead-tree printout of some code}:
    "On this very line of code, many iterations ago, for no apparent reason, the loop... abruptly ended! Some say it was a hardware failure, others suggest deep magic at work in the bowels of the interpreter..."
    ;)

    EDIT: Thanks for all the replies and interesting suggestions folks. Seems like, as Ovid suggests, the original author of the code I'm looking at just used the labels as some off form of odd commenting -- especially considering that there isn't any pod in the code.