in reply to Use of uninitialized value $1
use v5.10; use warnings; use strict; my $var; $var = "path_to_my_lib"; $var =~ m/(lib)/; say $1;
Which prints the captured "lib" (and gives no warnings).
$1, $2.. etc are weird variables. You can not assign them a value, and they disappear when they go out of scope, or a new regexp is encountered. You can solve it in a few ways, for example, test it before using it:
if($1){ say $1; }
or test the regular expression to match:
if( $var =~ m/(foo)/ ){ say $1; }else{ say "no match found"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Use of uninitialized value $1
by AnomalousMonk (Archbishop) on Aug 25, 2016 at 12:29 UTC | |
by Marshall (Canon) on Aug 25, 2016 at 19:15 UTC | |
by AnomalousMonk (Archbishop) on Aug 26, 2016 at 01:06 UTC | |
by Marshall (Canon) on Aug 26, 2016 at 03:15 UTC | |
|
Re^2: Use of uninitialized value $1
by Pazitiff (Novice) on Aug 25, 2016 at 12:27 UTC | |
by AnomalousMonk (Archbishop) on Aug 25, 2016 at 12:41 UTC | |
by FreeBeerReekingMonk (Deacon) on Aug 25, 2016 at 12:52 UTC |