To: beginners@perl.org
Subject: $_ and split function...

Hi
In the following code

****************************************************************
#!/usr/bin/perl
use warnings;
use strict;

my $found = 0;
$_ = "Nobody wants to hurt you... 'cept, I do hurt people sometimes, Case.";

my $sought = "people";

foreach my $word (split) {
if ($word eq $sought) {
$found = 1;
last;
}
}
if ($found) {
print "ok";
}
~
~
I don't understand the usage my $word (split).....how is $_ and $word 
linked?

cheers
v

SUN2>perldoc -f split
     split /PATTERN/,EXPR,LIMIT
     split /PATTERN/,EXPR
     split /PATTERN/
     split   Splits a string into a list of strings and returns
             that list.  By default, empty leading fields are
             preserved, and empty trailing ones are deleted.

             In scalar context, returns the number of fields
             found and splits into the @_ array.  Use of split in
             scalar context is deprecated, however, because it
             clobbers your subroutine arguments.

             If EXPR is omitted, splits the $_ string.  If
             PATTERN is also omitted, splits on whitespace (after
             skipping any leading whitespace).  Anything matching
             PATTERN is taken to be a delimiter separating the
             fields.  (Note that the delimiter may be longer than
             one character.)
...

perldoc is your friend ... 

So in this case $_ equals the assigned string, the split is on $_  and
whitespace and it returns a list - @_ - which each element is input by
foreach into $word ...

Try perldoc perldoc

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to