grewal7634 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to execute a perl file in terminal window in linux, but when i press enter after giving full path and name of the perl file it says bash: cpg4.pl : command not found.

Replies are listed 'Best First'.
Re: how to run perl file in linux
by marto (Cardinal) on May 13, 2014 at 10:45 UTC

      (Just in case the OP is new to UNIX/Linux.)

      ... and execute the file via './cpg4.pl' instead of just 'cpg4.pl'.

Re: how to run perl file in linux
by zentara (Cardinal) on May 13, 2014 at 13:26 UTC
    Just to amplify on Monk::Thomas's reply, the reason you need to put a ./ in front of commands run in the current directory, is to prevent the system from searching your system PATH first. What if the file name was identical to something else in your PATH? It gets confusing and is a security hazard, so the rule is put ./ in front of any command run from the current working directory. I hope it helps you remember it, it becomes second nature after awhile. :-)

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: how to run perl file in linux
by Anonymous Monk on May 13, 2014 at 13:25 UTC

    Easiest way is typing perl cpg4.pl at the bash prompt, This runs the perl interpreter on your program.

    Another way is add a "shebang" (#!/usr/bin/perl) line at the beginning of your script and mark the script as executable with the "chmod" command, and then run it like any other executable script.

      Of course, #!/usr/bin/perl will most likely run your system Perl, which may or may not be what you want. It is a good advice to install your "own" Perl in your home directory. Then you can install new modules and upgrade Perl without risking to break your system. Of course, the shebang should reflect the path to your own new Perl.

      If you find that this is all too difficult to manage, think about using perlbrew.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
        CountZero brings up a good point. This issue is why I like to use
        #!/usr/bin/env perl
        as the shebang, when I am on a linux or Mac OS X system. This way, it will run the script using the first perl found in your path. This node contains some more discussion on the topic: /usr/bin/env perl - query