Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the path in a perl program

Tags:

perl

I have a bluehost server setup and am trying to set the path in my perl program

 print "Content-type: text/html\n\n";
    my $output=`export PATH=\${PATH}:/usr/local/jdk/bin`;
    my output1=`echo \$PATH`;
    print $output1;

However it stil prints only the orginal $PATH. The /usr/local/jdk does not get added. Can anyone tell me what i am doing wrong?

like image 208
user1092042 Avatar asked Jun 17 '12 05:06

user1092042


People also ask

How do I set environment variables in Perl programs?

For example, to determine the setting of your "PATH" environment variable, you can write a line of Perl code like this: $path = $ENV{'PATH'}; As you may remember, "%ENV" is a special hash in Perl that contains the value of all your environment variables.

How do I create a Perl PATH in Windows?

Making Perl available via the PATH settings on WindowsRight-click on Computer. Go to “Properties” and select the tab “Advanced System settings”. Choose “Environment Variables” and select Path from the list of system variables. Choose Edit .

Where is my Perl PATH Linux?

Normally, perl will be in your shell's path. It can often be found lurking in /usr/bin or /usr/local/bin. Use your system's find or locate command to track down perl if it doesn't appear in your command path.


2 Answers

You are creating a shell, executing a shell command that sets an environment variable in the shell, then exiting the shell without doing anything with the environment variable. You never changed perl's environment. That would be done using

local $ENV{PATH} = "$ENV{PATH}:/usr/local/jdk/bin";

Kinda weird to add to the end of the path, though.

like image 109
ikegami Avatar answered Sep 30 '22 16:09

ikegami


Please note that ikegami's answer will only set the path in your local Perl-script, and will NOT change it for the shell that called your Perl script.

If you wish to change the path in the shell environment, so the next programs you run will also benefit from this change, you will have to use 'source' or "dot-space" sequence, or better yet - have this change to the path done in '.bashrc' or '.login' files.

like image 30
Gonen Avatar answered Sep 30 '22 17:09

Gonen