in reply to Beginner in perl : Use of uninitialized value
G'day Perl_Programmer1992,
Many of Perl's operations use $_ as a default value. You may have seen print and the for loop used that way:
$ perl -MO=Deparse -e 'for (0..2) { print }' foreach $_ (0 .. 2) { print $_; } -e syntax OK
[See B::Deparse if the code in that example is unfamiliar to you.]
$_ is also the default variable for a "pattern match (m//)". These are equivalent:
/fred/ $_ =~ /fred/ $_ =~ m/fred/
Relating all of that to your code: by assigning the value of each foreach iteration to $line, you do not assign the value to $_ (either explicitly or implicitly by default); consequently, $_ is undefined when used as the default value in /fred/.
By the way, in case you didn't know, for and foreach are synonymous.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Beginner in perl : Use of uninitialized value
by Perl_Programmer1992 (Sexton) on Dec 28, 2018 at 06:30 UTC |