For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2005 > to () or not to (), that is the question.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author to () or not to (), that is the question.
Brent Clark

2005-07-24, 8:29 pm

Hi list

Would someone be so kind as to share some light on this for me.

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.

Any tips / feedback would greatfully be appreciated.

Kind Regards
Brent Clark
Brent Clark

2005-07-24, 8:29 pm

Sorry I for got to add.

In the second statement

I only get a "1"

Thanks
Brent
Jeff 'japhy' Pinyan

2005-07-24, 8:29 pm

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

2005-07-24, 8:29 pm

Brent Clark wrote:
> Hi list
>
> Would someone be so kind as to share some light on this for me.
>
> 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 parens on the left side place the assignment operation in "list
context". This means the right-hand side (the regex match) will be evaulated
in list context, which gives different results then when it's evaluated in
scalar context (as in the second example).

You can read the section on "Context" in perldoc perldata for more details.
Brent Clark

2005-07-24, 8:29 pm

Jeff 'japhy' Pinyan wrote:
> 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.
>


Thanks so much.
I cant believe I did know, realise that.

Thanks again
Brent Clark

Bryan R Harris

2005-07-24, 8:29 pm



Great response, by the way, Jeff -- I wish I'd read this 3 years ago...

- Bryan



> On Jul 21, Brent Clark said:
>
>
>
> 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.




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com