On Sat, May 31, 2003 at 10:30:02AM +0200 Kevin Pfeiffer wrote: > In article <[EMAIL PROTECTED]>, Tassilo Von Parseval wrote: > > > On Sat, May 31, 2003 at 01:50:51AM +0200 Kevin Pfeiffer wrote: > > > >> I use a function to return a value: > >> "$ENV{HOME}/perl/qotw/qotw13/mb"; which is then assigned to $mb > >> > >> But the $ENV{HOME} part is not being interpreted, so this sample: > >> > >> my $mb = get_config('mb', '.get_quiz'); > >> print "$mb\n"; > >> exit; > >> > >> produces literally: "$ENV{HOME}/perl/qotw/qotw13/mb" instead of: > >> /home/pfeiffer/perl/qotw/... > > > > Can you post the (potentially) relevant bits of get_config()? Sounds as > > though this function reads in a configuration file and returns the > > requested values. Is "$ENV{HOME}/perl/qotw/qotw13/mb" one such line of > > this config file? If so, it would be data and not code and you'd have to > > eval() it or so. > > Sorry about leaving out the routine: > > sub get_config { > my ($setting, $cfg_file) = @_; # setting is 'mb' > > open DATA, "< $ENV{'HOME'}/$cfg_file" > or die "Error, could not open $cfg_file : $!"; > > while (<DATA>) { > next if (/^[#\s]*$/); > $result = $1 if (/^$setting\s*=\s*"?([^"]*)"?\s*$/); > } > > close DATA; > return $result; > } > > Data file looks like: > mb = "$ENV{'HOME'}/perl/qotw/qotw13/mb"
Ah, there's the delinquent. Indeed, it's data and not (yet) code. > It already occurred to me that I might need an eval and I've tried using it > in every position and way possible it seems, but the env variable still > doesn't get interpreted. In your case, you have to build an expression that, when evaluated, returns the expanded string. That's not so hard...all you have to watch is the use of quotations: my $val = q!$ENV{'HOME'}/perl/qotw/qotw13/mb!; my $mb = eval qq! "$val" !; print $mb; Bear in mind that simply evaling $ENV{HOME}/etc./.../ wont work because it is not a proper Perl expression. You need to add double-quotes to turn it into one. But I guess you already know that evaling strings from a config-file could be a problem, depending on the context in which you use it. It's totally ok, though, if you have control over the content of the file. Still, you could also put mb = "~/perl/quotw/qotw13/mb" and expand the tilde. 'perldoc -q tilde' comes up with a regex-solution. Using glob (which is also mentioned) is more straightforward, however: my $mv = glob('~/perl/qotw/qotw13/mb'); will do the right thing. Tassilo -- $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({ pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#; $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]