PGreen
PGreen

Reputation: 3307

Import a path defined in bashrc to perl script

I've defined a path (say, $MYPAT) in .bashrc file and I would like to use it in perl scripts, so that inside the script I just can write open(IN, "<$MYPAT/dir1/... How can I import this variable to directly use it in my script?

I guess is the same problem posted here How to use aliases defined in .bashrc in shell script but applied to perl.

Thank you!

Upvotes: 0

Views: 749

Answers (2)

Mak_Thareja
Mak_Thareja

Reputation: 177

There are so many modules for every work to make it possible. I think this module in CPAN would be helpful for you, just go through a little documentation.

Shell::Source

Upvotes: 1

friedo
friedo

Reputation: 67028

Assuming $MYPAT is an environment variable defined in your .bashrc, you can access it using the special %ENV hash in perl, which contains all the environment variables for your process. See %ENV in perlvar.

my $path = $ENV{MYPAT};

Also, as a stylistic note:

  1. You should use lexical filehandles instead of globals
  2. You should use the three-argument form of open.

So instead of

open(IN, "<$path/dir1/...") 

make that

open my $fh, '<', "$path/dir1/..." or die "yadda yadda";

Upvotes: 3

Related Questions