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

In How to make a CPAN module Ditribution I noted that there are issues with ^M ie the CR part or CRLF that you get for a line ending on Win32 when you move such a script to Linux. A monk just noted that he was less than convinced. I was thinking scripts (rather than Modules but you always have test scripts in a distro). Here is the evidence relating to the problem I get:

[root@devel3 root]# cat bad.pl #!/usr/bin/perl print "Hello Errors!\n" [root@devel3 root]# perl -pe 's/\015/^M/' bad.pl #!/usr/bin/perl^M print "Hello Errors!\n"^M [root@devel3 root]# ./bad.pl : bad interpreter: No such file or directory [root@devel3 root]# perl -pi -e 's/\015//' bad.pl [root@devel3 root]# ./bad.pl Hello Errors! [root@devel3 root]# perl -pe 's/\015/^M/' bad.pl #!/usr/bin/perl print "Hello Errors!" [root@devel3 root]#

So you can see that the \015 is present and causes the breakage shown which goes away when you make the \015s go away. Q: Why is it so?

cheers

tachyon

Replies are listed 'Best First'.
Re: Why does Perl choke on \015 ^M chars
by Paladin (Vicar) on Jan 03, 2004 at 04:26 UTC
    If there is a ^M at the end of the first line, your shell is trying to run a program called /usr/bin/perl^M which doesn't exist. This problem seems to go away if you use -w (or some other argument) on the shebang line, as the shell can then find the /usr/bin/perl in #!/usr/bin/perl -w^M.
      An alternate solution, which I have taken when I was still doing sysadmin chores for an ISP in the past, was to create a symbolic link from /usr/bin/perl^M to /usr/bin/perl (and the same for /usr/local, actually). This made sure that whenever someone uploaded a script in binary mode from a Windows machine, that the script would work without further human meddling from either end.

      Liz

      Of course, that makes perfect sense. I must have more scripts with no -w than I thought ;-)

      cheers

      tachyon

Re: Why does Perl choke on \015 ^M chars
by revdiablo (Prior) on Jan 03, 2004 at 06:30 UTC

    Paladin's answer is absolutely correct, and I see that you've even acknowledged that you understand why. I thought it might be worth underscoring the fact that, contrary to your question's title, this is not perl choking on ^M, it's the shell. This is an important distinction, and it's something that could use explicit mention. Perl should work fine with ^M's, no matter what platform. Incidentally, another trick besides -w (or getting rid of the ^M, which is the real solution) is to put a space at the end of the line, so it ends up as:

    #!/usr/bin/perl ^M
Re: Why does Perl choke on \015 ^M chars
by PodMaster (Abbot) on Jan 03, 2004 at 05:41 UTC
    It's not that I wasn't convinced, it's that I knew better :)

    Modules just don't care, and installed programs (by way of MakeMaker and alikes) will never have any issue with that (they will modify the shebang or something something).

    {local $RANT=1; perl scriptname will work no matter what the platform is, and that is how you should be invoking other perl programs :) }

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.