M HYPE SPLASH
// news

Is there a way to execute a perl script from any location in the machine?

By Emily Wilson

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.pl

I 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.

2

2 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 with which perl)
  • make the script executable chmod u+x srcipt.pl (or just +x to be executable by anyone)
  • make bin directory for executables mkdir ~/bin
  • move script to bin directory mv scritp.pl ~/bin
  • add ~/bin directory 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:

1

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 perl

Make the Perl script executable, so that you can execute it by typing its name:

chmod u+x /path/to/perl/script/perl_script_name.pl

Now 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.pl

SEE ALSO:
How to correctly add a path to PATH?
What should I use for a Perl script's shebang line?

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy