And another mendicant friar is tripped on the difference between undef, an empty string, spaces and stuff. Please allow me to demonstrate my pale understanding of the mysteries you have encountered.

In perl, when you say

my $foo;
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 ).

When you said

foreach ( $foo ) { print "$_\n"; }
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.

When you say

my @foo;
Perl is very kind and creates and empty array. This is very different from an array whose only element is undef. So when you said
foreach ( @foo ) { print "array[x] = $_\n"; }
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.

When you then said

my @array $array[0] = "";
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 ;/

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

#!/path/to/your/perl -w use strict;
Trust me. mikfire

In reply to RE: While Vs. Foreach by mikfire
in thread While Vs. Foreach by matthew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.