perl - Regex matches but $1 is uninitialized -


my code snippet:

my $url_pattern = qr/http.*html/; foreach $urlcandidate(@urlcandidates) {     if ($urlcandidate !~ $url_pattern)     {         next;     }     $url = $1;     if ($url !~ $some_other_pattern)   # line 216     # ... } 

i warning: use of uninitialized value $url in pattern match (m//) @ ./myscript.pl line 216.

what don't understand - if next instruction isn't executed have match. if have match $1 should contain url string. instead it's uninitialized. why's that?

you're mixing 2 things. 'match' boolean test. piece of text match particular pattern.

if ($urlcandidate !~ $url_pattern) 

this only tests whether variable (not) pattern defined.

$1 capture group, , it's used select things pattern. usually, that's 'stuff in brackets'.

so if turn url pattern into:

qr/(http.*html)/ 

then $1 defined.

personally though, don't whole $1 syntax, , tend assign variables directly out of pattern.

e.g.:

my ( $capture ) = ( $string =~ m/content: (\w+)/ ); 

you can still use in boolean expression ( if tests last expression):

if ( ( $capture ) = m/pattern_match: (\w+)/ ) {     print $capture; } 

or alternatively:

if ( $string =~ m/(?<capture>\w+)/ ) {     print dumper \%+;     print $+{capture},"\n"; } 

alternatively, there's set of match variables:

$`, $&, $' 

$& string matched last successful pattern match (not counting matches hidden within block or eval() enclosed current block).

$` string preceding whatever matched last successful pattern match, not counting matches hidden within block or eval enclosed current block.

$' string following whatever matched last successful pattern match (not counting matches hidden within block or eval() enclosed current block).

these each come caveat though:

http://perldoc.perl.org/perlvar.html#performance-issues

traditionally in perl, use of of 3 variables $` , $& or $' (or use english equivalents) anywhere in code, caused subsequent successful pattern matches make copy of matched string, in case code might subsequently access 1 of variables. imposed considerable performance penalty across whole program, use of these variables has been discouraged.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -