in reply to File::Find exit

You exit a sub through last? I'm shocked. I'm even more shocked nobody here makes even bothers to make a remark about it.

You should never ever leave a sub through last or next. I'd prefer it if Perl considered this a fatal error, either at compile time (like an "else" without "if"), or at runtime.

Replies are listed 'Best First'.
Re: Re: File::Find exit
by eric256 (Parson) on Oct 29, 2003 at 18:09 UTC

    Valid point but you failed to provide a correct way for leaving the sub. I'm no expert but i leave subs using return. Also I think that his code was psuedo code meant to express his desired outcome rather than actual code in need of work.

    ___________
    Eric Hodges
      The proper way is using return. So you are doing it the proper way.
Re: Re: File::Find exit
by rir (Vicar) on Oct 29, 2003 at 18:55 UTC
    This is a runtime error in Perl 5.6.1 at least.
    perl -e "sub x{last} x" Can't "last" outside a loop block at -e line 1.
    Update: That was naive. See Bart's followup which corrects/expands this.
      Eh... not always.
      #!/perl/bin/perl -wl sub x { last } for(1 .. 3) { print; x(); } print "done";
      Result:
      Exiting subroutine via last at test.pl line 2.
      1
      done
      
      Here, it's a warning, not a fatal error.
Re: Re: File::Find exit
by rir (Vicar) on Oct 29, 2003 at 21:54 UTC
    Then you may like to make it a runtime error:
    #!/usr/bin/perl -l use warnings; use strict; use warnings FATAL => qw/exiting/; # make it a runtime error sub xxx { next } no warnings; for ( 1 ... 3 ) { print; xxx; } print "done";
    So little time so much perldoc.

    (Thanks bart.)