http://qs321.pair.com?node_id=790258

hnd has asked for the wisdom of the Perl Monks concerning the following question:

i was skimming through the camel book (just revising the long forgotten things) when something struck me.....
i made a small program that creates a reference to a scalar and a hash like this
use strict; use warnings; my $scalar_ref=*foo{SCALAR}; print "scalar ref defined\n" if defined $scalar_ref; #------------------------# my hash_ref=*foo{HASH}; print "hash ref defined\n" if defined hash_ref; __END__ output scalar ref defined
the output is right in this case as the compiler need not see a scalar to create a reference but it needs to see a hash to create a reference.... so for this i did
use strict; use warnings; my $scalar_ref=*foo{SCALAR}; print "scalar ref defined\n" if defined $scalar_ref; #------------------------# my %hash=(); my hash_ref=*hash{HASH}; print "hash ref defined\n" if defined hash_ref; __END__ output Name "main::foo" used only once: possible typo at ref.pl Name "main::hash" used only once: possible typo at ref.pl scalar defined
now the problem is... the compiler sees that we do hv a hash existing then too the statement hash ref defined doesnt crop up..... why is that????
=====================================================
i'am worst at what do best and for this gift i fell blessed...
i found it hard it's hard to find well whatever
NEVERMIND

Replies are listed 'Best First'.
Re: creating references
by BioLion (Curate) on Aug 21, 2009 at 08:38 UTC

    Using diagnostics for more verbose warnings should make things more clear:

    The key part explaing the difference in ouput for your two scripts is :

    NOTE: This warning detects symbols that have been used only once so $c, @c, %c, *c, &c, sub c{}, c(), and c (the filehandle or format) are considered the same; if a program uses $c only once but also uses any of the others it will not trigger this warning.

    There is also a good discussion of a similar problem in "main::Objects" used only once: possible typo. HTH.

    Just a something something...
Re: creating references
by ikegami (Patriarch) on Aug 22, 2009 at 04:05 UTC

    *foo is a typeglob, or glob for short. It is the part of the symbol table that holds the variables associated with a name. Since the symbol table contains the package variables, the variables a glob contains are package variables (often called global variables). my creates lexical variables. They're stored in the current function's pad, not the symbol table.

Re: creating references
by AnomalousMonk (Archbishop) on Aug 21, 2009 at 17:13 UTC

    Note also that you are creating a lexical variable with the  my %hash=(); statement, and the  *foo{THING} syntax applies only to package variables; you get the '... "main::hash" used only once ...' message because there is no  %hash package hash variable in existence.

      thnx....
      =====================================================
      i'am worst at what do best and for this gift i fell blessed...
      i found it hard it's hard to find well whatever
      NEVERMIND