On Jul 21, Brent Clark said:

I have the following code:

($fileName) = ($_ =~ /regexcode/o);

Which gives me the correct data.

But if I make it like so (note the () missing around the variable):

$fileName = ($_ =~ /regexcode/o);

Whats the difference.

The difference is the context. A pattern match returns different values depending on whether it was called in scalar context or list context. In scalar context, a pattern match returns whether or not it was able to match. In list context, the pattern match returns the capture groups:

  $x   = "japhy" =~ /(.)...(.)/;  # $x = 1
  ($x) = "japhy" =~ /(.)...(.)/;  # $x = 'j'
  @x   = "japhy" =~ /(.)...(.)/;  # @x = ('j', 'y')

By placing parentheses on the left-hand side of an = operation, you're creating a list of values (even if it's one, or even zero values).

If the pattern match has the /g modifier on it (for global matching), the context changes how it behaves as well.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart

--
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