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

Yah, I know... the title makes no sense. Maybe part of my problem is because I can't get properly squared away on what the problem actually is... anyway: I have a script that is reading a huge list of hardcoded crap (and I really do mean *crap* here) from a seperate perl script to convert this to some semblance of a reasonable data scheme. The line in particular that is causing the problem is roughly akin to:
@{ $blah{ foo } } = ( 100 .. 104 , 105 , 106 .. 123 ); @{ $blah{ bar } } = qw| 100 101 102 103 |;
And what I'm doing is basically parsing those lines to slap the info into a seperate data structure. My problem is that I can't eval the right side list values into a list. I realize there are other ways to go about this ("Uh, how about the first script just dumps the data structure somewhere?"), but now that I've tried to do it the first way off the top of my head and failed, I'm intrigued... and irritated. So, what am I missing here?

Replies are listed 'Best First'.
Re: List as a string, eval'd back to the list?
by bbfu (Curate) on Mar 30, 2001 at 02:41 UTC

    As long as you're certain that the data lines are as (more or less) simple as the ones you've posted (ie, the keys to the hash are never strings that contain a '='), why can't you just eval the RHS?

    $line = '@{ $blah{ foo } } = ( 100 .. 104 , 105 , 106 .. 123 );'; $line =~ s/^[^=]+=//; @list = eval $line;

    Of course, this uses string-eval (on "user" data, even!), which is a Bad Thing. It's also easy to break. I think you really would be better off if you can get the other script to print the list as a CSV string. It'd be easy to do, and then you could just split said string and avoid all this eval-evil.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      Hmm... welp, the reason I was not seeing eval doing what I expected was because I was doing this:
      eval { $line };
      not this:
      eval $line;

      And I wholeheartedly agree... doing it this way is stupid. It was just the first thing I thought of, and got grumpy when things weren't behaving as I expected. eval EXPR != eval BLOCK. Right.

      Thanks for the assists...
Re: List as a string, eval'd back to the list?
by princepawn (Parson) on Mar 30, 2001 at 01:28 UTC
    My problem is that I can't eval the right side list values into a list.

    Can you give an example of what you want to do but can't? I don't know what you want to do exactly.