Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I evaluate shell variables in a string?

Tags:

perl

In my Perl script I get strings of file paths that may contain environment variables, e.g. $FONTS/test.ttf or $TMP/file.txt.

I now want to open those files like this:

open my $handle, "<$filename" or die $!;

How can I now expand the environment variables prior to the open call, as e.g. the bash shell would do?

like image 470
Fabian Avatar asked Aug 16 '10 13:08

Fabian


1 Answers

If the environmental variables are set, you can use a simple substitution:

$filename =~ s/\$(\w+)/$ENV{$1}/g;
like image 187
Eugene Yarmash Avatar answered Oct 16 '22 19:10

Eugene Yarmash