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

I looked through the archives and didn't see this discused.

My wife recently got a Mac laptop with OSX (Tiger - 10.4.mumble) and I have writen some perl scripts for her. The following example will not run from the command line:

#!/usr/bin/perl use strict; use warnings; print "Hello World\n";
However, adding the -w flag, it will:
#!/usr/bin/perl -w use strict; use warnings; print "Hello World\n";
on execution of the first all I get is
".: No such file or directory
Adding a "-w" to get things to work is minor. I am just trying to understand what is happening.

Any ideas?

P.S. It isn't a execute permision issue the file permision is set to 777.

Replies are listed 'Best First'.
Re: Problem with running Perl script on Mac OSX
by sgifford (Prior) on May 31, 2006 at 16:11 UTC
    Sounds like a line-ending problem. Try looking at the script with cat -vET.

    If there's a Windows-style line end on a file, Unix will often misinterpret the "shebang" line saying where to find the interpreter as /usr/bin/perl^M, and since there isn't a program called /usr/bin/perl^M it fails. By editing that line, you may have fixed the line ending, accidentally fixing the problem.

    If that's the problem, tools like dos2unix can fix it, or something like:

    perl -pi.bak -e 's/\r//g' your_script
      Actualy I can spend all day editing it back and forth adding and removing the "-w" or even "-d" and if there is nothing after the #!/usr/bin/perl it fails.. But I will double check it when I get home and make sure that I have run dos2unix on it.

      The real confusion is it works fine on Fedora Core 5 even with the "^M" in it.

Re: Problem with running Perl script on Mac OSX
by MonkE (Hermit) on May 31, 2006 at 16:46 UTC
    If it's not the dos2unix issue, try creating a new script file from scratch -- no copying, etc -- type it in. You could have invisible control characters embedded in there somewhere, depending on your choice of editor. This issue has bitten me before.

    Just a security note -- I know it's your wife's laptop -- a better choice for permissions would be 755. Setting permissions to 777 (everyone has full access) is a bad habit. If you ever get a job doing perl CGI scripts or something like that, you'll need to be careful.
      I relize that 777 is dangerous but that is what my wife set it to when she was trying to get it working initaly I am in the process of teaching her about unix security and good computing practices.
Re: Problem with running Perl script on Mac OSX
by roboticus (Chancellor) on May 31, 2006 at 22:29 UTC
    Ya don't happen to have ". in the PATH variable, do ya? I've seen that sort of behavior before when PATH gets munged.

    --roboticus

Re: Problem with running Perl script on Mac OSX
by dracos (Sexton) on Jun 02, 2006 at 17:49 UTC
    Well it was an issue with the stupid ^M. Now I also remember the stupid thing doesn't have dos2unix on it. Guess thats my next script...