Jenda Krynicky wrote:
From: "Chas. Owens" <[EMAIL PROTECTED]>
On Oct 24, 2008, at 11:00, "Sharan Basappa" <[EMAIL PROTECTED]> wrote:

I was just trying to match a string and save it in a single statement
as follows:

$extracted = "cp xyz";
$state_var = $extracted =~ m/cp\s+(.*)/;
print "$state_var $1 \n";

The output is: 1 xyz

So the assignment to $state_var does not work. Is this an incorrect way.

In scalar context a match returns true if it matches or false if it doesn't. You want to use list context to cause the match to return the captures:

($var) = $foo =~ /(blah)/;

It's not required, but this is one of the places where I would use explicit braces.


($var) = ($foo =~ /(blah)/);


Not so much because the left hand side is in braces, but because I'm never really sure which one, = or =~, has higher precedence.

There are no braces {} in that expression.  See:

http://en.wikipedia.org/wiki/Bracket

for the correct nomenclature.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to