3

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
  • Probably you want to use a shebang on the perlscript, mark it as executable and place it on a directory that is included on the $PATH. Another alternative would be to use a bash alias
    – Julio
    Oct 26, 2020 at 8:44
  • Once you set up the path, you'd use test.pl (or perl -S test.pl). The former requires the script to be marked as executable, and it must have a shebang (#!) line.
    – ikegami
    Oct 26, 2020 at 10:09

2 Answers 2

0

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
  • Sorry there, I tagged the post as Linux but I should have typed it out in the question. Thank you for answering. I'll try this.
    – Alyssa Low
    Oct 27, 2020 at 2:23
0

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
  • Hi! Thank you very much for this. I'll try and understand commands. Really appreciate it.
    – Alyssa Low
    Oct 27, 2020 at 2:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.