Drawing inspiration from a couple of different sources I decided to try something and it doesn't work quite like I expected it to.

Heres the deal. I have a data file as such:

my $fee="FEE" my $fi="FI" my $fo="FO" my @fum=qw / fee fi fo fum /
and I have a very simple Perl script as such:
use Data::Dumper; use warnings; open(DAT,"< datafile.txt") or die $!; while (my $line=<DAT>){ chomp $line; eval $line; if ($@){ print $@; } } print Dumper($main::fee,$fi,$fo,@fum);

That's not the code I started off with, it is pretty much what it morphed into as I tried to make this thing work. What I was expecting to have happen was the variables  $fee,$fi,$fo and @fum to be defined as a result of reading in the data file and feeding each line through eval. Reading the perldoc eval and looking at the man page for Inline::Files of all things led me to believe this should work. Part of my inspiration was to come up with a clever answer to this question and so far I've failed to using this method. And indeed a strange thing happened on the way to enlightenment.

New before you yell at me, I know I don't have use strict; in there. That was by choice as part of my intellectual excersize. When the code runs I see the following:

perl loadData.pl Name "main::fum" used only once: possible typo at loadData.pl line 15. Name "main::fo" used only once: possible typo at loadData.pl line 15. Name "main::fi" used only once: possible typo at loadData.pl line 15. Name "main::fee" used only once: possible typo at loadData.pl line 15. $VAR1 = undef; $VAR2 = undef; $VAR3 = undef;
In one set of lines I'm being told the the variables are only being used once during the scope and the other set are telling me the variables are not being set at all! Huh?

So, I turn on strict to dig at this further and I see:

[pberghol@cowdawg ext-files]$ perl loadData.pl Global symbol "$fi" requires explicit package name at loadData.pl line + 16. Global symbol "$fo" requires explicit package name at loadData.pl line + 16. Global symbol "@fum" requires explicit package name at loadData.pl lin +e 16. Execution of loadData.pl aborted due to compilation errors.
OK.. I'm seeing compile time errors... that makes sense.. sorta... Let's dig ourselves deeper and set the package names for all those variables. I'm back to the same complaints again:
perl loadData.pl Name "main::fum" used only once: possible typo at loadData.pl line 16. Name "main::fo" used only once: possible typo at loadData.pl line 16. Name "main::fi" used only once: possible typo at loadData.pl line 16. Name "main::fee" used only once: possible typo at loadData.pl line 16. $VAR1 = undef; $VAR2 = undef; $VAR3 = undef;

What in the world is going on here?


In reply to eval not behaving like I expected... why? by blue_cowdawg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.