Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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 cooling their heels in the Monastery: (5)
As of 2015-07-04 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls