daveg
daveg

Reputation: 1211

Can't access shell variable in perl script unless it's an environment variable

I need to access the value of a shell variable in a perl script. I can get it if it's an environment variable but not a regular ole shell variable. Consider

% set AAA=Avalue
% setenv BBB=Bvalue
% echo $AAA
Avalue
% echo $BBB
Bvalue

So far, so good. Now, here's a perl script to read them...

#!/usr/bin/env perl
use strict;

print "AAA: ".$ENV{AAA}."\n";
print "BBB: ".$ENV{BBB}."\n";

exit;

When I run it, I get...

AAA:
BBB: Bvalue

How can I get the value of AAA from inside the perl script ?

Thanks in Advance

Upvotes: 2

Views: 990

Answers (2)

Dev123
Dev123

Reputation: 293

You are not getting the value of AAA variable because AAA is local env variable where as BBB is exported variable.

Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only.

Example:

$ set AAA=123
$ csh
$ echo $AAA
AAA: Undefined variable.
$ exit

$ setenv BBB 456
$ csh
$ echo $BBB
456

Upvotes: 3

ikegami
ikegami

Reputation: 386551

A process doesn't have access to another process's memory, much less its variables. If you want your Perl script to have a value, you will need to pass the value to it somehow (e.g. via its environment).

Upvotes: 1

Related Questions