Jaguar 10.2.6 and Perl

Posted:
in Genius Bar edited January 2014
Im doing network programming using perl this semester. Is Perl installed in MacOS X by default, or does it need to be installed.



If an instalation is required, is there any websites that give detailed information ?



Thanks



.:BoeManE:.

Comments

  • Reply 1 of 14
    thuh freakthuh freak Posts: 2,664member
    i think perl comes with jaguar.
  • Reply 2 of 14
    baumanbauman Posts: 1,248member
    type 'man perl' in terminal



    Lotsa fun. You can drudge through some of it and start playing around early if you'd like.



    Edit: Hmm, I just realized I didn't really answer your question. Yes, perl is pre-installed on all Jag installations. The 'man perl' page that I pointed you to is the installed documentation.
  • Reply 3 of 14
    boemaneboemane Posts: 311member
    Quote:

    Originally posted by bauman

    type 'man perl' in terminal



    Lotsa fun. You can drudge through some of it and start playing around early if you'd like.



    Edit: Hmm, I just realized I didn't really answer your question. Yes, perl is pre-installed on all Jag installations. The 'man perl' page that I pointed you to is the installed documentation.




    Thanks. New question though!



    Ive gotten perl to work through the perl interpreter in the command line. Now just a few quesions about Perl and OS X.



    1. is the interpreter located at usr/bin/perl ?



    2. Trying on my campus' Red Hat server, I create a file called test.html inside public_html that sends a form to a file called test.cgi inside public_html/cgi-bin/. This works fine, but how do I do the same in Jaguar ? Ive tried placing test.html in my ~/Sites/ folder, and test.cgi inside ~/Sites/cgi-bin/ which gives me the following error:



    Method Not Allowed

    The requested method POST is not allowed for the URL /~jhb/perl/cgi-bin/sample.cgi.



    The file is chmod'ed to 755.



    3. I have created a script called test.pl and are able to run it though perl -w test.pl someserver:service but ./test.pl someserver:service gives errors.



    Code:




    #/usr/bin/perl

    # file: test.pl



    use IO::Socket;



    my $file = shift;

    my $fh = IO::Socket::INET->new($file);

    my $line = <$fh>;

    print $line;









    Code:




    error:



    use: Command not found.

    file: Undefined variable.









    Any help appriciated!



    .:BoeManE:.
  • Reply 4 of 14
    1. If I remember correctly, it's usr/bin/perl



    2. I get the POST method on local scripts (on Jaguar) that work on Linux servers. I swear these used to work before Jaguar, but I'm getting too old to remember properly.



    According to my notes, I should change to following in /etc/httpd/httpd.conf:



    Code:




    <Directory "/Library/WebServer/CGI-Executables">

    AllowOverride None

    Options ExecCGI

    Order allow,deny

    Allow from all

    </Directory>









    In other words, add the Options ExecCGI so that Apache will let you run your scripts.



    3. Sorry-- I'm starting to zone out and can't read this properly. Maybe after my nap.
  • Reply 5 of 14
    boemaneboemane Posts: 311member
    Quote:

    Originally posted by GardenOfEarthlyDelights

    2. I get the POST method on local scripts (on Jaguar) that work on Linux servers. I swear these used to work before Jaguar, but I'm getting too old to remember properly.



    According to my notes, I should change to following in /etc/httpd/httpd.conf:



    Code:




    <Directory "/Library/WebServer/CGI-Executables">

    AllowOverride None

    Options ExecCGI

    Order allow,deny

    Allow from all

    </Directory>









    In other words, add the Options ExecCGI so that Apache will let you run your scripts.





    I dont have any <Directory "/Library/WebServer/CGI-Executables"> in my httpd.conf file. I found <Directory "/Library/WebServer/Documents"> with those parameters, but adding Options ExecCGI didnt fix the problem.



    Thanks for your help though!



    .:BoeManE:.
  • Reply 6 of 14
    thuh freakthuh freak Posts: 2,664member
    you'll have to add the ExecCGI option to your folder. I'm not completely sure about everything about httpd.conf, so you should really seek its documentation (try apache.org, or apache.com). i know that many folders have their own directive, and furthermore that macosx keeps each users' directives in its own file at /private/etc/httpd/users/userName.conf. in that folder it'll look like this (or similarly):



    <Directory "/Users/name/Sites">

    Options Indexes MultiViews

    AllowOverride None

    Order allow,deny

    Allow from all

    </Directory>



    on the end of the "Options .." line, add "ExecCGI". You'll also have to add the line "AddHandler cgi cgi-script pl" inside the <Directory> </Directory> bunch. Mine is at the end right before the closing. I think its only necessary that it comes after the Options line that includes ExecCGI. Then, to make things simpler, put your cgi file inside ~/Sites. Each directory (and again, I'm not sure because I haven't read through httpd.conf's documentation) needs its own version of "<Directory ""> </Directory>". They also need an alias or something so apache will know when to use the new directory under apache's own path system (URIs). As I understand it, apache essentially creates its own file system that doesn't necessarily directly relate to the structure files exist on the hard drive (like how your /Users/yourname/Sites on your computer relates to http://serveraddress/~yourname). Putting your .cgi (or other scripts) inside ~/Sites would cut out the necessity for creating the new <Directory> and Alias commands inside /private/etc/httpd/httpd.conf.
  • Reply 7 of 14
    willoughbywilloughby Posts: 1,457member
    If you don't want to mess with the conf files, try putting your CGI scripts in:



    /Library/WebServer/CGI-Executables/



    Say you put test.pl in there. Than the URL to get to that would be:



    http://yourcomputername/cgi-bin/test.pl



    Any other web documents can go in:



    /Library/WebServer/Documents/



    So your test html page URL will be:



    http://yourcomputername/test.html



    I'm pretty sure that is the default configuration in Jaguar.
  • Reply 8 of 14
    boemaneboemane Posts: 311member
    Quote:

    Originally posted by Willoughby

    If you don't want to mess with the conf files, try putting your CGI scripts in:



    /Library/WebServer/CGI-Executables/



    Say you put test.pl in there. Than the URL to get to that would be:



    http://yourcomputername/cgi-bin/test.pl



    Any other web documents can go in:



    /Library/WebServer/Documents/



    So your test html page URL will be:



    http://yourcomputername/test.html



    I'm pretty sure that is the default configuration in Jaguar.




    Thanks. I tried to put the script in /Library/WebServer/CGI-Excecutables/ and link to in in a html site with http://myRandevousName.local/cgi-bin/sample.cgi which gives me the following error:



    Quote:

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.



    Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.



    More information about this error may be available in the server error log.



    Any sugestions ?



    .:BoeManE:.
  • Reply 9 of 14
    thuh freakthuh freak Posts: 2,664member
    you can check out the log (/var/log/httpd/error_log). that file can get pretty large, so i suggest doing this:

    tail -f /var/log/httpd/error_log



    then trying to open the page. anything new thats added should be the relevant error message. post that back here and i (or someone else) can help you through it. i usually get an error when i try to mess with the configuration files. sometimes all you have to do to fix it is:

    sudo apachectl stop; sudo apachectl start

    (that starts and stops apache. alternatively you could delect personal web sharing, then re-select it from the Sharing panel in System Preferences.)
  • Reply 10 of 14
    boemaneboemane Posts: 311member
    Quote:

    Originally posted by thuh Freak

    you can check out the log (/var/log/httpd/error_log). that file can get pretty large, so i suggest doing this:

    tail -f /var/log/httpd/error_log



    then trying to open the page. anything new thats added should be the relevant error message. post that back here and i (or someone else) can help you through it. i usually get an error when i try to mess with the configuration files. sometimes all you have to do to fix it is:

    sudo apachectl stop; sudo apachectl start

    (that starts and stops apache. alternatively you could delect personal web sharing, then re-select it from the Sharing panel in System Preferences.)




    Okay. This is the two error messages I get:



    Quote:

    [Mon Jul 21 23:45:07 2003] [error] (2)No such file or directory: exec of /Library/WebServer/CGI-Executables/sample.cgi failed

    [Mon Jul 21 23:45:07 2003] [error] [client 192.168.2.22] Premature end of script headers: /Library/WebServer/CGI-Executables/sample.cgi



    .:BoeManE:.
  • Reply 11 of 14
    willoughbywilloughby Posts: 1,457member
    Premature end of script headers is a tough one. It could be a lot of things. Often times it happens when you upload a text file as binary.



    Did you create this CGI file on your mac or upload it from somewhere else? If you uploaded it, try uploading again but this time make sure you do it as ASCII and not Binary.



    Have you gotten this script to work on another unix box? If not, are you writing out the proper HTTP headers?
  • Reply 12 of 14
    boemaneboemane Posts: 311member
    Quote:

    Originally posted by Willoughby

    Premature end of script headers is a tough one. It could be a lot of things. Often times it happens when you upload a text file as binary.



    Did you create this CGI file on your mac or upload it from somewhere else? If you uploaded it, try uploading again but this time make sure you do it as ASCII and not Binary.



    Have you gotten this script to work on another unix box? If not, are you writing out the proper HTTP headers?




    Ok. I did this. I created a file hello.pl and put it in /library/webserver/cgi-excecutables. hello.pl was created using BBEdit and runs fine through perl -w hello.pl. Its a "Hello World" example:



    Code:


    #!usr/bin/perl

    # File : hello.pl



    print "Hello World";







    the server gives me an internal server error through Safari, and the log gives me this error:



    Quote:

    [Tue Jul 22 09:16:26 2003] [error] (2)No such file or directory: exec of /Library/WebServer/CGI-Executables/hello.pl failed

    [Tue Jul 22 09:16:26 2003] [error] [client 192.168.2.23] Premature end of script headers: /Library/WebServer/CGI-Executables/hello.pl



    the address I gave the browser is: http://jhb.local/cgi-bin/hello.pl



    ls on the directory gives me:



    Quote:

    [jhb:/] jhb% ls -l /Library/WebServer/CGI-Executables/

    total 48

    -rwxr-xr-x 1 jhb admin 54 Jul 22 09:12 hello.pl

    -rw-rw-r-- 1 root admin 5398 Jul 28 2002 printenv

    -rwx--x--x 1 jhb staff 216 Jul 21 23:50 sample.cgi

    -rw-rw-r-- 1 root admin 757 Jul 28 2002 test-cgi



    .:BoeManE:.
  • Reply 13 of 14
    thuh freakthuh freak Posts: 2,664member
    ok, didn't notice this before. with cgi you have to spit out the headers before you give the html document.



    so your hello world should be like this:

    #!/usr/bin/perl

    print "Content-Type: text/html\

    \

    "; #<-All you need for headers

    print "<html><body><p>Hello World</p></body></html>";

    # most browsers would probably render the words 'Hello World' without the html tags, but why use a web browser at that point?
  • Reply 14 of 14
    123123 Posts: 278member
    Quote:

    Originally posted by thuh Freak

    ok, didn't notice this before. with cgi you have to spit out the headers before you give the html document.



    so your hello world should be like this:

    #!/usr/bin/perl

    print "Content-Type: text/html\

    \

    "; #<-All you need for headers

    print "<html><body><p>Hello World</p></body></html>";

    # most browsers would probably render the words 'Hello World' without the html tags, but why use a web browser at that point?




    Actually, you need two CRLFs there, one for the header line, one to separate the header from the rest:



    print "Content-Type: text/html\

    \

    \

    \

    "



    Also, I personally wouldn't set "Options ExecCGI", rather "AllowOverride All" (or whatever you need: "AllowOverride Options" in this case) and then create .htaccess files to explicitly allow script execution wherever you need it (put a line into the .htaccess file that reads: options +ExecCGI).



    Don't forget to restart the server after making changes to the httpd.conf file (not needed when using .htaccess files).



    Also, you forgot the ! mark at the beginning of your script:

    #!/usr/bin/perl

    # file: test.pl



    use IO::Socket;

    my $file = shift;

    ...
Sign In or Register to comment.