Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-09-28 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?