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

Dear Monks,

I have some code as follows.
opendir(DIR_C,"$input_directory") or die $!; while(my $job_C=readdir(DIR_C)){ next unless $job_C =~ /$tag\.txt$/; my $job_out; my $job_new_name; my $job_out_complete; my $job_out_complete_B; if ($job_C =~ /(.{36})\.txt$/) { Check_file_name_conditions ($job_C, $node); + next unless ($do_I_pick_up_request == 'Y' && $When_process_ +request== 'Now');
I have a couple of questions relating to this:

Is the last next unless statement used correctly? I want it to go to the next iteration in the while loop.

Am I right in assuming that $do_I_pick_up_request and $When_process_request will have been picked up when returned from the Check_file_name_conditions sub using  return ($do_I_pick_up_request, $When_process_request)?

Replies are listed 'Best First'.
Re: Use of next unless and subs
by davido (Cardinal) on Jan 31, 2007 at 18:18 UTC

    if(){} statements aren't considered loop blocks, and next isn't affected by your being within an if(){} statement. It will still jump to the next loop iteration.

    However, it may simply be less ambiguous to code readers / maintainers if you explicitly specify where to next to, using a label on your while loop:

    LOOP_NAME: while( ... ) { .... next LOOP_NAME unless .....; }

    Dave

Re: Use of next unless and subs
by educated_foo (Vicar) on Jan 31, 2007 at 18:15 UTC
    That seems to be correct use of next. However, $do_I_pick_up_request == 'Y' is actually testing whether $do_I_pick_up_request equals zero, so you probably want eq.
Re: Use of next unless and subs
by Herkum (Parson) on Jan 31, 2007 at 18:25 UTC

    Sometimes I look at your posts and wonder, do you even try and test your code? You have been here 3 years, you have to have learned something! Make a simple example like this.

    #!/usr/bin/perl -w use strict; for my $no (1..10) { print "No. $no \n"; next unless $no < 5; print "\t is less than 5\n"; }

    You would have an answer more quickly than the time you spent writing your post.

Re: Use of next unless and subs
by shmem (Chancellor) on Jan 31, 2007 at 18:20 UTC
    Is the last next unless statement used correctly? I want it to go to the next iteration in the while loop.

    Syntactically, yes.

    Am I right in assuming that $do_I_pick_up_request and $When_process_request will have been picked up when returned from the Check_file_name_conditions sub using return ($do_I_pick_up_request, $When_process_request)?

    You need the following for that:

    my ($do_I_pick_up_request, $When_process_request) = Check_file_name_co +nditions ($job_C, $node);

    For your variable checking, use 'eq'. '==' is for numeric comparison.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Use of next unless and subs
by chakram88 (Pilgrim) on Jan 31, 2007 at 18:28 UTC
    Am I reading you correctly that in your code you have a subroutine Check_file_name_conditions which returns the two values $do_I_pick_up_request, $When_process_request?

    If that's the case, when you call the subroutine you'll want to capture those return values into a var in your current scope to use them.

    my ($do, $when) = Check_file_name_conditions($job_C, $node); next unless ($do eq 'Y' && $when eq 'Now');