Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Reading variables from a txt file

by Perl_Padawan (Initiate)
on Jul 07, 2012 at 20:44 UTC ( #980513=perlquestion: print w/replies, xml ) Need Help??
Perl_Padawan has asked for the wisdom of the Perl Monks concerning the following question:

I am an intermediate perl user and I am trying to create an web site management interface for my church website that other people will be able to use. I want to have a txt file that I can read and write to that will have editable content. Then I want the cgi to read the variable strings in the txt file and spit out html.

I want to have a txt file like this:

$box_1="random content...";

$box_2="random content...";

$box_3="random content...";

Then use it like this:


print $box_1;

print $box_2;

print $box_3;

Is there a way to use the raw data in the txt file as the string variables instead of opening the file, reading the contents, splitting them and assigning each one to a variable?

Replies are listed 'Best First'.
Re: Reading variables from a txt file
by davido (Archbishop) on Jul 07, 2012 at 20:52 UTC

    It's easy to think of Data::Dumper as just a debugging and development tool; a means of visualizing data structures. But it's also a means of serializing data so that it can be eval'ed back into existence later on. Take a look at Data::Dumper's POD, and you'll find it does what you're asking for.

    However, you might find it more maintainable to just use your text files as text files, and read them in. That eliminates the potential for someone editing a file in a way that creates a global variable you weren't expecting to see when you later eval the document.


Re: Reading variables from a txt file
by jethro (Monsignor) on Jul 07, 2012 at 22:09 UTC

    I hope you don't really write something like this

    print $box_1; print $box_2; ...

    and instead have code similar to this:

    print @box; #or print join("\n",@box);
Re: Reading variables from a txt file
by cavac (Deacon) on Jul 08, 2012 at 12:17 UTC

    Yes, you can use require or eval. I'm doing eval in my adventure game demo 980155 although here the file is "obfuscated" as well (base64 encoded) to avoid spoilers.

    Remember, this generally unsafe for user supplied content, as this will execute all Perl code in the file, not just the variables (which would still be unsafe).

    Sorry for any bad spelling, broken formatting and missing code examples. During a slight disagreement with my bicycle (which i lost), i broke my left forearm near the elbow. I'm doing the best i can here...
Re: Reading variables from a txt file
by BillKSmith (Vicar) on Jul 07, 2012 at 23:07 UTC

    Consider using a .ini file. A quick search of CPAN reveals several modules for reading and writing in this format. Sorry, I have no practical experience to help you choose.

Re: Reading variables from a txt file
by cheekuperl (Monk) on Jul 08, 2012 at 03:23 UTC
    require the file in which you have written the variables.
Re: Reading variables from a txt file
by aitap (Curate) on Jul 08, 2012 at 10:48 UTC
    You can `do` any valid Perl file (as it's described in perldoc -f do). This can be unsafe, though. You may want to use some data-storing modules (like YAML, for example) instead.
    Sorry if my advice was wrong.
Re: Reading variables from a txt file
by flexvault (Monsignor) on Jul 08, 2012 at 12:36 UTC

    Welcome Perl_Padawan,

    If your "file" needs to be displayed as HTML, then you might find a previous post of mine interesting. Please see

    What it allows you to do is keep the HTML, CSS, etc. external to your program and once you call the 'sub' in your code, it replaces your tokens with the dynamic data that you built with your Perl script. This keeps the Perl clean and the HTML, etc in a separate file that can be modified without touching the Perl code. The more I use the technique, the more I appreciate the Monk that put it on PM.

    Good Luck

    "Well done is better than well said." - Benjamin Franklin

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://980513]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-06-22 02:17 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.