Nigel Peck wrote:
>
> Hi all,
>
> I'm trying to get the following to work and can't. It's the assignment
> to $val1 and $val2 that's causing me the problem.
>
> my ( $val1, $val2 ) = $data =~ /^([^:]+):([^:]+)$/
>     || die 'Failed to parse data';
>
> What am I doing wrong? I can do it on multiple lines by assigning $1 and
> $2 to the variables after the regex but I want to do it all on one line.

Hi Nigel

The || operator has a higher priority than assignment, so you've written:

my ($val1, $val2) = ($data =~ /^([^:]+):([^:]+)$/ || die 'Failed to parse 
data');

This forces the regex match into scalar context, so it returns 1 if it succeeds,
which leaves $val1 equal to 1 and $val2 undefined.

Instead, use the low-precedence 'or' operator:

my ($val1, $val2) = $data =~ /^([^:]+):([^:]+)$/ or die 'Failed to parse data';

and all will be well.

HTH,

Rob#

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