For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2004 > `next LABEL' usage









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author `next LABEL' usage
Harry Putnam

2004-03-29, 2:36 pm


I'm using a next LABEL inside a File::Find
sub wanted {...} loop

It is further buried in a while loop inside the `sub wanted()'

The while loop is while (<FILE> ) on the most recent found file. I
want this `next LABEL' to bring on a new file... not a new line in
while loop.

So using the `next LABEL' technique how do I designate the wanted()
subroutine as target? Something like this:

sub LABEL: wanted {
open(FILE,"<File::Find::name");
while (<FILE> ){
if(something) {
then do something
}else{
next LABEL;
}
}
}
Charles K. Clarkson

2004-03-29, 3:33 pm

Harry Putnam <reader@newsguy.com> wrote:
:
: I'm using a next LABEL inside a File::Find
: sub wanted {...} loop
:
: It is further buried in a while loop inside the
: `sub wanted()'
:
: The while loop is while (<FILE> ) on the most recent
: found file. I want this `next LABEL' to bring on a
: new file... not a new line in while loop.
:
: 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?

First, we need to know a few things.

Are you wanting to recursively call the entire
wanted() subroutine?

Or do you just want to call the while loop portion
only and continue with the execution after?

Is it alright to clobber the currently open file or
do we need to continue processing it afterward?

Is there any test needed to be certain we are not
opening the same file again?


HTH,

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




Harry Putnam

2004-03-29, 3:33 pm

"Charles K. Clarkson" <cclarkson@htcomp.net> writes:
> Harry Putnam <reader@newsguy.com> 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

> Or do you just want to call the while loop portion
> only and continue with the execution after?


Yes

> Is it alright to clobber the currently open file or
> do we need to continue processing it afterward?


It needs to be clobbered. This file has been processed to a point
where it has already failed tests that show if this file has what we
want.

> Is there any test needed to be certain we are not
> opening the same file again?


I don't think so, but not really sure of the internals of File::Find.
I was assuming if I threw down the file it hands me. It would hand
me a new different one.

The files offered by `wanted()' are filtered thru an `if' clause

I left this fact out by accident.
sub wanted {
if(/^\d+$/){.processing..
open(FILE),"$File::Find::name";
while(<FILE>{
if(something){
do something
}else{
next LABEL;
}
}
}
}

Looking at this simplified diagram... I think I may have answered my
own question..
I want to exit to just above the first `if' filter. I guess just
exiting the while loop will do that.
so: LABEL: while(<FILE> ){ processing}

is sufficient?

Charles K. Clarkson

2004-03-29, 5:31 pm

Harry Putnam <reader@newsguy.com> wrote:
:
: "Charles K. Clarkson" <cclarkson@htcomp.net> writes:
: > Harry Putnam <reader@newsguy.com> 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











Harry Putnam

2004-03-29, 9:31 pm

"Charles K. Clarkson" <cclarkson@htcomp.net> writes:

> HTH,


Definitely and thanks for the examples. I think I was making this
more complicated that it needed to be. It's slowly sinking in what
all a `return' can do.
R. Joseph Newton

2004-03-29, 11:30 pm

Harry Putnam wrote:

> "Charles K. Clarkson" <cclarkson@htcomp.net> writes:
>
>
> Definitely and thanks for the examples. I think I was making this
> more complicated that it needed to be. It's slowly sinking in what
> all a `return' can do.


Hi Harry,

Glad Charles got you squared away. I have to say it again, though--you will
make much more progress by focusing on a plain-language description of what you
are trying to accomplish, than by thinking in code. Or, if you must express
your ideas directly in code, run them in the command line compiler first, to get
the immediately available sanity check, before posting.

The problem with saying "I want to do something like this..." then showing
guesswork code, is that the code you showed already does whatever it does, or
nothing at all if sytax errors preclude compilation. How do we know, by looking
at code alone, whether the effect, if any, achieved by the code, is the effect
desired? Did you notice how quickly the issue resolved once Charles figured out
what you wanted--in words?

Joseph

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com