0

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!

2 Answers 2

3

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";
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your advice, I would apply your note. In case of using open my $fh, '<',... should I change while(<IN>){ to while(<>){?
Change it to while(<$fh>)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.