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

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

Thread Previous | Thread Next
From:
James E Keenan via RT
Date:
January 30, 2019 22:50
Subject:
[perl #133809] Perl silently adds a key to hash
Message ID:
rt-4.0.24-14132-1548888637-1988.133809-15-0@perl.org
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").

To paraphrase that section of perlreftut ...

This is Perl, so it does the exact right thing.  It sees that you are asking whether there is a hash element (a) keyed on 'population', (b) which has a defined value, and (c) which is an element of an anonymous hash which (d) itself is not yet created but which (e) is to be the value of an element keyed on 'London' within %cities.  So Perl helpfully makes a new, empty, anonymous hash for you, installs it as the value of the 'London' element within %cities.  This is called autovivification -- bringing things to life automatically.  Perl saw that the element keyed on 'London' wasn't in %cities, so it created a new hash entry automatically.

Thank you very much.

-- 
James E Keenan (jkeenan@cpan.org)

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

Thread Previous | Thread Next


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