Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: rel2abs of a path with env

by stevieb (Canon)
on Jun 07, 2021 at 14:14 UTC ( #11133619=note: print w/replies, xml ) Need Help??

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";


spek@scelia ~/scratch $ perl /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 #1 Sun Aug 2 19:46:04 2020 +x64'


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

      SKMDSK06::T:\>perl 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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11133619]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-05-29 05:12 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.