Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2016-10-26 04:52 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (333 votes). Check out past polls.