Steve Bertrand wrote:
...
> while ($buf = <FILE>) {
> # $buf now contains line of file, one per each loop of while
> $buf =~ /(\w+)/;
> $userName = $1;
> ...do something with $userName
> }
This is a common error. You should not use $1 without making sure the regex
did in fact match. Otherwise, $1 will have whatever value it previously had.
You need to do something like:
if ($buf =~ /(\w+)/) {
$userName = $1;
...
}
...
> Now, to answer the remaining questions. =~ is the 'pattern match'
> operator. It's what actually fetches the first word pattern (user).
> Likewise, !~ says 'do not match'.
Well, techincally, the m// is the pattern (regex) match operator.
=~ is used to "bind" the regex match to a scalar other than $_.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>