@LanX

One workaround would be to surround every value with quotes "$h{bla}" , to enforce a copy.
-- thank you. That's good hint. I have tried also $h{bla} // "".

@haukex

I'm pretty sure the answer is the same as in your previous thread that you referenced: map is providing lvalue context to its args, while the other two examples of string concatenation do not, hence the hash value is autovivified.
Yess.. this is the same problem. Sorry for coming again with the same...

$ perl -wMstrict -MData::Dump -e 'my %h; map{$_} $h{X},"$h{Y}"; dd +\%h' Use of uninitialized value $h{"Y"} in string at -e line 1. { X => undef }
Hmmmm... only one warning... It doesn't warn about X. Educative example.

I'm curious why you have so many issues with autovivification? Personally, I hardly ever do, because I use exists, or keys when looping over multiple hash keys.
In my company (dubielvitrum.com, manufacturer of mirrors) there is manufacturing system written in perl. And all data is kept in structured files, with strict data types (numbers, identifiers, quoted texts). This data is checked carefully on input and output. When undef gets somehow into hash then error is thrown. So I have to search how undef got injected into data structures. Original piece of code that made that error was like this:

$wi{OrderUniqueId} = join " ", grep { $_ ne "" } map { unquote +_text($_) } ($ty eq "need" ? "Zamów" : "Posia"), $$it{name}, $$it{ +para}{Termin}, $$it{para}{Ile}, seq_num(6);

To build OrderUniqueId I take (1) word "Zamów" or "Posia" (2) name of item (3) parameter "Termin" (4) parameter "Ile" (6) some sequence number -- take all non empty of these, join them with spaces.

$wi{OrderUniqueId} = join " ", grep { $_ ne "" } map { unquote_text($_ +) } ($ty eq "need" ? "Zamów" : "Posia"), $$it{name}, $$it{ +para}{Termin} // "", $$it{para}{Ile} // "", seq_num(6);

Normally when working with perl I hardly ever got errors from autovivification when aliased with grep or  map.


In reply to Re: Sometimes undef is initialized and sometimes not when hash values are fed to grep by leszekdubiel
in thread Sometimes undef is initialized and sometimes not when hash values are fed to grep by leszekdubiel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.