Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Reading and producing variable lists (w/o hashes!)

by bingohighway (Acolyte)
on May 01, 2009 at 09:44 UTC ( [id://761239]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I am trying to read in a list of variables from a text file e.g. $var1, $apple, $pear and having these variables useable in my code. I've tried grabbing the text and using something like $$array[0] from the extracted variables list, and I've tried using eval(). None seem to work.

I can do this with hashes but would like to do this with without! I am also fully aware that not using  strict may come back to haunt me.

Cheers!

Replies are listed 'Best First'.
Re: Reading and producing variable lists (w/o hashes!)
by why_bird (Pilgrim) on May 01, 2009 at 10:23 UTC

    I believe what you're trying to do is read in a bunch of names from a file, and then have those names available to use as variable names in your code? Is that right?

    I remember once trying to do something similar, and eventually convincing myself that it was a struggle to do for a reason. For a start, it's a huuuuge security risk, which makes it bad practice, whether or not this code is going to be used by anyone else.

    Perhaps this is an example of an X-Y problem, where you're not telling us what you're trying to achieve in the long run, or why you've chosen this particular method. Or maybe you're just doing it for the hell of it, because it seems like a fun thing to do, or just to find out if it can be done.

    If the former, if you can outline your goal and what you've tried so far, I'm sure people will give you great suggestions as to how to proceed. This is the case in which not using 'use strict' may well come back to haunt you, unless you really know what you're doing! If the latter, good on you, and again I'm sure people will help you out.

    I know this wasn't much practical help, but I think I'm echoing what was said above by almut, in that the more detail you give, the better and more useful people's answers will be.

    I for one am curious about what you want to use this for!

    why_bird

    update:added link

    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......
      I have a series of templates in a pseudocode style, used for config file generation for repeated and varying user setups. The pseudocode style has a pre-processor command style, e.g.
      #define $var = "hello" #if($var = "hello") include this text in config file also include the word $var as an inserted word #endif
      I have managed to get the evaluation of the #if statements sorted, but the actual definition of the variables is a bit more of a pain.

      The reason for not using hashes is to avoid having to process each line of config text, looking for the $variable. Also, using the #if statements with hashes, I'd have to process the $variable and turn it into $list_of_variables->{$variable}.

      Does that make sense? Do you agree? :-)

        bingohighway:

        Not to be difficult, but why not use cpp instead of reinventing the wheel? Or perhaps a templating engine (there's one or two on CPAN, I hear)?

        ...roboticus
        I agree with roboticus about the templates and would add that there are one or two config modules that are more than happy to generate files too.

        So, imho, it would be wise to at least have a look at the prior art.

Re: Reading and producing variable lists (w/o hashes!)
by CountZero (Bishop) on May 01, 2009 at 10:50 UTC
    But it does not run under use strict;:
    while (<DATA>) { eval $_; } print "$apple, $pear, $orange\n"; __DATA__ $apple = 'green'; $pear = 'sweet'; $orange = 'healthy';
    And unless you fully control the input, the security holes are wide enough to drive a large dump truck through.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Yep, totally agree on the security holes :-)
Re: Reading and producing variable lists (w/o hashes!)
by almut (Canon) on May 01, 2009 at 09:51 UTC

    I think it would be easier for us to understand what exactly you're trying to do, if you posted the complete snippet of code you've tried, together with some sample input and a description of what you'd expect to happen...

      ok, massively simplified example:
      my $one = ("$apple", "$pear"); my $two = "$orange"; $$one->[0]= "hello"; $$two = "goodbye"; print "$apple $orange";
      Neither of these work. I know it is something simple I am not doing.

      Cheers!

        Maybe something like this?

        my @one = ('$apple', '$pear'); my $two = '$orange'; eval "$one[0] = 'hello'"; # evaluates $apple = 'hello' eval "$two = 'goodbye'"; # evaluates $orange = 'goodbye' print "$apple $orange"; # hello goodbye

        (Note the single quotes in '$apple' etc. With double quotes, the (empty) global variable $apple would be interpolated.)

        Update: Another way would be to use symbolic references:

        use strict; use warnings; my @one = qw($apple $pear); my $two = '$orange'; our ($apple, $pear, $orange); { no strict 'refs'; ${substr $one[0],1} = 'hello'; # ${'apple'} = 'hello'; ${substr $two,1} = 'goodbye'; # ${'orange'} = 'goodbye'; } print "$apple $orange"; # hello goodbye

        (You'd need package variables and no strict 'refs' in this case, though)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-23 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found