in reply to Re^2: Two meanings of undef
in thread Two meanings of undef

DB<64> p exists $main::{X} DB<65> $X = undef DB<66> p exists $main::{X} 1 DB<67>

> Do not shoot the piano player, ... :-)

Why would I?

I'm not aware that Charles Aznavour spread fake news about programming ...

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^4: Two meanings of undef
by likbez (Sexton) on Aug 16, 2020 at 02:54 UTC

    Thank you ! It is always a pleasure to see your replies.

    But it is a bad practice to use a key in hashes without quotes, as in $main::{X} ;-)

    A more revealing variant of your test would be

    [0]  # cat undef_exists_test.pl
    use v5.10;
    say $main::{'X'};
    say $X;
    say $main::{'X'};
    if( exists $main::{'X'} ){ say "exists";} else {say "does not exist";}
    if( defined($X) ){say "defined";} else {say "not defined";}
    
    $X=1;
    undef $X;
    if( exists $main::{'X'} ){ say "exists";} else {say "does not exist";}
    if( defined($X) ){say "defined";} else {say "not defined";}
    
    
    Running it I got:
    *main::X
    
    *main::X
    exists
    not defined
    exists
    not defined
    
    [0]  #
    

    We can see that even before the statement "say X;" executed the variable X was already added to the symbol table (during begin block execution?),

    So it does exists.

    NOTE:

    I think undef is not a value, as many here assume, but a function, So
    $X=undef;
    actually means in Perl:
    $X=undef();
    and is equivalent to
    undef $X;
      But it is a bad practice to use a key in hashes without quotes, as in $main::{X}

      No, it's not. Some people advise against hash key autoquoting, because its behavior can be confusing to some, but this is still a question of style and preference, not "best practices".

      NOTE: I think undef is not a value ...

      You've made this same edit to the root node. Please mark all of your updates as such, and also don't duplicate content like this, as it makes the thread confusing to follow (some might reply to one copy of the content and miss replies to the other copy).

      There's a distinction between %main:: (the symbol table of the main package, i.e., namespace) and the two hashes %main and %main::main (which are two different ways of naming the same hash!) in the main package.

      c:\@Work\Perl\monks>perl -w -le "use v5.10; say X; say $main::{X}; if(exists $main::{X} ){ say 'A: exists';} else {say 'A: does not e +xist';} if(exists $main{X} ){ say 'B: exists';} else {say 'B: does not e +xist';} if(exists $main::main{X}){ say 'C: exists';} else {say 'C: does not e +xist';} die 'not the same' if \%main != \%main::main; die 'are the same' if \%main == \%main::; if( defined(X) ){say 'defined';} else {say 'not defined';} " say() on unopened filehandle X at -e line 1. *main::X A: exists B: does not exist C: does not exist defined
      (Note: warnings are enabled, strictures disabled.)

      The statement say X; creates an unopened filehandle X in the main package which exists and is defined, although useless. The hash elements $main{X} and $main::main{X} (again, two different names for the same thing) do not exist and are not defined.

      Update 1: A few minor wording changes.

      Update 2: The difference and equivalence among the two separate hashes running around in this example code can be highlighted by inserting the
          say ' \%main:: ', \%main::, ' \%main ', \%main, ' \%main::main ', \%main::main;
      statement just before the two die statements. Note that autovivification is at work to bring the latter two hashes | hash with two names into existence.


      Give a man a fish:  <%-{-{-{-<

      A reply falls below the community's threshold of quality. You may see it by logging in.
      > it is a bad practice to use a key in hashes without quotes

      Who are you? Ramus Lerdorf?

      I have to agree with others that your bold claims sound trollish.

      So I'll better refrain from further feeding...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery