Why when you access the {SCALAR} key of a symbol entry in the Symbol Table of a package it always return a scalar reference?! Even if the scalar, or any symbol (array, hash, glob) with this name is not defined!

For example:

my $name = 'main::test' ; if (defined *{$name}{SCALAR}) { print "\$$name\n" ;} if (defined *{$name}{ARRAY}) { print "\@$name\n" ;} if (defined *{$name}{HASH}) { print "\%$name\n" ;}
This prints:
$main::test
If you set the array and hash before
@test = %test = 1 ;
it prints:
$main::test @main::test %main::test
Soo, it always print true for $main::test!!!

The worst thing is that it always return a scalar reference:

my $name = 'main::test' ; my $ref = *{$name}{SCALAR} ; print "$ref\n" ;
This prints:
SCALAR(0x1a6f080)
Soo, it always create a new scalar in the memory, even if you just want to know if the scalar exists in the memory!

This was tested with Perl-5.6.1-Win32, Perl-5.8.1-Win32 (the new release), Perl-5.6.1-Linux.

We can see that Devel::Symdump uses

if (defined $val && defined *ENTRY{SCALAR}) {
to test if a scalar exists. But if the value of the scalar is undef it's says that the scalar is not in the table. And since *ENTRY{SCALAR} always return a scalar ref it's not needed.

I think that the behavior doesn't work like it should, since for {ARRAY} and {HASH} it works like we want. At least this need to be documented!

Graciliano M. P.
"Creativity is the expression of the liberty".


In reply to Symbol Table entry always return defined for {SCALAR}!!! by gmpassos

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.