Shebang line doesn't invoke perl

Solution 1:

I'm going to take a guess and postulate that your files (developed in Windows) are in DOS (CRLF) format.

That will interfere with the shebang, failing with not-so-clear messages:

$ ./my_script.pl
: No such file or directory

See https://stackoverflow.com/questions/3569997/view-line-endings-in-a-text-file on how to verify your line endings.

For example:

bash-4.1$ cat -v my_script.pl
#!/usr/bin/env perl^M
^M
print "Hello World\n";^M
^M

Then there are different tools (e.g. dos2unix or fromdos) that will help you to convert your text files to proper unix line-ending.

After conversion:

$ cat -v my_script.pl
#!/usr/bin/env perl

print "Hello World\n";

And you will be able to execute them:

$ chmod 755 my_script.pl
$ ./my_script.pl
Hello World

Solution 2:

In order to solve this you can use #!/usr/bin/env perl as the hash-bang line, instead of #!/usr/bin/perl so that bash use the first Perl found in your PATH.

Quoted from http://perlmaven.com/hashbang

While we used #!/usr/bin/perl as our hash-bang line there can be other as well. For example if we have installed another version of perl in a different location and we would like our scripts to use that, then we can put the path to that version of perl. For example #!/opt/perl-5.18.2/bin/perl.

The advantage of setting a hash-bang (and turning on the executable bit) is that user does not have to know the script is written in Perl and if you have multiple instances of Perl on your system the hash-bang line can be used to pick which perl to be used. This will be the same for all the people on the specific machine. The drawback is that the perl listed in the hash-bang line is only used if the script is executed as ./hello.pl or as hello.pl. If it is executed as perl hello.pl it will use the version of perl that is found first in the directories listed in PATH. Which might be a different version of perl from the one in the hash-bang line.

Solution 3:

Taking a different guess: Does your script have executable permissions?

ls -l should list permissions like rwxrwxr-x in order to run the file as a script. You can modify the permissions in your GUI (right-click → Properties, and depending on your file manager program, usually something like a Permissions tab with a setting like Execute: □ Allow executing file as a program) or from a text shell with chmod; eg, chmod u=rwx,g=rwx,o=rx my_script.pl to set "the user owning the file can read, write, and execute it; members of the group owning it can do the same; others can read or execute (but not write) it"