Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Accessing variables in an external hash without eval

by huck (Prior)
on May 17, 2017 at 03:33 UTC ( [id://1190418]=note: print w/replies, xml ) Need Help??


in reply to Accessing variables in an external hash without eval

As kevbot said your example is flawed.

This works for me

use strict; use warnings; use Data::Dumper; my %hash; my $loadedFile = join "", <DATA>; { no strict; eval ($loadedFile); if ($@) {print 'eval error:'.$@."\n"; } } my @hashData = $hash{Scope}{model}; my @dereferencedData; foreach my $line (@hashData){ @dereferencedData = @$line; } print "Data: \n"; print Dumper @dereferencedData; print "\n"; __DATA__ %hash = ( #scope changes Scope => { model => [ 1,2,3,4,5,6 ] }, IrreleventScope =>{ a=>'b' } ) ;
Result
Data: $VAR1 = 1; $VAR2 = 2; $VAR3 = 3; $VAR4 = 4; $VAR5 = 5; $VAR6 = 6;
Notice the addition of a semi to the end of your example data

Replies are listed 'Best First'.
Re^2: Accessing variables in an external hash without eval
by victorz22 (Sexton) on May 17, 2017 at 05:12 UTC

    Thank you but the solution didnt work for me, I still get an uninitialized variable for my @hashData. However i was able to get your code to load in $loadedFile.

      If it did not work for you (but you don't explain i which respect it did not work) and worked for huck, then it is likely there's something wrong in the code you did not show, perhaps in the hash initialization you replaced with pseudo-code.

      I still get an uninitialized variable for my @hashData.

      Then it seems obvious that you're not loading a value with this line:

      my @hashData  = $hash{Scope}{model};

      Side note: This should probably read more like:

      my @hashData  = @$hash{Scope}{model};

      Never assume there is data where you expect it to be when it is coming from an outside source. Check it first. For example:

      my @hashData = (); if (!defined $hash{Scope}{model}) { print "Scope and model missing!\n"; } else { @hashData = @$hash{Scope}{model}; }

      ...or something of that ilk.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found