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

I've been struggling to install a web-based HTML Validator on Mac OS X. There are ways to do it, with lots of patience and makeing various binaries.

So then I had what I thought was a brilliant idea -- BBEdit has a validator, BBEdit is scriptable via AppleScript; I'll use BBEdit as the back end and cut out all the hassle.

This script works just fine from the command line:

#!/usr/bin/perl use strict; use Mac::AppleScript::Glue; use LWP::Simple; print "Enter URL:\n -> "; my $URL = <STDIN> || die "$!"; getstore( $URL, "/path/to/tempfile.html" ) || die; my $BB = new Mac::AppleScript::Glue::Application('BBEdit') || die "$!"; my $results = $BB->check_syntax( file => "path:to:tempfile.html" ) || die "$!"; # If it worked, $results is now # an AoH-type data structure foreach my $item ( @{$results} ) { print 'Line ' . $item->{'result_line'} . ': ' . $item->{'message'} . "\n"; }
So then I tried it as a CGI script and it totally dies at the commented line above, "# if it worked...".

When run from he command line, it works fine.

When run from the browser, it gives no output beyond that point.

I tried to wrap it with an eval{} but it still died.

Any ideas? Any reason why it should run fine from the command line but fail when run from the browser? What should I try or troubleshoot?



($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Mac::AppleScript::Glue in CGI script
by tachyon (Chancellor) on Nov 13, 2004 at 05:21 UTC

    See the CGI Help Guide. Amongst your issues is not printing a valid header (500 Internal Server Error). However if you did have a valid header and it runs on the command line the problem is almost certainly permissions as a CGI runs as the webserver user (nobody or apache typcially) not user you. Add this to see what the problem is:

    BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); }

    cheers

    tachyon

      I'm sorry not to have pointed out, I didn't just take the script above and try to run it as a CGI.

      I printed a valid header and used CGI::Carp and that didn't help. It still died silently at the line noted.

      Here's a script which works in the command-line and not in the browser:

      #!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); print "Content-type:text/plain\n\n"; require Mac::AppleScript::Glue; print "started\n"; my $BB = new Mac::AppleScript::Glue::Application('BBEdit') || die "$!"; print "success creating Applescript Glue App object\n"; my $results = $BB->check_syntax(file => 'path:to:file') || die "$!"; print "success checking syntax\n";

      It gets as far as the first success message, and then, if the file exists, just stops.

      If I try it on a file which doesn't exist, then yes, I get a failure from AppleScript and CGI::Carp does its thing. But if there is a file at that location, then it just stops.

      What can I investigate in terms of permissions?



      ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
      =~y~b-v~a-z~s; print
        Take a dummy file, chmod it to 777, then point your script at it. 777 is world-writable so if it works you know you have a permissions problem. If it doesn't work then it's probably something else. Either way, delete the file after you run your tests.

        In addition to the chmod 777 test file check you should switch debugging on. The syntax is a bit clunky but you do:

        $Mac::AppleScript::Glue::Debug{SCRIPT} = 1;

        Where SCRIPT can be SCRIPT,RESULT,PARSE,INIT,AUTOLOAD. This should get you more output if it is not a simple perms issue (which I still think it is). When you say stops there is stop and STOP! Is it hung or exited? Is it using 100% CPU or 0% cpu? Does it timeout eventually?

        cheers

        tachyon

Re: Mac::AppleScript::Glue in CGI script
by brian_d_foy (Abbot) on Nov 13, 2004 at 17:02 UTC

    If you only want an HTML validator, get one of the many written in Perl (or even some written in C). Only use AppleScript when you have no other options ande absolutely have to work with a Mac application to get something done.

    --
    brian d foy <bdfoy@cpan.org>
      Could you point me in the right direction for these validators? There are two major options as far as I know, the W3C and the WDG validators, both of which come in Red Hat packages and packages for other Linux distros, but they each rely on a binary parser which is difficult to compile on OSX.


      ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
      =~y~b-v~a-z~s; print
        HTML Tidy There are lots of options with this as it is available in C, Perl XS, Python or even a Java Port.

        cheers

        tachyon

Re: Mac::AppleScript::Glue in CGI script
by Fletch (Bishop) on Nov 13, 2004 at 14:28 UTC

    I'd bet it might be a problem with you trying to send Apple Events from a different user (www). This is getting out of my range of Mac-fu, but you might try enabling Remote Apple Events under System Preferences / Sharing and see if that works. You might also try setting up the CGI to run under your userid with suexec and see if that works.

    Addendum: And if you can't get BBEdit to work you might look at htmltidy, which probably will compile on OS X without much trouble.