Code does work in different packages only

Discussion in 'Perl Misc' started by Ferry Bolhar, Jun 25, 2006.

  1. Ferry Bolhar

    Ferry Bolhar Guest

    Hi folks,

    While looking at the code in vars.pm (the "use vars" pragma), I tried to
    reproduce its behaviour:

    use strict;
    use vars $x;
    $x = 3;

    by writing:

    use strict;
    BEGIN {
    *main::x = \$main::x;
    }
    $x = 3;

    instead. However, this code doesnt work, I get the messages

    Variable "$x" not imported at line 5
    Global symbol "$x" requires explicit package name at line 5

    (Line 5 contains the assignment to scalar $x)

    However, when including a "package hugo;" in the BEGIN block (or even
    outside, and a "package main;" thereafter), the code works as expected. It
    also works when the BEGIN block runs in package "main" and the scalar
    assignment in a different package. It fails, whenever both assignments
    (typeglob and scalar) are executed in the same package.

    Can someone explain why? The typeglob assignment occurs full-qualified, so
    it shouldn't matter which package is currently active. However, it does.
    Why?

    Thanks for your answers and kind greetings from Vienna,

    Ferry
     
    Ferry Bolhar, Jun 25, 2006
    #1
    1. Advertising

  2. Ferry Bolhar

    Anno Siegel Guest

    Ferry Bolhar <> wrote in comp.lang.perl.misc:
    >Hi folks,
    >
    >While looking at the code in vars.pm (the "use vars" pragma), I tried to
    >reproduce its behaviour:
    >
    >use strict;
    >use vars $x;
    >$x = 3;
    >
    >by writing:
    >
    >use strict;
    >BEGIN {
    > *main::x = \$main::x;
    >}
    >$x = 3;
    >
    >instead. However, this code doesnt work, I get the messages
    >
    >Variable "$x" not imported at line 5
    >Global symbol "$x" requires explicit package name at line 5
    >
    >(Line 5 contains the assignment to scalar $x)
    >
    >However, when including a "package hugo;" in the BEGIN block (or even
    >outside, and a "package main;" thereafter), the code works as expected. It
    >also works when the BEGIN block runs in package "main" and the scalar
    >assignment in a different package. It fails, whenever both assignments
    >(typeglob and scalar) are executed in the same package.
    >
    >Can someone explain why? The typeglob assignment occurs full-qualified, so
    >it shouldn't matter which package is currently active. However, it does.
    >Why?


    That's a documented fact. Unfortunately I forget where exactly it is
    documented, but an assignment to a typeglob only counts as importation
    when if happens in a package that is different from the typeglob's.

    Anno
     
    Anno Siegel, Jun 25, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul  Smith
    Replies:
    0
    Views:
    734
    Paul Smith
    Nov 18, 2003
  2. Davy
    Replies:
    2
    Views:
    416
    Terry Reedy
    Nov 10, 2007
  3. David Lyon
    Replies:
    19
    Views:
    583
    David Lyon
    Apr 24, 2009
  4. Jonas
    Replies:
    0
    Views:
    165
    Jonas
    Sep 23, 2003
  5. Ian
    Replies:
    3
    Views:
    171
Loading...

Share This Page