Symbolic reference to function in wrong package? 
Author Message
 Symbolic reference to function in wrong package?

While messing about with a bit of obfuscated perl, I stumbled upon
this behaviour:

cthulhu:~$ perl -le 'package Foo; $a="Bar"; &{$a};'
Undefined subroutine &Foo::Bar called at -e line 1.
cthulhu:~$ perl -le 'package Foo; $a=",Bar"; &{$a};'
Undefined subroutine &main::,Bar called at -e line 1.
cthulhu:~$

Note how the missing subroutine is suddenly expected to be in main::
rather than Foo:: when I let the name start with a non-alphanumeric
character. I'm not calling this a bug or anything, since I know I'm
far into "Don't _do_ that!" territory, but I'm curious about why it
happens. Would someone here care to enlighten me?
--

  "Such a pretty day for a {*filter*}bath." -- Callisto, "Xena: Warrior Princess"



Thu, 24 Oct 2002 03:00:00 GMT  
 Symbolic reference to function in wrong package?
: While messing about with a bit of obfuscated perl, I stumbled upon
: this behaviour:

: cthulhu:~$ perl -le 'package Foo; $a="Bar"; &{$a};'
: Undefined subroutine &Foo::Bar called at -e line 1.
: cthulhu:~$ perl -le 'package Foo; $a=",Bar"; &{$a};'
: Undefined subroutine &main::,Bar called at -e line 1.
: cthulhu:~$

: Note how the missing subroutine is suddenly expected to be in main::
: rather than Foo:: when I let the name start with a non-alphanumeric
: character. I'm not calling this a bug or anything, since I know I'm
: far into "Don't _do_ that!" territory, but I'm curious about why it
: happens. Would someone here care to enlighten me?

perlmod.pod says

        Only identifiers starting with letters (or underscore) are stored
        in a package's symbol table.  All other symbols are kept in
        package C<main>, including all of the punctuation variables like
        $_.  

Since the name of the sub starts with a punctuation mark, Perl expects to
find it in the main package.



Thu, 24 Oct 2002 03:00:00 GMT  
 Symbolic reference to function in wrong package?

Quote:

> : cthulhu:~$ perl -le 'package Foo; $a="Bar"; &{$a};'
> : Undefined subroutine &Foo::Bar called at -e line 1.
> : cthulhu:~$ perl -le 'package Foo; $a=",Bar"; &{$a};'
> : Undefined subroutine &main::,Bar called at -e line 1.

> perlmod.pod says

>    Only identifiers starting with letters (or underscore) are stored
>    in a package's symbol table.  All other symbols are kept in
>    package C<main>, including all of the punctuation variables like
>    $_.  

perlmod is actually lying here, because it is possible to have a
subroutine named something like Foo:,comma whose name begins with a
comma and yet is nevertheless stored in the symbol table for package
Foo, rather than for package main.

What is really going on is more general.  Whenver a name is resolved
to an object of any sort, the logic looks something like this:

        if (the name contains a package qualifier) {
          look for it in that package
        } else if (the name begins with a letter, digit, or _) {
           look for it in the current package
        } else {
           look for it in the default package, 'main'
        }

I left out a number of exceptions.  For example, in the middle case,
there are special exceptions for names like STDIN, ARGV, ENV, and _,
which are looked up in main instead of the current package.

This resolution applies in the normal case, where you jsut wrote

that Calle brought up.  

So the statement from perlmod that

Quote:
>    Only identifiers starting with letters (or underscore) are stored
>    in a package's symbol table.

is not quite correct, because if you mention the package name
explicitly, as with ${"Foo::!!!!"}, Perl always looks in the package
you mentioned, in this case Foo, and never in main.  But you were
right to point oiut this part of the manual,m because in Calle's case
there is no explicit package name, and so names that do not begin with
a \w character are indeed looked up in main::.

A look at the section of `perlvar' titled "Technical Note on the
Syntax of Variable Names" may be enlightening.  The relevant code is
the gv_fetchpv function of perl, which can be found in the vicinity of
line 514 of gv.c.



Thu, 24 Oct 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. BDE Engine error: Index file out of date...

2. Am I wrong about Symbolic References?

3. symbolic references to built-in functions

4. use strict and symbolic function references

5. WeB Sites PLEASE????

6. TTable with Filter capability

7. CODE reference to member function of package Confusing..

8. symbolic reference problem??

9. perlref - Symbolic references

10. should be Re: Symbolic Array reference

11. Symbolic Array reference

12. Q: Prob with symbolic reference to obj method stored in array

 

 
Powered by phpBB® Forum Software