Is there a way to execute a perl script from any location in the machine?
I'd like to know if there are other ways to execute a perl file without giving the path explicitly.
example:
perl ./images/seed/test.plI read on the export path command but I don't quite understand. Is that code supposed to be in the perl file? If so, please enlighten me. Thank you.
22 Answers
OP did not indicated what OS the script runs on.
OS: UNIX/Linux/Mac OS X
- add to top of the script
#!/usr/bin/env perl - or add to top of the script
#!/usr/local/bin/perl(verify perl location withwhich perl) - make the script executable
chmod u+x srcipt.pl(or just+xto be executable by anyone) - make bin directory for executables
mkdir ~/bin - move script to bin directory
mv scritp.pl ~/bin - add
~/bindirectory in login dot files (depends on used shell) - check what shell you use
echo $SHELL - for example for bash edit .bashrc and add
export PATH $PATH:~/bin - logout/login to pickup made changes (or alternatively source ~/.bashrc)
At this point you should be able to start script at any location of filesystem.
OS Windows:
- Follow the Instruction
Execute in your shell the following command, to enable finding executable files in the directory /path/to/perl/script:
export PATH=/path/to/perl/script:${PATH}If you plan to use the script in that path routinely, consider adding the above command to your ~/.bashrc or ~/.bash_profile file.
Add to the Perl script this line as the very first line in the script (it tells the shell to use the Perl interpreter):
#!/usr/bin/env perlMake the Perl script executable, so that you can execute it by typing its name:
chmod u+x /path/to/perl/script/perl_script_name.plNow you can execute the Perl script in any of the following equivalent ways:
perl_script_name.pl
# or:
/path/to/perl/script/perl_script_name.pl
# or:
perl /path/to/perl/script/perl_script_name.plSEE ALSO:
How to correctly add a path to PATH?
What should I use for a Perl script's shebang line?