'what the following syntax means in perl?
my $dir = $ENV{"CDSSITE_INI_DIR"};
hello, I'm new to Perl. I'm confused with the curly braces above syntax. I know it is declared a scalar variable $dir. But what do the curly braces mean in that statement?
Solution 1:[1]
Perl populates a global hash (%ENV
) with the environment variables. To get any value, you must use the $ENV{'env-var-name'}
syntax. That is the way to access a concrete value in a hash, using curly braces.
Solution 2:[2]
It's retrieving the value from a hash based on key 'CDSITE_INI_DIR'. But in this case it's a special hash from the OS variables. You can code your own hash such as this:
my %depts;
my $south = 'SOUTH';
$depts{'EAST'} = 123;
$depts{WEST} = 456;
$depts{$south} = 789;
print $depts{WEST} . "\n";
Note: different ways I set the keys. Perl automatically creates a hash called %ENV and puts the environment variables with their values into that hash at startup. Such as $HOME, etc. Hashes (key/value pairs) and arrays serve different purposes. Both are very useful depending on need.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Miguel Prz |
Solution 2 | Keith Gossage |