in reply to Loop stops at first iteration

You're using glob in scalar context without fetching all the results from it.

By the way, your regex is equivalent to /^\./. You're skipping everything that starts with a .. You want /^\.\.?\z/.

Replies are listed 'Best First'.
Re^2: Loop stops at first iteration
by props (Hermit) on Dec 28, 2007 at 17:45 UTC
    let's unfold my brain thoughts: i did some minor changes from previous code:
    foreach my $folder (@folders) { # for instance folder1 next if ($folder eq "."); next if ($folder eq ".."); my $fqname = catdir( $output, $folder ); #$fqname is /mnt/music/output/folder1 if ( -d $fqname ) { #passed the test if (glob("$fqname/*.cue")) { # yes there is /mnt/music/found/output/folder1/a.cue print "$fqname: cue found\n"; #prints: /mnt/music/found/output/folder1/: cue found my ($cue) = <$fqname/*.cue>; # my ($cue) grabs /mnt/music/output/folder1/a.cue } elsif(!glob(("fqname/*.cue"))) { print "$fqname: no cue found\n"; next; } } }
    I understand that every loop grabs one element in the @folder array and executes the subroutine. Each iteration is unique and it doesn't have to remember the previous executions. my ($cue) = <$fqname/*.cue>; gets a different cue file on every iteration. Now if it has to do with the context of glob and I push @cues , ($cue) i will have an array of cue files in @cues. So how i will be helped by this approach?
      In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.
      for (1..4) { if (glob($0)) { print("I exist\n"); } else { print("I don't exist\n"); } }

      outputs

      I exist I don't exist I exist I don't exist

      if (glob(...)) is wrong. The only time glob should be in scalar context is in while (glob(...)), and the loop better not execute last, return or die.

      Fix:

      for (1..4) { if (() = glob($0)) { print("I exist\n"); } else { print("I don't exist\n"); } }

      or

      for (1..4) { if (-e $0) { print("I exist\n"); } else { print("I don't exist\n"); } }
      I exist I exist I exist I exist