Stop Microsoft

Miscellaneous => Programming & Networking => Topic started by: Calum on 23 September 2002, 03:19

Title: perl training wheels
Post by: Calum on 23 September 2002, 03:19
i wrote the following into a file:
Code: [Select]
as per a tutorial page i found online, now i have got perl installed on my computer in /usr/bin/perl and when i visit the file with mozilla, it displays it as text rather than getting perl to execute it and then display the html.

Why is this? is it because i am visiting it on the same machine? is there some config i have not set? is the perl i have installed somehow different from the perl i need to do this?

sorry, i know it is very newbish to ask, but what's going on (or not)?
Title: perl training wheels
Post by: voidmain on 23 September 2002, 04:01
What did you name the file? Usually Apache is configured to treat *.cgi files as executable programs such as Perl programs. Also Apache needs to be configured to recognize *.cgi files as executable programs which it usually doesn't come configured to do so by default. You also need to put "ExecCGI" in your Options in the Apache configuration file for the directory that you want to be able to execute CGI programs.  Also, the file needs to be set "executable" for the user that your Apache server runs under (usually user "apache" or "nobody" but as long as you do a "chmod a+rx file.cgi" that would cover it).

Do you need help with the Apache configuration? I gave m0r the same informtation in another thread...
Title: perl training wheels
Post by: Calum on 23 September 2002, 04:16
wow!   :confused:  yes i think i do, where's the thread? thanks, i never realised how much there was out there that i didn't know!

now as i say, there's no reason i shouldn't be able to test my file out on the same machine as it is hosted on is there?
Title: perl training wheels
Post by: voidmain on 23 September 2002, 04:22
Not at all. Just put it under your web root (as long as you have Apache configured with ExecCGI and the other configs I mentioned) and point your browser to http://localhost/file.cgi. (http://localhost/file.cgi.)

There actually should be an area that *is* configured by default to run CGI files and that would be under /var/www/cgi-bin on the local directory which would be http://localhost/cgi-bin/file.cgi (http://localhost/cgi-bin/file.cgi) in the browser.  I'll see if I can dig up the thread on configuring Apache to run CGI files in any directory under your web root.

Yes, here it is:
http://forum.fuckmicrosoft.com/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=5&t=000766 (http://forum.fuckmicrosoft.com/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=5&t=000766)

[ September 22, 2002: Message edited by: void main ]

Title: perl training wheels
Post by: Calum on 23 September 2002, 04:26
thank you very much, good to know you are around with this info when i need it!  :D
Title: perl training wheels
Post by: Master of Reality on 23 September 2002, 04:46
dont you just need to add something like:

<directory /var/www/html/cgi-bin>
Options ExecCGI FollowSymLinks
Order allow,deny
AllowOverRide none
</directory>

and make sure you have the line:
addhandler cg-script .cgi
or somethin'

i cant remember what my exact configuration is but i have it set to execute cgi in all directories except for a couple in my ikonboard tree.

are you following an online tutorial? if so, where is it?

[ September 22, 2002: Message edited by: The Master of Reality / Bob ]

Title: perl training wheels
Post by: voidmain on 23 September 2002, 04:48
Actually you shouldn't have to add anything if you put the file under the cgi-bin directory.
Title: perl training wheels
Post by: Master of Reality on 23 September 2002, 04:52
my default setup had /var/www/cgi-bin but had the document root set to /var/www/html/
so wouldnt there be no way to access the cgi-bin from the net?
Title: perl training wheels
Post by: Master of Reality on 23 September 2002, 04:56
ahhhh... i get it. the ScriptAlias command redirects /cgi-bin/ to /var/www/cgi-bin
Title: perl training wheels
Post by: voidmain on 23 September 2002, 04:59
You got it.
Title: perl training wheels
Post by: Master of Reality on 23 September 2002, 06:15
can anyone see whats wrong with this:
i dont plan to really use this script. It was just part of tutorial stuff.
Code: [Select]

[ September 22, 2002: Message edited by: The Master of Reality / Bob ]

Title: perl training wheels
Post by: voidmain on 23 September 2002, 06:52
Try and run it on the command line, it will tell you where your errors are.

Line 9 is missing an ending quote and a semicolon. And while you are at it you might as well set line 9 to what your sendmail program is (probably /usr/lib/sendmail):

$sendmail = "/usr/lib/sendmail";

Line 24 just doesn't make any sense. Looks like what is missing from line 9, comment it out:

#usr/lib/sendmail";

Line 33 is missing a semicolon:

chop($date);

Line 88 is screwed, turn it into this:

Code: [Select]

So now it should look like this:

Code: [Select]

Actually there was some repeated shit at the top that I got rid of. Now, that fixes the syntax problems. You also need to make sure the program will have write access to the log file you have assigned in the program.

And of course you'll need the configuration file that the program uses which is an empty ~/.allowmailform file in the home directory of the user that is to receive messages from this CGI. That file needs to be readable by the web server which means the user must also have at least the execute bit turned on in their home directory for world.

I just created a basic HTML form that calls this CGI and it worked. It sent mail to my local account. So you'll need to write an HTML form to use it.

[ September 22, 2002: Message edited by: void main ]

Title: perl training wheels
Post by: Master of Reality on 23 September 2002, 07:49
after looking over my post agin... i ma sure that i copied that completely wrong...or at least the parts about sendmail that dont make sense.

[ September 22, 2002: Message edited by: The Master of Reality / Bob ]

Title: perl training wheels
Post by: voidmain on 23 September 2002, 07:58
Well like I said, it works.  Just had to create a simple HTML form page to test it:

Code: [Select]

Of course you'll probably not want fields for a couple of them and would want to hard code values using "<input type=hidden>" tags...

[ September 22, 2002: Message edited by: void main ]

Title: perl training wheels
Post by: Master of Reality on 23 September 2002, 08:11
Code: [Select]
this is the form i have:
Code: [Select]
edit: fixed

[ September 22, 2002: Message edited by: The Master of Reality / Bob ]

[ September 23, 2002: Message edited by: The Master of Reality / Bob ]

Title: perl training wheels
Post by: voidmain on 23 September 2002, 08:15
You didn't read my corrections.  Look at the line right above that print line (the chop()) function has no semicolon at the end.  Just copy my code out and it should work.

If there is nothing wrong with the line that Perl complains about then usually the problem is on the line before it (missing semicolon etc). Or within a few lines above it.

[ September 22, 2002: Message edited by: void main ]

Title: perl training wheels
Post by: Master of Reality on 23 September 2002, 08:24
i read your post again before you posted and edited my message. I took out the line that checks for .allowmail since i am the only user on my server.
Title: perl training wheels
Post by: voidmain on 23 September 2002, 08:26
Well it's still not going to work without the semicolon after the chop() statement..
Title: perl training wheels
Post by: Kintaro on 23 September 2002, 21:08
Perl and Web design, just doesnt work, Im learning PHP4 whcih is the best Web development language i've seen. It can be inside HTML and executed by the server.
Title: perl training wheels
Post by: Calum on 23 September 2002, 14:03
i know, but the more strings one has to ones bow and all that.

So long as perl doesn't create any bad habits, i am happy to learn it prior to php...
Title: perl training wheels
Post by: voidmain on 23 September 2002, 19:24
quote:
Originally posted by Ex Eleven / b0b:
Perl and Web design, just doesnt work, Im learning PHP4 whcih is the best Web development language i've seen. It can be inside HTML and executed by the server.


Heh heh, you're obviously on those mind altering drugs.  Perl is a *great* CGI language.  I do use PHP more than Perl now for web programming but there is nothing wrong with using Perl CGI programs.  You can use *any* language as a CGI program, even C and C++, even shell scripts.
Title: perl training wheels
Post by: voidmain on 23 September 2002, 19:29
quote:
Originally posted by Calum:
So long as perl doesn't create any bad habits, i am happy to learn it prior to php...


I program in both and love both. I used to use Perl exclusively for web programming. Now I just use it occasionally and use PHP whenever possible.  However, I still use Perl a lot for intermediate systems programming, that is, I use Perl where what I need done is a little bit much for a shell script.

I believe you are better off to know both languages and then use the one that makes the most sense when solving a task. If you know them both well, you'll know which one makes more sense when the time comes.
Title: perl training wheels
Post by: Calum on 23 September 2002, 20:43
well in the meantime, (translation - haven't set apache up at home yet) i will upload my perl scripts to my calumsmusic.netfirms.com webspace where i have 24.5 Mb left and they have perl installed.

Only thing is, how do i chmod using ftp? (windows ftp at that), any idea? i currently do not have permission to view my new perl file when i view it via a browser as 'masses' or whoever.

Also, i tried to telnet in there to do this and no dice. That's not too good because i also wanted to do a 'which perl' to see if the path is correct in my text file. Can this be done using ftp as well?

the server itself is running Apache/1.3.26 (Unix) mod_perl/1.26 PHP/4.0.4pl1 mod_ssl/2.8.10 OpenSSL/0.9.6a on FreeBSD according to netcraft, but i do need to use windows to connect to the internet sadly.
Title: perl training wheels
Post by: Master of Reality on 23 September 2002, 20:46
type chmod 0755 /scripts/whatever.cgi when you are logged in the ftp server.
Title: perl training wheels
Post by: Calum on 23 September 2002, 21:23
Invalid command


remember this is the windows ftp.exe i am using, i ftp into the site, logon and type chmod 0711 /cgi-bin/* and get Invalid command

so what now? I have an ftp GUI at homw called WS_FTP95LE which can chmod, and it runs on windows. how is this done? is it acting as a frontend to ftp, or has it been written with more functions in it that a standard windows ftp.exe?

thanks...
Title: perl training wheels
Post by: voidmain on 24 September 2002, 00:00
Windows ftp.exe is not fully functional. The ftp server has the capability but if you don't use a client that is able to take advantage of that functionality then you will not be able to chmod.

Now, your web server does not allow telnet which is good but does allow ssh access?  Download putty.exe from http://www.openssh.org/ (http://www.openssh.org/) and try to connect to the server via ssh (make sure you click ssh in the putty logon because it also supports telnet by default).
Title: perl training wheels
Post by: Calum on 24 September 2002, 00:33
no they don't seem to support ssh or rlogin either.

there's precious little actual technical info available on their website either ( www.netfirms.com (http://www.netfirms.com)  , they are really proud of now being able to support frontpage extensions...   :rolleyes:  ) however i cannot do this chmodding thing with ftp.exe and i thought that meant i could therefore not do it in windows however that turned out to be a pile of pants since i connected using ws_ftp95le (my GUI ftp client for windows) and hey presto, chmod is suddenly supported. so i used that.
Title: perl training wheels
Post by: voidmain on 24 September 2002, 00:36
ewwww, front page extensions...
Title: perl training wheels
Post by: Calum on 24 September 2002, 00:45
http://calumsmusic.netfirms.com/cgi-bin/1stperlscript.cgi (http://calumsmusic.netfirms.com/cgi-bin/1stperlscript.cgi)

that's where i have put the script. When i visit it with a browser i get a 500 error (it is mozilla 1.0 on WinME not that it matters) and the file's permissions are 755. I am flummoxed, the file exists because i tried taking out the cgi-bin/ part and i got a 404.
Title: perl training wheels
Post by: Master of Reality on 24 September 2002, 01:10
i gots a 500 error too.... i dunno what to tell you.
Title: perl training wheels
Post by: voidmain on 24 September 2002, 01:10
It definately appears to be executing your script. Now the question is, do you have access to the web server error logs?  That would be the easiest way to see what is wrong.  But failing that there are pretty common things that can cause that problem. Incorrect headers are common. Can you post a copy of your script so I can take a look at it and test it on my system?
Title: perl training wheels
Post by: Calum on 24 September 2002, 03:59
no i don't have access to the server logs unfortunately...

i found out what the problem was. unicode text rather than ansi code, whatever the difference is (i don't even know) it turned out to be the difference.

I noticed since i made the text file in win2000 notepad, and then i opened it now in win9x notepad and hey presto, fucked text!!! hooray! so now it is fixed. The code is identical to that in the first post, (edit - i changed the link, but that's it) and now it works there's going to be no stopping me...  ;)

thanks folks for helping me. I still haven't got apache set up at home of course, i'll read up on it myself before i ask questions about that though...
Title: perl training wheels
Post by: Master of Reality on 24 September 2002, 04:49
good for you
Title: perl training wheels
Post by: Calum on 28 September 2002, 17:16
hello again:

Perl:


On my home machine i am trying to write basic perl stuff to learn it. i cannot seem to get my scripts to run without calling perl. here is what i mean: i have a page telling me to write the following into a text file:  
quote:
#!/usr/bin/perl

$inputline = <STDIN>;
print ("$inputline");
Now of course i can see easily what this does and when i execute it with "perl sample.pl" it works fine. The page however says this:  
quote:
1) type in the program and save it to a file (in subsequent steps assume the file is called foo)
2) tell the system that this file contains executable commands. To do this enter the command 'chmod +x foo'
3) run the program by entering the command foo
if you receive the error message foo not found or an equivelant, either enter the command ./foo or add the current directory to your PATH environment variable
Okay, received and understood. BUT i type in ./sample.pl and i get:  
quote:
bash: ./sample.pl: bad interpreter: Not a directory
i have verified that perl is indeed installed in /usr/bin/perl and i don't know what is going on. i suspect it is very simple so what is the situation please?

as always, thank you in advance.
Title: perl training wheels
Post by: voidmain on 28 September 2002, 20:30
I suspect you are writing this code on a Windows machine (using notepad for example) as this is a common issue since DOS/Windows text files are not the same as UNIX text files. Is this the case with you? I went over this with people in the past on this site about why this occurs and how to fix it.

DOS/Windows text file lines are terminated with a CR (Carriage Return) and an LF (Line Feed). Lines in UNIX text files are terminated with LF only. If you try to run a Perl script in UNIX and the first important line "#!/usr/bin/perl" is terminated with a CR/LF your program will spit out a "bad interpereter" message.

First of all, make sure your path to your perl interpereter is correct. Type "which perl". It should spit out "/usr/bin/perl", in which case your script has the right first line with the possible exception of the CR/LF.  To tell if that is the case type this:

$ head -1 sample.pl | od -c

which should spit this out:

Code: [Select]

Notice the "\n" at the end. If it shows "\r \n" then it is a DOS/Windows file and you need to strip out the CRs. The best solution is not to use that nasty icky Windows to write your Perl code. But you can strip the CRs by:

$ tr -d '\r' < sample.pl > unixsample.pl
$ mv unixsample.pl sample.pl
$ chmod +x sample.pl
$ ./sample.pl

You may also have a utility called "dos2unix" installed on your system that does the same thing, or you can download that utility and install it. It's just a tiny utility which will strip the CRs from a file. With my example using the "tr" command you need to make sure the file you are redirecting to is a new file and don't use the same file name for both input and output, then of course you will have to set the executable bit on that new file before you run it and replace the original file with it. The dos2unix utility does all of this automatically.
Title: perl training wheels
Post by: beltorak0 on 30 September 2002, 21:02
here's a little tip to help when you don't have access to the server logs:
Code: [Select]

this will (as indicated) send fatal server errors to the browser as well as the error logs (provided they have the CGI/Carp module installed).

hope this helps with future troubleshooting.

-t.
Title: perl training wheels
Post by: Calum on 30 September 2002, 13:23
useful information indeed. i will be remembering that.

Voidmain i am a bit clueless as to why this might be happening because i am writing my text file using emacs in linux! i haven't performed that operation that you suggest, i really need to brush up on the use of things like od and tr!

As i write this i am testing this out at home, and still do not have apache configured right. However my question really is, that the tutorial i am doing does not say that apache must be installed and has not in fact mentioned anything of this sort. edit:- sorry, just getting confused, i am attempting to make it work without needing to call perl, not attempting to serve it over the net.

Still, i'm not stumped yet, i'll keep fiddling and see what happens...

edit: thank the gods for shell accounts! i just typed the above <STDIN> file in my Grex account, did a chmod +x, and ran it using ./1stperlscript.pl and hey presto it does exactly what it is supposed to do! without needing to invoke 'perl' on the commandline. Now i just need to get it to work at home. i must be overlooking something simple...

[ September 30, 2002: Message edited by: Calum ]

Title: perl training wheels
Post by: Master of Reality on 30 September 2002, 17:01
quote:
Originally posted by void main:
The best solution is not to use that nasty icky Windows to write your Perl code. But you can strip the CRs by:

$ tr -d '\r' < sample.pl > unixsample.pl
$ mv unixsample.pl sample.pl
$ chmod +x sample.pl
$ ./sample.pl

You may also have a utility called "dos2unix" installed on your system that does the same thing, or you can download that utility and install it. It's just a tiny utility which will strip the CRs from a file. With my example using the "tr" command you need to make sure the file you are redirecting to is a new file and don't use the same file name for both input and output, then of course you will have to set the executable bit on that new file before you run it and replace the original file with it. The dos2unix utility does all of this automatically.


dos2unix works great. I had to use it onmy messageboard scripts.
Title: perl training wheels
Post by: voidmain on 30 September 2002, 19:44
Calum, the only thing that bugs me is the bash error message "not a directory" that you claim to be getting. If you are trying to run a DOS formatted Perl script you should get a bash error that looks like this:

Code: [Select]

But your "bad interpreter" message definately indicates that the first line in your Perl script is f00ked.  It either doesn't match the location of your perl executable or you have a '\r\n' where there should be a '\n' as I mentioned in the previous post.  Did you follow my message step by step and do the "which perl" and do the "od" commands?  If so I want to see the output from both of those commands.