On 9/30/05, Gergely Buday <[EMAIL PROTECTED]> wrote: > Hi, > > this is a working code snippet: > > opendir (DIR, $dir) || die "Open failed: $!\n"; > > $\ = "\n"; > while ($_ = readdir(DIR)) { print; }; > > But when I try to use > > while (readdir(DIR)) ... > > my script writes only empty lines. It seems that the proper number of > empty lines are written. Does the prepended $_ change readdir's > calling context? Or any other explanation? > > - Gergely >
Gergely, readdir's context isn't changed;it still returns in scalar context. while just doesn't automatically set by default $_ the way foreach, etc. do. Because while doesn't use a loop variable for control, you have to load $_ yourself if you want it. Your code issues the system call but then doesn't do anything with the output, and prints an undef variable. This would also be more obvious if you code were using warnings and strict, because you would see things like "use of uninitialized value in print at line n" instead of just a blank line. Consider the differences between: my @a = (1,2,3,4,5); print while @a; # prints "use of uninitialized value" in an infinite loop print while $_ = @a; # prints "5" in an infinite loop print while shift @a; # prints "use of uninitialized value" 5 times print while $_ = shift @a; # prints and undefs each item in @a and of course: print foreach @a; # prints each item in @a; leaves array intact HTH, -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!