Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^4: Two meanings of undef

by likbez (Sexton)
on Aug 16, 2020 at 02:54 UTC ( [id://11120800]=note: print w/replies, xml ) Need Help??


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

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;

Replies are listed 'Best First'.
Re^5: Two meanings of undef
by haukex (Archbishop) on Aug 16, 2020 at 09:10 UTC
    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).

Re^5: Two meanings of undef (updated)
by AnomalousMonk (Archbishop) on Aug 16, 2020 at 03:28 UTC

    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.
Re^5: Two meanings of undef
by LanX (Saint) on Aug 16, 2020 at 10:57 UTC
    > 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11120800]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found