Perl pearl

Discuss Programming

Perl pearl

Postby worker201 » Mon May 02, 2005 2:35 pm

I was running a really nifty Perl script someone gave me while this forum was down, and ran into some problems.
: bad interpreter: No such file or directory


According to a website I found since Void wasn't around, this means that the script's shbang path (#!/usr/bin/perl) is wrong. Well, it isn't! So, why did this command not work:
Code: Select all
./polygon.pl txcoast.xy

but this one did:
Code: Select all
/usr/bin/perl polygon.pl txcoast.xy

??

(btw, txcoast.xy is an argument file)
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Postby Void Main » Mon May 02, 2005 3:07 pm

You didn't happen to create the Perl file in Windows did you? If so, the problem is because the file is in DOS format. DOS formated text files are CRLF line terminated rather than LF terminated only which is how it's done in UNIX. This is a very common issue and causes the exact error you are referring to. There are several ways to convert the file from DOS to UNIX format. There is a command called "dos2unix" that will do it, you can do it in vim by loading the file and doing this:

Code: Select all
:set fileformat=unix
:wq


or you could:

Code: Select all
tr -d '\r' < original.pl > stripped.pl
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby worker201 » Mon May 02, 2005 3:23 pm

Silly me, I figured a Perl script would be written on a *nix machine. I am quite familiar with dos2unix. :x

Thanks for that, it works now.
worker201
guru
guru
 
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Postby ZiaTioN » Sun May 15, 2005 7:50 pm

For future reference another thing that can cause this is if you had to upload the file via ftp or sftp to a remote server and did so in binary mode.

If you upload a text file in binary mode it adds ^M's (CTRL+M) to the end of every line and therefore the perl interpreter freaks out when it sees this and generates the exact error you spoke of.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Sun May 15, 2005 7:56 pm

ZiaTioN wrote:For future reference another thing that can cause this is if you had to upload the file via ftp or sftp to a remote server and did so in binary mode.

If you upload a text file in binary mode it adds ^M's (CTRL+M) to the end of every line and therefore the perl interpreter freaks out when it sees this and generates the exact error you spoke of.


Actually that's not true. FTPing a file in binary mode does not "add" carriage returns. If it does then your FTP server is broken. In binary mode it should not change anything on the transfer.

A text file in DOS/Windows uses CR+LF (carriage return + line feed) to terminate lines where in UNIX/Linux only LF is used to terminate lines. Now, if you transfer a DOS formatted file from DOS/Windows to a UNIX/Linux via FTP in "ascii" mode then the CRs should get stripped off. In binary mode they will not and thus cause your problem.

If you don't ever use DOS/Windows you should never run into this issue. ;)
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Sun May 15, 2005 8:20 pm

Well it seems your terminology is more correct. Yes it may not add the illegal characters it simply does not alloow for the proper conversion between platforms.

I was going to reply back to your previous post and explain that I write perl code in Windows and transfer to Linux and UNIX all the time but decided it was not a needed statement. Of course I use the proper mode (and not auto mode) to transfer files.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests