http://qs321.pair.com?node_id=147268


in reply to Re: Re: uninitialized value in phonebook program
in thread uninitialized value in phonebook program

Oh, now I got it! The warning is not actually coming from the if ($2 =~ /^0$/) { line. Unfortunately, perl sometimes reports the line number where an if block starts when the warning actually occurs later in the block. That's what is happening here.

First, you perform a regex match against $name, setting $1 and $2. Then, you perform a regex match against $2. So far, so good. Then you print out the values of $1 and $2. Oops! That second regex match you just did has wiped out the values of $1 and $2!

jeffa's node shows two ways to solve this problem. Either assign the results of the first match to new variables: my($first, $last) = $name =~ /(.+)&(.+)/; Or replace the second match with a comparison: if ($2 eq '0') {