develooper Front page | perl.perl5.porters | Postings from February 2019

[perl #133809] Perl silently adds a key to hash

From:
Tony Cook via RT
Date:
February 5, 2019 22:42
Subject:
[perl #133809] Perl silently adds a key to hash
Message ID:
rt-4.0.24-19611-1549406532-1826.133809-15-0@perl.org
On Wed, 30 Jan 2019 14:50:37 -0800, jkeenan wrote:
> On Wed, 30 Jan 2019 10:23:54 GMT, v.99cygwin@gmail.com wrote:
> > Reply-To: v.99cygwin@gmail.com
> > Subject: Perl silently adds a key to hash
> > To: perlbug@perl.org
> > Message-Id: <5.28.1_9680_1548841096@hpzb>
> > From: v.99cygwin@gmail.com
> >
> >
> > This is a bug report for perl from v.99cygwin@gmail.com,
> > generated with the help of perlbug 1.41 running under perl 5.28.1.
> >
> >
> > -----------------------------------------------------------------
> > [Please describe your issue here]
> >
> > DESCRIPTION:
> >
> > Perl silently adds a key to hash - see example below, please:
> >
> > #!/usr/bin/perl
> > use strict;
> > use warnings;
> > use Data::Dumper;
> >
> > my %cities = (
> >      Paris => +{ state => 'France' },
> > );
> > print( "BEFORE-----------\n", Dumper( \%cities), "\n");
> > if( defined( $cities{London}->{population}) ) {
> >      print "is defined\n";
> > }else {
> >      print "isn't defined\n";
> > }
> > print( "AFTER-----------\n", Dumper( \%cities), "\n");
> >
> >
> >
> > OUTPUT:
> >
> > BEFORE-----------
> > $VAR1 = {
> >        'Paris' => {
> >                 'state' => 'France'
> >               }
> >      };
> >
> > isn't defined
> > AFTER-----------
> > $VAR1 = {
> >        'Paris' => {
> >                 'state' => 'France'
> >               },
> >        'London' => {}
> >      };
> >
> >
> > EXPECTED OUTPUT:
> >
> > BEFORE-----------
> > $VAR1 = {
> >        'Paris' => {
> >                 'state' => 'France'
> >               }
> >      };
> >
> > isn't defined
> > AFTER-----------
> > $VAR1 = {
> >        'Paris' => {
> >                 'state' => 'France'
> >               },
> >      };
> >
> >
> > NOTES:
> >
> > Perl silently adds key 'London' to %cities in order evaluate next
> > part
> > of expression. IMHO the result of dereferencing undef should be undef
> > of
> > whole expression, or runtime error.
> >
> > Same behavior observed in Cygwin (perl 5, version 26, subversion 3
> > (v5.26.3) built for x86_64-cygwin-threads-multi) and on Kubuntu (perl
> > 5,
> > version 26, subversion 1 (v5.26.1) built for
> > i686-linux-gnu-thread-multi-64int).
> >
> >
> 
> This behavior is what the Perl documentation calls "autovivification".
> See 'perldoc perlreftut' -- particularly the section titled "Solution"
> (as distinct from the section titled "The Solution").

As you say, it's documented behaviour, so rejecting.

Tony

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=133809



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About