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?
If the environmental variables are set, you can use a simple substitution:
$filename =~ s/\$(\w+)/$ENV{$1}/g;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With