http://qs1969.pair.com?node_id=11133619


in reply to rel2abs of a path with env

Like this?:

use warnings; use strict; use Path::Tiny; my $path = path("/a/b/c/$ENV{USER}")->absolute; print "$path\n";

Output:

spek@scelia ~/scratch $ perl path.pl /a/b/c/spek

If not, I fail to realize what it is exactly you're after.

Replies are listed 'Best First'.
Re^2: rel2abs of a path with env
by ovedpo15 (Pilgrim) on Jun 07, 2021 at 14:27 UTC
    Yes that's exactly what I did and it didn't resolve the env. The output of path("/a/b/c/$ENV{USER}")->absolute for me is /a/b/c/$ENV{USER}. Using Perl 5.26.1.
    Edit: I misread. Sorry. Why did you replace /a/b/c/${USER} with /a/b/c/$ENV{USER}?

      All worked here (FreeBSD 13, Perl 5.32, Path::Tiny 0.118) ...

      # perl -Mstrict -Mwarnings -MPath::Tiny -E \ 'say join qq[\n], map path( $_ )->absolute(), q[~/tmp], q[~tester/tmp], qq[/tmp/$ENV{USER}] ' /home/parv/tmp /home/tester/tmp /tmp/parv

      Very odd. Thinking to step ouside of Linux, I tested this on Windows. It appears to have worked correctly for me as well.


      SKMDSK06::T:\>perl -V | findstr uname uname='Win32 strawberry-perl 5.32.0.1 #1 Sun Aug 2 19:46:04 2020 +x64'


      SKMDSK06::T:\>set user USERDOMAIN=SKMDSK06 USERDOMAIN_ROAMINGPROFILE=SKMDSK06 USERNAME=Steve USERPROFILE=C:\Users\Steve


      SKMDSK06::T:\>type test01.pl use strict; use Path::Tiny; my $path = path("/a/b/c/$ENV{USER}")->absolute; print "$path\n";


      SKMDSK06::T:\>perl test01.pl T:/a/b/c SKMDSK06::T:\>


      Not sure that this helps any, but there it is.

      Perl is not bash, and has different interpolation syntax. If you want to interpolate the value of an environment variable into a string in Perl, you say $ENV{USER}, not $USER.

      As to why you got a literal /a/b/c/$ENV{USER}, are you sure you used "/a/b/c/$ENV{USER}" and not '/a/b/c/$ENV{USER}'? The single quotes do not interpolate.

      Because env vars are accessed using $ENV{USER}, and there presumably no var named $USER in your program.

      Seeking work! You can reach me at ikegami@adaelis.com