8

I am a new Ubuntu user, trying to run scripts developed on Straberry Perl on Windows 7. My Ubuntu environment is Ubuntu 14.04 LTS and Perl 5.18.2.

The first line of my scripts is #!/usr/bin/perl, which should invoke the perl interpreter in /usr/bin, and indeed, there is a 10.2 Kb object living there. However, my scripts won't run unless 'perl' is the first command line argument on Terminal, as in perl myscript.pl.

Is there a way to have perl automatically running .pl scripts without invoking it explicitly?

4
  • 8
    what is the error when you run ./myscript.pl
    – Maythux
    May 21, 2015 at 10:34
  • do you have more than one version of perl?
    – Maythux
    May 21, 2015 at 10:35
  • 3
    What's the output of which perl?
    – 0x2b3bfa0
    May 21, 2015 at 10:47
  • 4
    What's the output of ls -l myscript.pl ? In particular, what are the permissions — is there an x in the list (something like rwxr-x-rx)? You may need to enable the executable bit: chmod u+x myscript.pl will make it executable for the owner (i.e., you, presumably). May 21, 2015 at 16:54

4 Answers 4

10

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
9

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.

1
  • +1: You solution was what I inmediately thought when read the question. ;-)
    – 0x2b3bfa0
    May 21, 2015 at 10:48
2

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"

0

Thank you, all, I found a working solution.

I use only one version of perl. I use a default perl installation on a default Ubuntu 14.04 LTS installation, with defaults tools bash and gedit.

Using bare 'myscript.pl' on the cmd line I got weird 'no such file','command not found' or 'cannot execute binary file...' messages.

Using chmod u=x, I changed the x bit. Script still doesn't run.

Then, using gedit, I saved the UTF-8 file with Unix LF instead of DOS CR/LF. Still wouldn't run.

Then, using './myscript.pl', bingo! it finally runs with !#/usr/bin/perl.

Conclusion : Linux users coming from Windows have to learn some Unix...

My script now runs on both Linux and Windows and correctly prints and sorts accented character filenames and messages provided it is saved in the correct default format, UTF-8 with Unix LF for Linux, or ISO-8849-1 or ANSI with CR/LF for Windows.

If anybody would like to have more info, I can post a barebone test version doing exactly that.

3
  • 1
    This answer suggested exactly to do what you did, thanks for posting your solution back but perhaps you should accept that answer instead?
    – kos
    May 24, 2015 at 10:35
  • I tried to take into account all the above comments to work out my own solution. I would like to sent "my" solution to all those who took the time to answer to my question, I am not sure I did that correctly. So, if anybody receive an e-mail link about this thread this morning, please acknowledge.
    – mlibrt
    May 24, 2015 at 13:43
  • This is usually good, but in this case the answer I linked you suggested to do, in practice, exactly what you did to make it work, i.e. to convert carriage return+line feed(s) to newlines and to mark the script as executable; your answer contains a different method to accomplish the same, and therefore it should be kept, and again, thanks for having posted it back. Perhaps it would be fair to accept that answer at least to thank the user for his effort, since basically the content is the same, and that answer has been posted 3 days ago.
    – kos
    May 24, 2015 at 15:12

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.