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

I've set up Rael Dornfest's perl script for hooking up to the Google API <http://www.oreillynet.com/cs/weblog/view/wlg/1283>, and it's working swell from the command line (only way I know how to run it unfortunately!), eg:

$perl googly <GoogleKey> related:www.foo.com 5

Would it be hard for me to create another perl script (that I can CRON to execute once a day) to...?:

1) execute the perl script for the API to generate the needed HTML (as invoked in the command line above)
2) output the results to a given PHP file I'd then be able to "Include"

I know this must be super-easy, but this newbie's clueless. I've been whacking at this with @ARGV, system() and backticks(), but I think I'm completely lost! Thanks in advance.

Replies are listed 'Best First'.
Re: Running perl and outputting to file
by thunders (Priest) on Apr 30, 2002 at 01:45 UTC

    Don't use system to call a perl script. It's kind of messy I googled around for the script you were talking about and I found that It was not at all difficult to convert it to a subroutine

    #!/usr/bin/perl use SOAP::Lite; print join "\n", googly(q|insert key here|,"super friends",2); sub googly{ @_ == 3 or die "Usage: googly <key> <query> <number of results>\n"; my($key, $q, $maxResults) = @_; # key, q, start, maxResults, filter, restrict, safeSearch, # lr, ie, oe my @params = ($key, $q, 0, $maxResults, 0, '', 0, '', 'latin1', 'latin1'); my $result = SOAP::Lite -> service("file:./GoogleSearch.wsdl") -> doGoogleSearch(@params); @matches = map( { qq{<a href="$_->{URL}">} . ($_->{title} || $_->{URL}) . qq{</a><br />} } @{$result->{resultElements}} ); }

    This was the orignal code I found it here

    #!/usr/bin/perl use SOAP::Lite; @ARGV == 3 or die "Usage: googly <key> <query> <number of results>\n" my($key, $q, $maxResults) = @ARGV; # key, q, start, maxResults, filter, restrict, safeSearch, # lr, ie, oe my @params = ($key, $q, 0, $maxResults, 0, '', 0, '', 'latin1', 'latin1'); my $result = SOAP::Lite -> service("file:GoogleSearch.wsdl") -> doGoogleSearch(@params); print join "\n", map( { qq{<a href="$_->{URL}">} . ($_->{title} || $_->{URL}) . qq{</a> <br />} } @{$result->{resultElements}} );
Re: Running perl and outputting to file
by Mr. Muskrat (Canon) on Apr 30, 2002 at 20:52 UTC
    Here's a hack of the google.pl script that is included when you download the API developer's kit. It uses the GoogleSearch module. In this example, it writes to results.html but that's easy enough to change.
    #!/usr/bin/perl use strict; use warnings; use GoogleSearch; @ARGV > 0 or die "usage: $0 key [query] [max results]\n"; my ($key, $query, $max) = @ARGV; # $key='000000000000000000000000'; my $google = GoogleSearch->new($key); my $result = $google->doGoogleSearch( query => $query || 'related:www.perlmonks.org', maxResults => $max || 10, )->result; open(HTML, ">related.html"); print HTML qq(<a href="$_->{URL}">$_->{title}</a><br>\n) for @{$result->{resultElements}}; close(HTML);

    Matthew Musgrove
    Who says that programmers can't work in the Marketing Department?
    Or is that who says that Marketing people can't program?
Re: Running perl and outputting to file
by merlinmann (Initiate) on Apr 30, 2002 at 00:27 UTC
    I begged some much-appreciated guidance from Mr. Trott, and I think have something that might work.
    #!/usr/bin/perl system('perl googly <GoogleKey> foo 5 > /path/to/outputted/file/bar.ph +p');
    Seems like I could run that (even with multiple scripts for different searches). Make sense?