I can't even make the simplest Perl script...

I’ve been succesfully tweaking other people’s Perl script for years. But when I start to compose my own, they just flat do not work.

For example, what the hell am I doing wrong here - all I want is for a logged in username to be printed on the screen:


#!/usr/bin/perl

print "$ENV{'REMOTE_USER'}";

exit;

(All permissions are set to execute, btw).

I’m not bad at programming JavaScript for a rank amateur. But I am just plumb mystified when it comes to Perl.

BTW, is that path thing at the top necessary? Do I have to call the Perl from another page to make it execute (I have tried this, BTW, with <!–#exec cgi="the_filename.pl@–>, but it just generates a standard HTML header code, and nothing else) or can I just type the filename into my browser?

Mystified. :confused: I suspect there’s some massive fundamental foundation truth about Perl that I’m missing. I’ve looked at several “teach yourself” sites, but they all seem to assume some comprehension of, and/or control over a Unix box, neither of which I possess.

First things first: What’s your OS?

You appear to be trying to run a Perl script from a web browser. You are aware that you need to run the script using the Perl interpreter? Since you’re complaining about the Unix-centricity of you reference material, I’m going to assume that you’re using Windows. In which case, you will need to download ActivePerl. Once you’ve got that, you should be able to run the script from the command line using


perl script_name_here

I assume you uploaded the perl script to a Web server (whether Windows or Unix-based) that can execute Perl scripts, as checked with another Perl script, right?

In that case, to have a Web page returned to you, you need to output a HTTP header, followed by a blank line (i.e. in the simplest case **print "HTTP/1.0 200 OK

";[/p], followed by the actual Web content.
A very basic framework for outputting a Web page:



#!/usr/bin/perl
use strict;                       # saves a lot of grief
use CGI::Carp qw(fatalsToBrowser);    # useful error messages

print "HTTP/1.0 200 OK

"; # primitive HTTP header

print *whatever* ; # content


You can then call the page by the script filename.

I’m no expert on these matters, but a couple of things:

As has been pointed out, your little script won’t produce html, so your browser won’t display jack. You need to add a couple of bits.
This next snippet might work, but as I said I’m no expert:



#!/usr/bin/perl
print "<html>
";
print "Hello ";
print "$ENV{'REMOTE_USER'}";
print "
</html>
";
exit;


Just save it in a file called something.pl, make it executable, and call the file directly from your browser. If you see a page, then perl is enabled on the server, if you see code or garbled code, then it isn’t and you need to ask for perl to be turned on.

Now, here’s a handy thing: As I know you have OS X, you’re in a situation where you don’t even need to worry about downloading or installing perl if you want to test stuff locally.

Save the above file into your home folder (as say /Users/jjimm/something.pl or whatever). Open the Terminal app. Type ‘perl something.pl’, and press return. You’ll see the output of the perl script: the REMOTE_USER bit won’t print cos there’s no such thing when you’re running the script from the command line. However:



print "Your home folder is $ENV{'HOME'}";


will tell you what your home folder is.

One last thing, perl isn’t enabled by default in the ‘Personal Web Sharing’ on OS X clients, but I believe its relatively easy to do and, if you like, mail me and I’ll help you do it.

Thank you for realising I’m not a complete dunderhead! :wink:

I am indeed working off an (externally hosted) Unix box running Linux, that is enabled for Perl. I already have about 20 scripts successfully running on it, all of which have written by other people, that I have customized.

micilin, your script gave me nothing but an internal server error!

Just a guess, but how are you uploading these scripts? Are you writing them on a windows machine and then ftping them? You got the ftp set to ascii or binary for the upload?

Yes I’m doing them on Windows, and I’m uploading them using Ascii - as said, I have about 20 other scripts that I’ve worked on and uploaded, and they all work.

Anyway, I have again rewritten someone else’s script and it’s all working tickety-boo for now. I’m still mystified but my immediate crisis has passed.

Thanks for everyone’s help!

Ah, but are you saving them in the Unix text format? Or in the windows text format? Some code editors can do the convert automatically on files in the right format (so that scripts you only edit are OK) , but I remeber having a problem with this - windows text using a different end-of-line ASCII character or somesuch, so that when I tried to run scripts, they didn’t work - and when I opened the files I’d uploaded on the Linux box, every line ended with “\M”. My new editor doesn’t seem to have a problem, but my last one always defaulted to Windows format. Which editor are you using?

Yup. set to Unix via the ‘file settings’ dialog on Allaire HomeSite.

If you are just FTP’ing the scripts directly, then you need to change their permissions to executable. I’m no expert, but you need to set the correct read/write/execute flags with
chmod a+rx scriptname.pl

Perhaps the other perl scripts that you are editing already have the correct permissions set but yours doesn’t. Check with ls -l to see if your script matches up.

The shebang notation (#!/usr/bin/perl) in the top line is absolutely necessary unless you are going to invoke your script by
perl scriptname.pl
If the file is executable and contains the shebang pointing to the correct perl interpreter, then you will be able to invoke your script by just
scriptname.pl

You might find this line in the OP instructive:

:wink: :stuck_out_tongue:

Sorry, edwino, my response came over as overly ungrateful there. Thanks for your advice, and I particularly appreciate the information about the <!# gubbins!