bugsbunny has asked for the wisdom of the Perl Monks concerning the following question:

hi,
I have very weird problem, or i'm too stupid..so,
diff myprog myprog.new 276a266 > my $xx = $db[$vars{_recNo}]{'name'};

Ok the variable $xx is not used in any place of the program, just this assignment.. but the program yeld different result if I comment it ?! It is not nececery to make assignment, just using it changes the result.. in my case it was into one reg-ex.. but assignment is easer do debug...so lets continue..
do u have any explanation on this...
some examples, lets call result sets - rs1 and rs2 :
my $xx = $db[$vars{_recNo}]{'name'};
yield rs1
my $xx = $db[$vars{_recNo}]{'name'}; $xx = 0;
yield rs1
#my $xx = $db[$vars{_recNo}]{'name'};
yield rs2
my $xx = $db[5]{'name'};
yield rs2, never mind what number I use..
my $xx = $db[$vars{_recNo}];
yield rs2

I'm again mentioning this virable is not used in any part of the script....
Tell me other testing scenarios ot better some solution :") I'm with Gentoo - perl-5.8.0-r12
It seems to me like some problem with hashes ! or me just doing stupid things !!

Replies are listed 'Best First'.
Re: weird problem or perl-bug?
by PodMaster (Abbot) on Dec 20, 2003 at 12:31 UTC
    Heard of autovivification? (btw, its documented in perlref...)
    use strict; use warnings; use Data::Dumper; my @bar; my $foo = $bar[1]{bar}; die Dumper( \@bar ); __END__ $VAR1 = [ undef, {} ];

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      yep i've heard about it... forgot about it.. have to test... thanx alot
      Does :
      print 'a' if defined $vars[xxx]{zzz};
      brings the element into existense.. i.e. how to check some element w/o turning it into existense if it doesnt exists already ?!

        print 'a' if exists $vars[xxx]{zzz};
        will check whether the hash contains an element with the key zzz, but not autovivify it.

        perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: weird problem or perl-bug?
by bart (Canon) on Dec 20, 2003 at 12:38 UTC
    Just mentioning $db[$vars{_recNo}]{'name'} might make it spring to life. It's a process known as "autovivification" ("automatic bringing to life", or somesuch). Note this little testscript:
    use Data::Dumper; my %vars = ( _recNo => 3 ); my @db; (undef) = $db[$vars{_recNo}]{'name'}; print Dumper \@db;
    Resulting in:
    $VAR1 = [ undef, ${\$VAR1->[0]}, ${\$VAR1->[0]}, {} ];
    Never mind the ${\$VAR1->[0]}, it's just Data::Dumper's silly way of saying it's uninitialized (the "same" undef value as the first array item), but trying to check a (nonexistent) field in what isn't even a hash, makes the hash spring to life. That's why you see an empty anonymous hash there, as a fourth array element.

    Searching through Google for perl autovivify OR autovivification brings up this article as a first hit: Autovivification : What Is It and Why Do I Care?.

Re: weird problem or perl-bug?
by Corion (Patriarch) on Dec 20, 2003 at 12:39 UTC

    My guess is, that each element of @db needs to contain a name entry in its keys. Maybe try:

    #my $xx = $db[$vars{_recNo}]{'name'}; $db[$vars{_recNo}]{'name'} = 'exists';
    or
    #my $xx = $db[$vars{_recNo}]{'name'}; $db[$vars{_recNo}]{'name'} = undef;
    and see whether this gives you the same results. This is the "autovivication" PodMaster talks about.
    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: weird problem or perl-bug?
by pg (Canon) on Dec 20, 2003 at 17:01 UTC

    In Perl, in case of handling a multi-tier collection (hash or array), to read (including test) an element, will automatically bring its ancestors (in the tree-like structure) into existance.

    I don't like this "feature" at all, as it largely reduces your application's maintainability. But we have to live with it (at least for now).

Re: weird problem or perl-bug?
by Anonymous Monk on Dec 20, 2003 at 12:17 UTC
    Is this a joke?
      I want it to be a joke, but no it is not a joke...:"(
      Result set 1 : 78a 7a4 7ec 7ee 7f0 820 85e 866 868 890 896 8aa 8c2 8d6 8f4 90c 910 Result set 2 : 788 7a2 7ea 7ec 7ee 81e 85c 864 866 88e 894 8a8 8c0 8d4 8f2 90a 90e