in reply to Perl configuration files
strict is your friend - it'll save you a lot of debugging (mistyped variable names, etc.).
As for using or requiring a file to set variables, you could do that, but you probably don't want to.
What might make sense is to write a function in a second perl script that reads a file containing your parameters and returns them as, say, a hash-reference.
e.g. (not fully tested)
require 'test_03.pl'; my $result = SetVars(); foreach(keys %{$result}){ print "$_ : ${$result}{$_}\n"; }
and then in test_03.pl:
use strict; sub SetVars(){ my %vars; open(VARS, 'myvars.txt')||die "Cannot open myvars.txt:$!\n"; while(<VARS>){ chomp; $vars{(split /:/)[0]} = (split /:/)[1]; } return \%vars; } return 1;
|
|---|