Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

next within one subroutine that looks to another subroutine

by c (Hermit)
on Feb 13, 2002 at 18:53 UTC ( [id://145245]=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

i am looking at a piece of code that can be simplified to the following...

#!/usr/bin/perl -w use strict; my @list = qw(Charles Cody Gina); &action; sub action { for my $i(@list) { &decide($i); } } sub decide { my $i = shift; next if (/^C/); print "$i\n"; }

the next if statement causes the script to spit out an error that it "Exiting subroutine via next at Script line 14". i can see where this error would come up, since, according to the subroutine, there is no list, just a single value for $i that is being passed to it.
is there a method of maintaining my structure and would allow me to exit the subroutine for the specific value within the list? sorry that this example is simplified rather than providing the entire code snippet. rather that seeking a rewrite to solve my code issue, i am more interested in finding out if it is possible to drop out of a sub routine's action without exiting the script entirely.

thanks! -c

Replies are listed 'Best First'.
Re: next within one subroutine that looks to another subroutine
by dragonchild (Archbishop) on Feb 13, 2002 at 19:40 UTC
    sub action { for my $i (@list) { my $rc = decide($i); next unless $rc; # Do stuff here with $rc } } sub decide { my $i = shift; return 0 if $i =~ /^C/; print $i, $/; }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: next within one subroutine that looks to another subroutine
by ehdonhon (Curate) on Feb 13, 2002 at 19:33 UTC

    I suspect what you are looking for is really something that passes this:

    sub action { for my $i(@list) { &decide($i); print "Decide sub didn't call next!\n"; } }

    While I hate the idea of using labels, you could use them here:

    sub foo { next I; }; I: foreach my $a ( 1..5 ) { print "$a\n"; &foo(); print "$a\n"; }
    Output:
    
    1
    2
    3
    4
    5
    

    Your other option (better style IMHO) would be to evaluate the return code of decide in your action sub and call next based on that.

Re: next within one subroutine that looks to another subroutine
by trs80 (Priest) on Feb 13, 2002 at 20:44 UTC
    One possible method would be:
    #!/usr/bin/perl -w use strict; my @list = qw(Charles Cody Gina); # pass the array to the sub to avoid value reuse. &action(@list); sub action { my @list = @_; for my $i(@list) { # tests the return value of decide and only prints # if it is true (1) print "$i\n" if decide($i); } } sub decide { my $i = shift; # returns 1 if it is a match otherwise nothing # is returned. return 1 if $i =~ m/^C/; # you could add an explicit return 0 if you want # return 0; }

    If your conditional is more complex then the example you would want to most likely put the if before the print and then use an else condition for a failed decide.
Re: next within one subroutine that looks to another subroutine
by dreadpiratepeter (Priest) on Feb 13, 2002 at 18:55 UTC
    maybe I don't understand the question, but don't you just want:  return if /^C/;

    -pete
    Entropy is not what is used to be.
Re: next within one subroutine that looks to another subroutine
by Rich36 (Chaplain) on Feb 13, 2002 at 19:07 UTC
    Keywords (not functions... thanks dragonchild)</s> like next and last and used in the context of loops. next goes to the next iteration of the loop, last leaves the loop.
    foreach(1,2,3) { next if ($_ == 2); print "next: $_\n"; } foreach(1,2,3) { last if ($_ == 2); print "last: $_\n"; } __RESULTS__ next: 1 next: 3 last: 1

    Rich36
    There's more than one way to screw it up...

      <DIRECTED RANT>
      1. next and last are keywords, not functions. You cannot redefine their behavior. If they were functions, you'd be able to do so.
      2. The question was concerning the capabilities of next and last when the scope had changed, especially concerning nested function calls. I would assume that, given the nature of the question, that the questioner knew how to use next and last.
      </DIRECTED RANT>

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://145245]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found