in reply to Split ( ) is Giving Me a Splitting Headache

OK Im a little confused now - I see where and why he is getting his error above and that $_ is undefined. In my example below - I see the first print statement which I beleive to be equivelant to the ops print statement. But Im confused about my second print statement - where and how is $_ getting set? Is split setting it?

from perldoc -f split:
If EXPR is omitted, splits the $_ string. If PATTERN is also omitted, + splits on whitespace (after skipping any leading whitespace). Anyth +ing matching PATTERN is taken to be a delimiter separating the fields +. (Note that the delimiter may be longer than one character.)
EXPR is not omitted, PATTERN is not omitted - where is $_ coming from? Is there some documentation someone can point out that explains this behavior?
#!/usr/bin/perl use warnings; use strict; print "$_\n" for split ( "Hello World" ); print "$_\n" for split /\s+/, "Hello World";

UPDATE
OK I see in perlvar that for is setting it and split is not my culprit.
Here are the places where Perl will assume $_ even if you don't use it +: __snip__ - The default iterator variable in a foreach loop if no other variable + is supplied.
But why is for not setting it in the first print statement?

UPDATE #2
from the following looks like $_ is an empty hash?
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; print "\n\n"; print "\$_ = $_\n"; print "\n\n"; for ( 1 .. 10 ) { print "\$_ = $_\n" } print "\n\n"; print $_ for {}; print "\n\n"; print Dumper $_ for {}; print "\n\n"; my $count = 0; for ( sort keys %{ $_ } ) { print $count++, ":$_{$_} $_\n" };

Now Im really confused.
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Replies are listed 'Best First'.
Re^2: Split ( ) is Giving Me a Splitting Headache
by bpphillips (Friar) on May 25, 2006 at 11:24 UTC
    The key here is what happens when you perform the incorrect usage of split:
    my @parts = split( "Hello World" ); # generates warning print "Parts: " . scalar(@parts) . "\n"; # prints "Parts: 0" for( @parts ){ # never enters this loop since the array is empty print "$_\n"; }
    The warning you saw on your first loop was generated by the split statement, not the print statement. As demonstrated above, the print statement never gets executed because the loop is never entered.

    Hope that helps make some sense of what's happening,
    -- Brian