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

Answer: Reading Variables from a File

( #261917=categorized answer: print w/ replies, xml ) Need Help??

Q&A > input and output > Reading Variables from a File contributed by msemtd

I quite often provide Perl apps compiled into executables with perl2exe for deployment on systems without Perl installed. As such I always provide a back-door means by which the configuration can be adjusted e.g.: -
use strict; my $cake = 'chocolate'; my $pie = 'apple'; eval slurp("config.pl");
Which will run config.pl at run time assuming a nice handy slurp function is provided to read a file...
## slurp - read a file into a scalar or list sub slurp { my $file = shift; local *F; open F, "< $file" or die "Error opening '$file' for read: $!"; if(not wantarray){ local $/ = undef; my $string = <F>; close F; return $string; } local $/ = ""; my @a = <F>; close F; return @a; }
So when config.pl contains...
$pie = 'pecan'; cake = 'fairy';
...the pie and cake get set to your favourites rather than the defaults.

If you are interested in errors from the eval, it can be followed by something like...

if($@){ print "Some errors happened...\n\n"; print $@."\n\n"; print "Oh well...\n"; }

Comment on Answer: Reading Variables from a File
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-09-16 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (51 votes), past polls