1

I have given a file testconf.pm , with nothing else in it but

my $item = {

'default'   =>     {

    'General'       =>    { 'item1' => 'config1',
                            'item2' => 'config2'
                          }
                   }
           };

In a second file main.pl , i want to load the hash for next processing steps, I tried some stuff like

use testconfig;
use Data::Dumper;
my $config;
$config = testconfig::item;
print Dumper $config;

But i could not get to use the data from testconf. Unfortunately, i am not able to extend testconf.pm with an exporter or a packagedeclaration, using our instea of my and so on, as this file has to stay like this. How could i get the values out of item in main.pl (btw. i Need to Access the data, not only Dumping it )

chenino
  • 454
  • 2
  • 7
  • 19
  • typo, already corrected it. thanks – chenino Jan 23 '15 at 15:31
  • 1
    "Unfortunately, i am not able to extend testconf.pm with an exporter or a packagedeclaration, using our instea of my and so on, as this file has to stay like this." What kind of project are you on, that stops you from fixing basic architectural problems like this? – Dave Cross Jan 23 '15 at 15:59
  • basically, someone else build a huge program, this is a config file for this program and i am just a small someoneelse who has to use this file and can't change it to not kill the old huge program – chenino Jan 23 '15 at 16:01
  • My advice: Stick a ticket on the backlog to fix this monstrosity and then start looking for a better job :-) – Dave Cross Jan 23 '15 at 16:03
  • Well, as everything else is fine in my job, i guess i can live with that ;) – chenino Jan 23 '15 at 16:12

2 Answers2

7

You specifically told Perl to limit the scope of the variable (where it's seen) to the file, so you can't.

If that's the entire file, you could rely on the fact that the assignment to $item is the last statement of the file by changing

do("testconf.pm")
   or die($@ || $!);

to

my $item = do("testconf.pm")
   or die($@ || $!);
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Well, it was not me who specified it. This file is given to me, somehow they read it too. But i don't know how. – chenino Jan 23 '15 at 15:33
  • 1
    PS - It's not appropriate to give files that aren't Perl modules the `.pm` extensions – ikegami Jan 23 '15 at 15:44
  • Well, i am not completely sure who to give the kudos with an correct answer mark, cause you both gave great Support. Ben Grimm on dereferencing and first help, you on an more elegant way. So who to give the mark? ( well ikegami, i know what you mean....but this side of the code does not lay in my hands, so i have to accept whats given) – chenino Jan 23 '15 at 15:53
  • There is more than one way to do it, but this answer is the correct one. – Ben Grimm Jan 23 '15 at 15:55
  • so with your acceptance Ben, i flagged this one as answer. But i am thankful to you too! – chenino Jan 23 '15 at 16:02
2

If the file were not structured to allow do to work, you could read the file, make any necessary changes and eval:

open my $fh, '<', 'testconfig.pm';
$/ = undef;
my $testconfig = <$fh>;
# changes to the $testconfig source go here
my $config = eval $testconfig;
Ben Grimm
  • 4,316
  • 2
  • 15
  • 24
  • Not saying it makes sense, but the OP does say "i am not able to extend testconf.pm with an exporter or a packagedeclaration, **using our instea of my** and so on, as this file has to stay like this." – ThisSuitIsBlackNot Jan 23 '15 at 15:27
  • as i said, i can't change anything in testconfig.pm . THisSuitIsBlackNOt, yeah I know if I could change anything, it would be easier, but i somehow have to accept this file as given. Ben, this was a typo of mine , corrected the comma – chenino Jan 23 '15 at 15:28
  • Well, this gets me one step closer, but i would have to use it as a hash. With your hack, i cannot use it as a hash – chenino Jan 23 '15 at 15:39
  • 1
    It's read in as the hashref that it is in the file, you can simply dereference it as %$config - or directly e.g. $config->{default} – Ben Grimm Jan 23 '15 at 15:42
  • 1
    Your code is indeed a hack because it's a reimplementation of `do`. Other than that, it's not a hack since he should be using `do` instead of `use`/`require` anyway! – ikegami Jan 23 '15 at 15:46
  • I haven't used `do` since perl4, but it certainly makes for a cleaner answer. :) – Ben Grimm Jan 23 '15 at 15:48
  • dereferencing was the key. i think i need to get my time to learn this. Thank you very much Grimm! – chenino Jan 23 '15 at 15:52
  • @Ben Grimm, That's cause your modules have had `package` statements since Perl 4. The OP's file doesn't have a `package` statement, so [`require` isn't appropriate](http://stackoverflow.com/a/6379109/589924). – ikegami Jan 23 '15 at 16:12
  • Re "dereferencing was the key." There's no dereferencing in our answers. – ikegami Jan 23 '15 at 16:13