Addressing a PACKAGE, sincerely confused:( 
Author Message
 Addressing a PACKAGE, sincerely confused:(

While hacking on my latest perl project, I found the need to manipulate
variables within a package.  The following works:

        $goo = $foo'bar;

This stores the contents of variable bar in package foo into goo.

So far so good. But suppose the package name was stored in a variable
say:

$package="foo";

how do address bar now?  For example

$goo = $$package'bar;

fails miserably

Thanks,

david

--
David W. Bourgoyne                      Texas Instruments Dallas, Tx

****************************************************************************
A wise man said: If it didn't come with source, it's firmware not software :)



Tue, 18 Jun 1996 06:30:55 GMT  
 Addressing a PACKAGE, sincerely confused:(

Quote:
> While hacking on my latest perl project, I found the need to manipulate
> variables within a package.  The following works:

>    $goo = $foo'bar;

> This stores the contents of variable bar in package foo into goo.
> So far so good. But suppose the package name was stored in a variable
> say:

> $package="foo";

> how do address bar now?  For example

> $goo = $$package'bar;

> fails miserably

You can easily do this using 'eval' as in

        eval "\$goo = \$${package}'bar";

You need the {}'s or perl thinks you're trying
to use the package named 'package'.

Looking at this again, this is pretty ugly and
definitely not straightforward -- is there a
"better" way?

Michael D'Errico



Tue, 18 Jun 1996 14:30:59 GMT  
 Addressing a PACKAGE, sincerely confused:(


:>   $goo = $foo'bar;
:
:> This stores the contents of variable bar in package foo into goo.
:
:> So far so good. But suppose the package name was stored in a variable
:> say:
:
:> $package="foo";
:
:> how do address bar now?
:
:Once again, it's Type-glob Man (TM) to the rescue!
:
:*package="foo";
:$goo = $package'bar;
:
:Ooh!  Type-glob Man (TM) is hit!  Globs don't work for packages.
:He makes the tag, and Eval Man steps into the ring.  After a few
:feints, he launches a successful {*filter*}:
:
:$package = "foo";
:eval "\$goo = \$${package}'bar";
:
:The judges call him ugly and slow, but Eval Man is the winner of
:this battle.

You just have to try a mite harder with the type-globs.

    $package  = 'foo';
    $variable = 'bar';
    *goo = "${package}'${variable}";

    $goo = 1;
    print $foo'bar;
    1

--tom
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212



Wed, 19 Jun 1996 07:31:51 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Addressing a PACKAGE, sincerely confused:

2. confused packages use

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

4. confused by full package name and warning

5. package/object confuses each others variables?

6. Can't call method "Mail::Address::address" without a package - in my class

7. Need resolver from IP numeric address to alphabetic address

8. How to find IP address by MAC address?

9. Finding an email address by ip address

10. IO addresses, Memory Addresses and RS232 ports in DOS

11. How to find IP address by MAC address?

12. How to find IP address by MAC address?

 

 
Powered by phpBB® Forum Software