First, never use construction like:
my $var = $val if( $some );
according to documentation:
NOTE: The behaviour of a my, state, or our modified with a statement
modifier conditional or loop construct (for example, my $x if ... ) is
undefined. The value of the my variable may be undef, any previously
assigned value, or possibly anything else. Don't rely on it. Future
versions of perl might do something different from the version of Perl
you try it out on. Here be dragons.
The m//
operator, when the /g
modifier is specified in list context, it returns a list of the substrings matched by any capturing parentheses in the regular expression. So, as @Сухой27 says in the comment above, you should to use:
my ($some) = $str =~ m/...(...).../g;
With simple example:
use strict;
use warnings;
my $u="undefined";
my $str = q{some="string" another="one"};
#will match
my ($m1) = $str =~ m/some="(.*?)"/g;
print 'm1=', $m1 // $u, '= $1=', $1 // $u, "=\n";
#will NOT match
my ($m2) = $str =~ m/nothere="(.*?)"/g;
print 'm2=', $m2 // $u, '= $1=', $1 // $u, "=\n";
#will match another
my ($m3) = $str =~ m/another="(.*?)"/g;
print 'm3=', $m3 // $u, '= $1=', $1 // $u, "=\n";
prints:
m1=string= $1=string=
m2=undefined= $1=string= #the $1 hold previously matched value
m3=one= $1=one=
As you can see, the $1
remains, when matching NOT successful. The documentation says:
These special variables, like the %+ hash and the numbered match
variables ($1
, $2
, $3
, etc.) are dynamically scoped until the end
of the enclosing block or until the next successful match, whichever
comes first. (See Compound Statements in perlsyn.)
NOTE: Failed
matches in Perl do not reset the match variables, which makes it
easier to write code that tests for a series of more specific cases
and remembers the best match.
So, if you don't want have the $1
defined, you can enclose the matching part into a block, like:
use strict
use warnings;
my $u="undefined";
my $str = q{some="string" another="one"};
my($m1,$m2,$m3);
{($m1) = $str =~ m/some="(.*?)"/g;}
print 'm1=', $m1 // $u, '= $1=', $1 // $u, "=\n";
{($m2) = $str =~ m/nothere="(.*?)"/g;}
print 'm2=', $m2 // $u, '= $1=', $1 // $u, "=\n";
{($m3) = $str =~ m/another="(.*?)"/g;}
print 'm3=', $m3 // $u, '= $1=', $1 // $u, "=\n";
what prints
m1=string= $1=undefined=
m2=undefined= $1=undefined=
m3=one= $1=undefined=
PS: I'm not a Perl guru, maybe others will extend/correct this answer.