Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Eval/package question

by Ratazong (Monsignor)
on Jan 21, 2020 at 11:33 UTC ( #11111675=note: print w/replies, xml ) Need Help??


in reply to Re: Eval/package question
in thread Eval/package question

Thanks a lot, GrandFather and Eily!

I have a generic configuration in an Excel-file, and move it to several hashes by creating the commands as strings and eval-ing them. It works in my stand-alone-script, but doesn't any longer when moving the code into the package (the package variable (%items above) is not available). So my example above did not reproduce my issue - and I'll have to search further.

Nevertheless, I learned something today :-)

Have a great day, Rata

Replies are listed 'Best First'.
Re^3: Eval/package question
by haukex (Chancellor) on Jan 21, 2020 at 19:05 UTC
    I have a generic configuration in an Excel-file, and move it to several hashes by creating the commands as strings and eval-ing them.

    As others have mentioned, stringy eval is often not a good idea. Especially when working with hashes, what's wrong with the following?

    our %items; my $item = '3'; my $key = 'Value'; my $value = 2; $items{$item}{$key} = $value;
Re^3: Eval/package question
by shmem (Chancellor) on Jan 21, 2020 at 14:38 UTC
    (the package variable (%items above) is not available).

    It is not, because %items is declared as a my variable and thus available only in the the package itself, not in the main package.

    If you have to access it from main, you either have to fully qualify it as %items::items, or provide methods to access it (exporting it, providing accessors or a method which returns a reference to it, etc), but it's hard to say what's best without knowing the whole scenario.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      my variables are local to the lexical scope in which they are declared, and can not be accessed outside that scope.

      If you want to access %items outside the file in which it is declared, you need to specify our %items, not my %items. Then you can access it by its fully-qualified name. That is, if it is declared in package Foo you say (e.g.) $Foo::items{Dent} = 'Arthur';.

      In an ideal world the OP would not be mucking around with global variables, but this is not an ideal world.

Re^3: Eval/package question
by jcb (Priest) on Jan 22, 2020 at 00:29 UTC
    I have a generic configuration in an Excel-file

    Have you considered exporting that as CSV and using Text::CSV to load the data?

    (Hint: Data::Dumper will tell you what you have at intermediate stages if you are unsure.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11111675]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2020-05-30 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (171 votes). Check out past polls.

    Notices?