Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Multi line file to variables

by sundialsvc4 (Abbot)
on Dec 30, 2014 at 15:47 UTC ( [id://1111734]=note: print w/replies, xml ) Need Help??


in reply to Multi line file to variables

I would strongly advise that, instead of “creating variables,” you should always create “keys in a hash.”   The namespace of “variables in the currently-running program” should never be polluted by anything that is read from an external data-source.   (This was probably the worst idea that PHP ever came up with ...)   The trouble is that, if the name matches an existing variable, and is unthinkingly permitted to replace it, the program will either be crashed or, infinitely worse, deliberately exploited.   In any case, such a program can never be reliable.

You should write the program to read the file line-by-line, then, say, split() each line into “key” (formerly known as “variable-name” ...) and “value” components.   Then, $hashref->{$key} = $value;.   Now, the file can pretty-much contain anything that it wants, because the only thing that will actually be affected ... or that can possibly be affected, is:   the set of keys in $hashref.   Not the program’s own variable-pool.

Replies are listed 'Best First'.
Re^2: Multi line file to variables
by AnomalousMonk (Archbishop) on Dec 30, 2014 at 16:57 UTC

    I think what jackierobs12 had in mind was to assign the two lines to two already defined variables (i.e., 'var1' and 'var2' from the OP) rather than creating new global variable symbols based on input data, which I agree is a Bad Idea.


    Give a man a fish:   <%-(-(-(-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-18 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found