"Eddie C." wrote:
> 
> Hello,

Hello,

> I have 2 little programs here and they do different things, but I don't understand 
>why.
> 
> # First program
> 
> use warnings;
> use strict;
> my $line = "TRAVSTAT 08-27-2002 02:20:15 There is no output file required.";
> (my $word) = ($line =~ /(\S+)/);
> print $word;
> # Prints TRAVSTAT
> 
> # Second program
> 
> use warnings;
> use strict;
> my $line = "TRAVSTAT 08-27-2002 02:20:15 There is no output file required.";
> my $word = ($line =~ /(\S+)/);
> print $word;
> # Prints 1
> 
> The only difference is the lack of parentheses around my $word
> on the 4th line.  I read that this has something to do with
> scalar context, but I don't see why one would be scalar context
> and not the other.  They both look like scalars to me.
> 
> Will someone help me understand the difference?


The use of parentheses on the LHS of the expression forces a list
context on the RHS, without the parentheses the LHS forces a scalar
context on the RHS.  In a list context, a regular expression returns the
list of matches that were enclosed in parentheses.  In a scalar context,
a regular expression returns 1 (true) if it matched or 0 (false) if it
didn't match.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to