In perl, when you say
Perl is very nice and assigns it a very unique value - undef. The "value" undef is kind of strange, it is the sound of one hand clapping ( or my jaws flapping, if prefer ).my $foo;
When you said
you created an anonymous array with exactly one element, and the "value" if that element was undef. When you try to print undef ( especially when not using -w ;/ ), perl prints nothing.foreach ( $foo ) { print "$_\n"; }
When you say
Perl is very kind and creates and empty array. This is very different from an array whose only element is undef. So when you saidmy @foo;
perl tested @foo, found no elements and passed on. It is even possible the branch was compiled out - ie, the interpreter never even saw it.foreach ( @foo ) { print "array[x] = $_\n"; }
When you then said
You created an array with one element, and the value of that element was the empty string. This is very different from creating an element whose value was undef. This form will not generate a warning, had you been using -w ;/ Thus, when you went to print, you printed an empty string which does, coincidentally look an awful lot like printing undef. Unless one is using -w ;/my @array $array[0] = "";
Of course, your last example was nothing but an extension of the previous and I have covered that. Except I feel the overwhelming urge to mention you didn't use -w ;/
The emptry string is not the same as undef. An empty array is not the same as an array whose first element is either the empty string or undef. But most of all, my fellow monk, use -w. This is exactly the kind of pain -w was intended to stop. Because one day you will type $from when you meant $form and things won't work. -w is the only thing between you and possibly days of fruitless bug chasing.
The correct invocation for perl is
Trust me. mikfire#!/path/to/your/perl -w use strict;
In reply to RE: While Vs. Foreach
by mikfire
in thread While Vs. Foreach
by matthew
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |