Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to export hash references

by Anonymous Monk
on Feb 14, 2020 at 11:03 UTC ( #11112960=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to export a reference to a hash but can only access it with a fully-qualified name. What am I doing wrong? Thanks

Package Foo; use strict; use warnings; use Exporter 'import'; our @ISA = qw[Exporter]; our @EXPORT_OK = qw[%env $env]; our %env = %ENV; our $env = \%ENV; 1;

perl -I. -MFoo=%env -le 'print scalar keys %env'
38

perl -I. -MFoo=$env -le 'print scalar keys %$env'
0

perl -I. -MFoo=$env -le 'print scalar keys %$Foo::env'
38

Replies are listed 'Best First'.
Re: How to export hash references
by hippo (Chancellor) on Feb 14, 2020 at 12:05 UTC
    Package Foo;

    This isn't valid. In Perl, package is all in lowercase.

    Your actual problem (once that's fixed) is that you are not escaping the dollar symbol.

    $ perl -I. -MFoo=$env -le 'print scalar keys %$env' 0 $ perl -I. -MFoo=\$env -le 'print scalar keys %$env' 76
      Thanks hippo. That Package thing was a dumb typo, good catch. I'm so used to importing arrays and hashes, without having to escape the sigil, that I forgot about the pesky $hell. Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11112960]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2020-04-08 23:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (46 votes). Check out past polls.

    Notices?