Harry Putnam <[EMAIL PROTECTED]> wrote:
: 
: "Charles K. Clarkson" <[EMAIL PROTECTED]> writes:
: > Harry Putnam <[EMAIL PROTECTED]> wrote:
: 
: [...]
: 
: Wants to exit a while loop  inside a File::Find \&wanted
: sub routine. Is exiting the while loop sufficient.. or
: does one need to exit from the current file being offered
: by `sub find()'
: 
: > : So using the `next LABEL' technique how do I
: > : designate the wanted() subroutine as target?
: > : Something like this:
: > : 
: > : sub LABEL: wanted {
: >
: >     I haven't tested it, but I would think this would fail.
: >
: > :   open(FILE,"<File::Find::name");
: > :   while (<FILE>){
: > :     if(something) {
: > :         then do something
: > :     }else{
: > :        next LABEL;
: > :     }
: > :   }
: > : }
: >
: >     As I understand this, you want to immediately open
: > a new file found in current file with the current while
: > loop. Is that correct?
: 
: Yes...
: 
: >     First, we need to know a few things.
: >
: >     Are you wanting to recursively call the entire
: > wanted() subroutine?
: 
: I guess not no.  I just want to go on to the next file
: it has found

    Wait a minute! Hold on there Bucko!

    When I first read your response I thought you wanted
to open a new file you found on your own, not one that
File::Find would find itself. If that is all you want then
you need only return from the subroutine. No LABEL needed.

sub wanted {
    if( /^\d+$/ ) {

        # always check for success on open
        open FILE, $File::Find::name or
                die qq(Cannot open "$File::Find::name": $!);

        while( <FILE> ) {
            if( something() ){
                # do something

            } else {

                # close FILE and exit sub
                close FILE;
                return;
            }
        }
    }
    close FILE;
    return;
}



    File::Find calls wanted() every time it finds a file.
Each time it finds a file it resets $File::Find::name
and a bunch of other variables. There is no need for you
to do anything extra to make it work again and again.

    It would be wise not to clobber another file opened
as FILE. To do that you would use something like this:

sub wanted {
    # don't clobber open FILE
    local *FILE;

    if( /^\d+$/ ) {

        # always check for success on open
        open FILE, $File::Find::name or
                die qq(Cannot open "$File::Find::name": $!);

        while( <FILE> ) {
            if( something() ){
                # do something

            } else {
                # exit sub, FILE closes automatically
                return;
            }
        }
    }
    # exit sub, FILE closes automatically
    return;
}


    Or in perl 5.6.1 or later:

sub wanted {

    if( /^\d+$/ ) {

        # always check for success on open
        open my $fh, $File::Find::name or
                die qq(Cannot open "$File::Find::name": $!);

        while( <$fh> ) {
            if( something() ){
                # do something

            } else {
                # exit sub, $fh closes automatically
                return;
            }
        }
    }
    # exit sub, $fh closes automatically
    return;
}


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328












-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to