in reply to Split ( ) is Giving Me a Splitting Headache
EXPR is not omitted, PATTERN is not omitted - where is $_ coming from? Is there some documentation someone can point out that explains this behavior?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.)
#!/usr/bin/perl use warnings; use strict; print "$_\n" for split ( "Hello World" ); print "$_\n" for split /\s+/, "Hello World";
But why is for not setting it in the first print statement?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.
#!/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" };
|
|---|
| 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 |