Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Answer: Reading Variables from a File

by msemtd (Scribe)
on May 30, 2003 at 18:48 UTC ( #261917=categorized answer: print w/replies, xml ) Need Help??

Q&A > input and output > Reading Variables from a File - Answer 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("");
Which will run 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 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"; }

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-06-24 11:12 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.