in reply to Why do I need -w in a cgi script

Are you writing your programs on Windows and copying it "as is" (say, by using binary FTP, HTTP PUT, HTTP file upload, scp, copying on a mounted disk, sneakernet, or something else) to a Unix machine?

In that case, all the lines will have a trailing ^M (before the newline). Without the -w, you're telling the kernel you want to execute "/usr/bin/perl^M". That's a perfectly valid filename under Unix, but it's unlikely such a program exists.

With the -w, the line ends with "-w^M", and perl itself can deal with the Windows ending.

The solution is to strip the ^M's out of your file. FTP ASCII upload will do this automatically for you. Or use a utility like dos2unix, or something like:

perl -wple 's/^M//' yourfile
on the Unix side.

Abigail

Replies are listed 'Best First'.
Re: Re: Why do I need -w in a cgi script
by liz (Monsignor) on Oct 09, 2003 at 18:25 UTC
    Once, in a previous lifetime, when I was a sysadmin on a server where rather clueless peopleclients were uploading Perl CGI scripts, I fixed this problem by creating a symbolic link of which the name would include the ^M. It was easier then explaining having to use ASCII FTP mode over and over and over again.
    # perl -e '$a=`which perl`;symlink $a,"$a\r"'

    Liz

    Update:
    Change symlink creation to a more Perlish way.

    Update:
    Changed perlish way from # perl -e 'symlink $^X,"$^X\r"' to the above, to take into account the directory where Perl resides. Pointed out by ambrus.

       perl -e 'symlink $^X,"$^X\r"' won't work, as $^X equals to "$^X" in this case, so it will create a broken symlink named perl^M in the current directory. You'll first have to chdir to /usr/bin.
      Sorry to annoy you, but I still don't like it this way. On my system, `which perl` will return "/usr/local/bin/perl", so "/usr/bin/perl\cM" will still not exist. Also, which is just an alias for type -p, so I doubt if it's portable (it might not even work if it's defined in bashrc, that's read only in interactive sessions). (Also, on some machines, /usr/local/bin/perl is a later version of perl than /usr/bin/perl.)
Re: Re: Why do I need -w in a cgi script
by etcshadow (Priest) on Oct 09, 2003 at 15:41 UTC
    Actually, that's
    perl -wpi -e 's/\r//' yourfile
    But whos keeping track?

    ------------
    :Wq
    Not an editor command: Wq
Re: Re: Why do I need -w in a cgi script
by kryberg (Pilgrim) on Oct 09, 2003 at 15:51 UTC
    Excellent answer. I would not have thought of the "/usr/bin/perl^M", but it makes perfcet sense.

    I work in a Windows and Unix environment. One of the most simple yet useful things I've ever found on perlmonks was a slightly different version for stripping the ^M's. I use it all the time and it's very nice if you have multiple files that need cleanup.

    perl -pi.bak -e 's/\cM//g' *.cgi

    You don't really need the backup up, but it's nice when you first try it to know that you won't lose your data if it doesn't work.
      I work in a Windows and Unix environment. One of the most simple yet useful things I've ever found on perlmonks was a slightly different version for stripping the ^M's.

      I just ssh into the server and edit the files directly there, but for a more production-oriented environment I can see why some might not favor that approach, as a mis-edit could leave the script broken for a couple of minutes. In my situation this is not a big deal. Of course, if it were, I could have two directories on the server, one for development and one for production, which is probably what I'd do. Or have two servers.


      $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: Re: Why do I need -w in a cgi script
by Juerd (Abbot) on Oct 09, 2003 at 16:18 UTC
      I gave the code as a one-liner to be entered from the command line, and then ^M (as in "control-M", and not a caret followed by an M) will work. You might have to enter it as "CTRL-V CTRL-M" in certain shells though.

      Abigail