What does "exit the block or routine with a loop control operator" mean?

Discussion in 'Perl Misc' started by Suresh Govindachar, Jul 24, 2004.

  1. Hello,

    Page 789 of Programming Perl says that the block or
    routine used for sorting cannot be exited "with a loop
    control operator". What does this mean?

    Specifically, is the following OK or not-OK?

    sub the_sorter
    {
    my $rv = 0;
    for my $i(1..10)
    {
    # some code involving $a and $b
    # an assignment to $rv

    $rv and last; # exitting the loop inside a sort subroutine.
    }
    return $rv; # explicit return statement; not a loop-control operator
    }

    I suspect the preceding is indeed OK; please give an
    example of exitting a "block or routine with a loop control operator".

    Thanks,

    --Suresh
    Suresh Govindachar, Jul 24, 2004
    #1
    1. Advertising

  2. Suresh Govindachar

    Bob Walton Guest

    Suresh Govindachar wrote:

    ....

    > I suspect the preceding is indeed OK; please give an
    > example of exitting a "block or routine with a loop control operator".

    ....


    > --Suresh



    They probably mean something like:


    use warnings;
    use strict;
    sub test{
    print "in test\n";
    last; #terminates the for loop below
    print "still in test\n";
    }
    for(1..3){
    print "calling test $_\n";
    test();
    print "returned from test $_\n";
    }

    That gives a warning, though, at least in 5.8.4.

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Jul 24, 2004
    #2
    1. Advertising

  3. In article <8xkMc.3927$>,
    "Suresh Govindachar" <> wrote:

    > Hello,
    >
    > Page 789 of Programming Perl says that the block or
    > routine used for sorting cannot be exited "with a loop
    > control operator". What does this mean?


    They are referring to exiting the direct block or subroutine you provide
    to the sort operator. Not to a sub-block of this block.

    IE, something like this is probably forbidden

    sort {
    my $a_processed = do_something($a);
    my $b_processed = do_something($b);

    if (!defined($a_processed) or !defined($b_processed)) {
    next; # illegal!!!!
    }

    return $a_processed <=> $b_processed
    || $a_processed cmp $b_processed;
    } @vals_to_sort;

    HTH,
    Ricky
    Richard Morse, Jul 26, 2004
    #3
  4. "Suresh Govindachar" <> writes:

    > Page 789 of Programming Perl says that the block or
    > routine used for sorting cannot be exited "with a loop
    > control operator". What does this mean?
    >
    > Specifically, is the following OK or not-OK?
    >
    > sub the_sorter
    > {
    > my $rv = 0;
    > for my $i(1..10)
    > {
    > # some code involving $a and $b
    > # an assignment to $rv
    >
    > $rv and last; # exitting the loop inside a sort subroutine.
    > }
    > return $rv; # explicit return statement; not a loop-control operator
    > }
    >
    > I suspect the preceding is indeed OK;


    It is just fine.

    > please give an
    > example of exitting a "block or routine with a loop control
    > operator".


    OK first without sort. One can use last to implement a throw/catch
    type of semantic rather as you would with eval/die.

    sub baz {
    no warnings 'exiting';
    print "last FOO\n";
    last FOO;
    }

    sub bar {
    baz();
    print "Not reached\n";
    }

    FOO: {
    bar();
    print "Not reached\n";
    }
    print "Reached\n";

    __END__

    Now if instead I replace bar with

    sub bar {
    print sort baz 1,2,3;
    }

    then we get an error: Label not found for "last FOO".

    IMNSHO this is a bad thing.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Jul 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pratip Mukherjee

    Help: what does this VHDL code mean?

    Pratip Mukherjee, Jun 22, 2005, in forum: VHDL
    Replies:
    16
    Views:
    1,257
    Kim Enkovaara
    Jun 27, 2005
  2. methi
    Replies:
    1
    Views:
    821
    info_
    Jun 23, 2005
  3. Li Ma
    Replies:
    1
    Views:
    2,262
    Roedy Green
    Mar 9, 2009
  4. Rahul
    Replies:
    4
    Views:
    547
    Robert Kern
    Apr 7, 2009
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,178
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page