Perl environment variables

It seems I can access the environment variables in perl by simply doing:

use Env; print ${PATH}; 

Is this the expected behavior?

Env docs say you need to do $ENV{PATH} .

+5
source share
3 answers

Env says

Perl supports environment variables in a special hash named %ENV . If this access method is inconvenient, the Perl Env module allows you to treat environment variables as scalar or massive variables.

So this is the legal and expected use of the variables $PATH , $USER , $HOME , etc.

However this module

By default, it associates all existing environment variables ( keys %ENV ) with scalars.

and I prefer to use the %ENV hash directly rather than its tie elements.

+5
source

Yes, this is the expected behavior due to the interaction of two factors:

  • You used the Env module, which aliases $ENV{PATH} to $PATH .

Note that $ENV{PATH} always available in Perl. use Env just adds aliases to the contents of %ENV , no need to do %ENV :

 $ perl -E 'say $ENV{LANG}' en_US.UTF-8 
  1. ${PATH} is nothing more than a more verbose way of saying $PATH . The ${...} construct (and its cousins, @{...} and %{...} ) are most often used to interpolate inside double-quoted strings to make Perl recognize all contents {...} as a name a variable, not a shorter name followed by literal text, but the syntax can also be used in other contexts.

A simple demonstration of this:

 $ perl -E '$foo = "bar"; say ${foo}' bar 
+1
source

Use $ ENV predefined variable format in perl

 print $ENV{PATH}; 
0
source

Source: https://habr.com/ru/post/1271086/


All Articles