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

This one should be easy but I'm at a loss....

pacakge Foo has a global array @A_GLOBAL I use pacakge Foo like this:

my $foo = Foo->new;

I can access @A_GLOBAL in Foo like this:

my @array = @Foo::A_GLOBAL

I don't understand why when I do this:

my @array = $foo::A_GLOBAL

I don't get any results... I'm missing something fundamental here and just can't seem to get it. Any ideas?

THanks.

Replies are listed 'Best First'.
(ar0n) Re: Accessing Other Globals?
by ar0n (Priest) on Dec 15, 2001 at 02:40 UTC

    It's because $foo::A_GLOBAL is equivalent to getting the scalar A_GLOBAL in the package foo (which of course does not exist).


    update Now you're accessing an array A_GLOBAL in the package foo, not Foo. Perl is case sensitive, after all.

    [ ar0n -- want job (boston) ]

      update Now you're accessing an array A_GLOBAL in the package foo, not Foo. Perl is case sensitive, after all.

      I should explain myself better. I have an object $foo but don't know for sure what pacakge it is from. How do I access it's globals?
        my $package = ref($foo); if ($package) { no strict 'refs'; @global_array = @{"$package::A_GLOBAL_ARRAY"}; }
        Please note that using global arrays is a sign that you have no idea how to correctly program your design (if you even have one). This is especially true if you're doing OO programming.

        If you want to work with "global arrays", look into using Exporter.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Ooops tat was a type that should be:

      my @array = @foo::A_GLOBAL

      Which fails with no error...
        Perl is case-sensitive. 'foo' is not the same as 'Foo'.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Accessing Other Globals?
by blakem (Monsignor) on Dec 15, 2001 at 02:48 UTC
    Seems to work fine for me:
    #!/usr/bin/perl -wT use strict; package Foo; use vars qw(@foo); @foo = qw(a b c); # global package variable @Foo::foo package Bar; my @bar = @Foo::foo; # assigned to lexical variable print "@bar\n"; # <==== prints 'a b c'

    -Blake

Re: Accessing Other Globals?
by strat (Canon) on Dec 17, 2001 at 18:39 UTC
    Btw: with ref($variable), you can find out about the class it is blessed in...

    Best regards,
    perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"