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

Hello! Is there a way i could create varibles in runtime from STDIN or whereever. For instance: i would read a string from STDIN (let's say user enters "asd"). I would then like to create $asd or @asd or %asd. Is this possible? rgds and thx Rabotnik

Replies are listed 'Best First'.
Re: Runtime variable creation
by davorg (Chancellor) on Sep 27, 2001 at 12:00 UTC

    Yes it's possible, but it's a very bad idea so I'm not going to tell you how.

    For more information on why it's such a bad idea, search this site for information on "symbolic references".

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: Runtime variable creation
by dragonchild (Archbishop) on Sep 27, 2001 at 17:07 UTC
    Use hashes.

    Here's the basic thing - Perl stores all variables in a symbol table, which is just a special kind of hash. If you want to create your own personal symbol table, use your own personal hash.

    Now, you're going to say that hashes can only store scalars as their values, and you'd be right. However, use straight, normal, basic references. Thus, if it's a scalar, you just store it. If it's an array or hash (or even a subroutine!), you store a reference to it, and dereference it as necessary. (Use ref to figure out what's what, if you need to, but you shouldn't.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Runtime variable creation
by IOrdy (Friar) on Sep 27, 2001 at 12:05 UTC
    Why would you want to create alot of variables at runtime when you can just create one that will hold all your values?

    read The Basic Datatypes, Three I think you are looking for @ as Zaxo suggests, or %

    <edit> I'm not saying it can't be done though. </edit>
Re: Runtime variable creation
by Zaxo (Archbishop) on Sep 27, 2001 at 11:52 UTC

    Certainly

    # produces an array of $/ delimited records -- lines ususally my @asd = <STDIN>;
    Further manipulation gets you whatever you can extract.

    Update: Oops, misread the question. Listen to davorg.

    After Compline,
    Zaxo