Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Why? uninitialized value in pattern match when using if (defined)

by GrandFather (Saint)
on Mar 21, 2006 at 23:30 UTC ( [id://538344] : note . print w/replies, xml ) Need Help??


in reply to Why? uninitialized value in pattern match when using if (defined)

I have a code snippet and it works exactly as expected:

use strict; use warnings; my $tmpf = 'farms'; my %factories = (farms => "Farmer Brown's"); print "$tmpf\n"; # prints 'farms' print keys( %factories), "\n"; # 'farms' is one of them. print "Is defined" if ( defined $factories{ $tmpf});

Prints:

farms farms Is defined

How is it different from your code snippet that generates an error message that on the face of it has nothing to do with the snippet? Perhaps you could modify my snippet to demonstrate your error? You could also read I know what I mean. Why don't you?.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Why? uninitialized value in pattern match when using if (defined)
by insaniac (Friar) on Mar 21, 2006 at 23:34 UTC
    or even with a match:
    #!/usr/bin/perl use strict; use warnings; my %factories = ( 'farms' => 'horses'); my $tmpf = 'farms'; my $match = "horses on the farm!"; print "$tmpf\n"; # prints 'farms' print keys( %factories), "\n"; # 'farms' is one of them. if ( defined $factories{$tmpf} ) { print "yay!\n" if $match =~ m/$factories{$tmpf}/; }
    prints:
    farms farms yay!

    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame