Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Two meanings of undef

by likbez (Sexton)
on Aug 15, 2020 at 22:25 UTC ( [id://11120794]=note: print w/replies, xml ) Need Help??


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

That's wrong, the variable is still in the symbol table, it's value is just undef.
Do not shoot the piano player, ... :-)

In FAT if you delete a file from the directory it is still present but marked as deleted.

So the fact that it is still present does not change the fact that semantically it is deleted.

In other words, if the item in a Perl symbol table has value undef for all practical purposes is it that same as deleted item in FAT and should be treated by interpreter developers in the same way as "not found" item.

Replies are listed 'Best First'.
Re^3: Two meanings of undef
by AnomalousMonk (Archbishop) on Aug 15, 2020 at 22:49 UTC
    In FAT if you delete item from the directory it is still present but marked as deleted.

    So the fact that it is still present does not change the fact that semantically it is deleted.

    Further to stevieb's and haukex's comments:   In a file system, if you delete a file and then create another file with the same name, do you get any warning that the old file still, in any sense, "exists", or "masks" anything, or that there is any possible symbol/name collision? The symbol spaces of Perl are not file systems.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $scalar = 42; undef $scalar; my $scalar; " "my" variable $scalar masks earlier declaration in same scope ...
    Of course, this message is only emitted if warnings are enabled, but it highlights the fact that an undef-ed scalar still very much exists. (Same behavior with our package-global scalars.)

    I think the critical point that you miss is that undef is a very well-defined value! It is not random, it can be tested, scalars having that value exist in every sense, etc.


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

Re^3: Two meanings of undef
by haukex (Archbishop) on Aug 15, 2020 at 22:46 UTC
    In other words, if the item in a Perl symbol table has value undef for all practical purposes is it that same as deleted item in FAT and should be treated by interpreter developers in the same way as "not found" item.

    No, that's an incorrect analogy. Assigning undef to a scalar is like truncating a file down to zero bytes - it still exists as before, just its content has been cleared. (Not to mention that lexicals aren't even in the symbol table. See perlmod for the details.)

Re^3: Two meanings of undef
by stevieb (Canon) on Aug 15, 2020 at 22:37 UTC
    "In other words, if the item in a Perl symbol table has value undef for all practical purposes is it that same as deleted item in FAT and should be treated by interpreter developers in the same way as "not found" item. "

    Might I ask where you got your information that Perl variables are relative to a file system designed in the 80's?

    In other words, before you make statements that relate the two together, you should at least test on another 3-4 programming languages and see how things are dealt with there first.

    At least that's what I'd do before making blanket complaints.

    Have you filed a bug report for this 'issue' with the p5p team?

Re^3: Two meanings of undef
by LanX (Saint) on Aug 16, 2020 at 01:15 UTC
    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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found