Sponsored Content
Top Forums Programming Perl: restrict perl from automaticaly creating a hash branches on check Post 302924341 by durden_tyler on Saturday 8th of November 2014 02:12:18 AM
Old 11-08-2014
That's a documented behavior in Perl and is called "autovivification".
Check the stackoverflow website for the question "How do I disable autovivification in Perl?" for a detailed discussion and workarounds.

gandolf989 has a point here. Any reason you do *not* want the intermediate branches to be created? As long as they are empty and your existence checks return the values as expected, your program should be fine.

Autovivification in Perl is by design. It's not a bug; it's a feature. And I think it stems from Perl's tendency to go out of its way to make your text processing task easy.
For an in-depth technical discussion of why it works like that, what its advantages are and what problems it solves, check this link: http://www.sysarch.com/Perl/autoviv.txt
This User Gave Thanks to durden_tyler For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Hash

HI I have a hash like this $hashname->{$filesystem}->{'fsname'}=$filesystem; How to get the values from this multilevel hash. Thanks in advance... :) (1 Reply)
Discussion started by: Harikrishna
1 Replies

2. Shell Programming and Scripting

Perl Hash

hi i have two hash achi %disk1,%disk2 with( key, value) (key1,value1) How to store it in another hash.. Plz replyyy. Regards Hari (1 Reply)
Discussion started by: Harikrishna
1 Replies

3. Shell Programming and Scripting

perl (word by word check if a hash key)

Hi, Now i work in a code that 1-get data stored in the database in the form of hash table with a key field which is the " Name" 2-in the same time i open a txt file and loop through it word by word 3- which i have a problem in is that : I need to loop word by word and check if it is a... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

4. UNIX for Advanced & Expert Users

Perl loop txt and check if a hash key

Hi, The task i have to do is to 1- create a database contains the Names .run the query and store results in hash make the Name field is the hash key 2- in the same time i have a txt which i will loop through it word by word and check for each word if a hash key ( compare it with the Names in... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

5. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

6. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

7. Shell Programming and Scripting

Perl hash help

Hi , i have the below code its working fine when i execute in unix , but its not working in windows could you pls explain me where i am going wrong. This is the program $data = { '1' => 'one' , '2' => 'two' , 3 => 'three' }; print "hello : $data->{'1'}... (2 Replies)
Discussion started by: ragilla
2 Replies

8. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

9. Programming

Perl: How to load some functions automaticaly, starting Perl inteructively (with -d -e 0)?

I would like to use Perl in 'interactive' mode (kind off), starting it by > perl -d -e 0; But I need to have some function be read on loading. Also, it should not be for anyone who starting Perl or use it any how. I did try to search, but not much result. I have try a file '.perldb':... (1 Reply)
Discussion started by: alex_5161
1 Replies

10. Shell Programming and Scripting

Restrict remote DB connection from PERL

I have PERL code to connect to Oracle database using DBI. e.g. $PERL -e "use DBI; DBI->connect(qw(DBI:Oracle:db111 testu testpass));" by using DBI , if remote DB added to tnsnames.ora , I can connect using DBI . is there a way to restrict not to connect to remote DB using DBI ? ... (1 Reply)
Discussion started by: talashil
1 Replies
autovivification(3pm)					User Contributed Perl Documentation				     autovivification(3pm)

NAME
autovivification - Lexically disable autovivification. VERSION
Version 0.10 SYNOPSIS
no autovivification; my $hashref; my $a = $hashref->{key_a}; # $hashref stays undef if (exists $hashref->{option}) { # Still undef ... } delete $hashref->{old}; # Still undef again $hashref->{new} = $value; # Vivifies to { new => $value } DESCRIPTION
When an undefined variable is dereferenced, it gets silently upgraded to an array or hash reference (depending of the type of the dereferencing). This behaviour is called autovivification and usually does what you mean (e.g. when you store a value) but it's sometimes unnatural or surprising because your variables gets populated behind your back. This is especially true when several levels of dereferencing are involved, in which case all levels are vivified up to the last, or when it happens in intuitively read-only constructs like "exists". This pragma lets you disable autovivification for some constructs and optionally throws a warning or an error when it would have happened. METHODS
"unimport @opts" Magically called when "no autovivification @opts" is encountered. Enables the features given in @opts, which can be : o 'fetch' Turns off autovivification for rvalue dereferencing expressions, such as : $value = $arrayref->[$idx] $value = $hashref->{$key} keys %$hashref values %$hashref Starting from perl 5.11, it also covers "keys" and "values" on array references : keys @$arrayref values @$arrayref When the expression would have autovivified, "undef" is returned for a plain fetch, while "keys" and "values" return 0 in scalar context and the empty list in list context. o 'exists' Turns off autovivification for dereferencing expressions that are parts of an "exists", such as : exists $arrayref->[$idx] exists $hashref->{$key} '' is returned when the expression would have autovivified. o 'delete' Turns off autovivification for dereferencing expressions that are parts of a "delete", such as : delete $arrayref->[$idx] delete $hashref->{$key} "undef" is returned when the expression would have autovivified. o 'store' Turns off autovivification for lvalue dereferencing expressions, such as : $arrayref->[$idx] = $value $hashref->{$key} = $value for ($arrayref->[$idx]) { ... } for ($hashref->{$key}) { ... } function($arrayref->[$idx]) function($hashref->{$key}) An exception is thrown if vivification is needed to store the value, which means that effectively you can only assign to levels that are already defined In the example, this would require $arrayref (resp. $hashref) to already be an array (resp. hash) reference. o 'warn' Emits a warning when an autovivification is avoided. o 'strict' Throws an exception when an autovivification is avoided. Each call to "unimport" adds the specified features to the ones already in use in the current lexical scope. When @opts is empty, it defaults to "qw<fetch exists delete>". "import @opts" Magically called when "use autovivification @opts" is encountered. Disables the features given in @opts, which can be the same as for "unimport". Each call to "import" removes the specified features to the ones already in use in the current lexical scope. When @opts is empty, it defaults to restoring the original Perl autovivification behaviour. CONSTANTS
"A_THREADSAFE" True iff the module could have been built with thread-safety features enabled. This constant only has a meaning with your perl is threaded ; otherwise, it'll always be false. "A_FORKSAFE" True iff this module could have been built with fork-safety features enabled. This will always be true except on Windows where it's false for perl 5.10.0 and below . CAVEATS
The pragma doesn't apply when one dereferences the returned value of an array or hash slice, as in "@array[$id]->{member}" or @hash{$key}->{member}. This syntax is valid Perl, yet it's discouraged as the slice is here useless since the dereferencing enforces scalar context. If warnings are turned on, Perl will complain about one-element slices. DEPENDENCIES
perl 5.8.3. A C compiler. This module may happen to build with a C++ compiler as well, but don't rely on it, as no guarantee is made in this regard. XSLoader (standard since perl 5.006). SEE ALSO
perlref. AUTHOR
Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>. You can contact me by mail or on "irc.perl.org" (vincent). BUGS
Please report any bugs or feature requests to "bug-autovivification at rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=autovivification>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. SUPPORT
You can find documentation for this module with the perldoc command. perldoc autovivification Tests code coverage report is available at <http://www.profvince.com/perl/cover/autovivification>. ACKNOWLEDGEMENTS
Matt S. Trout asked for it. COPYRIGHT &; LICENSE Copyright 2009,2010,2011 Vincent Pit, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2011-08-24 autovivification(3pm)
All times are GMT -4. The time now is 12:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy