On Jan 5, Prahlad Vaidyanathan said:

>> >my $leading_spaces = ($text =~ m/^(\s+)/) ; # This doesn't work
>> >print $leading_spaces ;                     # Prints 1
>> 
>> You have executed the regex in scalar context.  Why?  Because the
>
>Does this mean the return value of the match is put into $leading_spaces
>?

Yes.  A regex returns different things in scalar or list context.  In
scalar context, it returns whether or not it match (1 or '').  In list
context, it returns the $1, $2, $3 variables.

Context is determined by what is on the left-hand side of the = sign.  If
there is NOT a list on the left-hand side, then the context is SCALAR.

  $foo = bar();
  $a, $b, $c = bar();    # because '=' is tighter than ','
  $a, $b, ($c = bar());  # same as above
  bar();                 # NO = at all, this is void context

Void context is a special type of scalar context.

>Thanks. That worked. But, I still don't understand what "list" in this
>context means. And, what does "one-element list" mean ?

You might want to read my article "'List' is a Four-Letter Word", which
you can read here:

  http://www.pobox.com/~japhy/articles/pm/2000-02.html

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


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

Reply via email to