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

When I try to run this program on linux, like so:

lucky$ google perl monks

it displays a nice results page of all the searches for perl monks, but lynx seems to be running in the background: any keyboard action on my part, like up/down arrows or enter, are interpreted by the shell. I can't interact w/lynx at all; when I do ps -aux, I find "lynx (defunct)" listed. I thought exec() meant that perl replaced itself with whatever you're exec()ing -- what am I missing?

#!/usr/bin/perl -w # google: A quick and dirty way of searching google for the arguments # given on the command line, hampered only by the fact that it # doesn't work use strict; my $search="http://www.google.com/search?q="; foreach my $arg (@ARGV) { $search .= $arg . " "; } $search =~ s/\s/+/g; $search =~ s/\"/%22/g; $search =~ s/\'/%27/g; $search =~ s/\+$//; $search .= "&btnG=Google+Search"; $search = "--cookies $search"; exec "lynx $search";

I am now blessing your keyboard...

Replies are listed 'Best First'.
Re: Why can't I start up lynx with these arguments?
by bikeNomad (Priest) on Jul 25, 2001 at 10:05 UTC
    Lynx seems to have some interesting problems with this. This worked for me:
    exec "xterm -e lynx $search";
    as did this:
    use strict; my $search="http://www.google.com/search?q="; foreach my $arg (@ARGV) { $search .= $arg . " "; } $search =~ s/\s/+/g; $search =~ s/\"/%22/g; $search =~ s/\'/%27/g; $search =~ s/\+$//; # $search .= "&btnG=Google+Search"; # $search = "--cookies $search"; exec "w3m $search"

    I've found w3m to be better than lynx, anyway (table support, among other things).

Re: Why can't I start up lynx with these arguments?
by arturo (Vicar) on Jul 25, 2001 at 15:22 UTC

    If you're just trying to get a little handier with Perl and writing this code to learn, I applaud. If you're trying to get something actually done, though, I recommend you save yourself the effort and check out the WWW::Search family of modules on CPAN. There are routines in there for conducting searches on a lot of the popular search engines, including, of course, Google.

    HTH!

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Why can't I start up lynx with these arguments?
by JPaul (Hermit) on Jul 25, 2001 at 18:06 UTC
    The problem is in this line here:
    $search .= "&btnG=Google+Search";
    The & in the exec'd line is causing the lynx process to background itself, thus giving you the defunct lynx process.
    You are effectively doing:
    lynx www.google.com/searcn?q=whatever&btnG=Google+Search --cookies whatever
    (Presuming the last '$search =' is actually supposed to be '$search .=', which would also screw things up)

    To stop the process from being backgrounded, double-escape the & symbol. (Once to escape the & for the shell, and once for perl itself to pass the \ through to the shell).

    $search .= "\\&btnG=Google+Search";
    JP
    -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --
      Thanks to everyone who replied...When I saw the double-escape answer, I smacked myself on the forehead and uttered the Sacred Syllable "D'oh!"

      While I like w3m well enough, I'm used to lynx. And this was meant to be a way of learning better how to do a quick and dirty hack, rather than parse search results for other nefarious ends.

      I am now blessing your keyboard...

(tye)Re: Why can't I start up lynx with these arguments?
by tye (Sage) on Jul 25, 2001 at 21:50 UTC
    exec "lynx", $search;

    Prevents the shell from seeing your command line and doing nasty things to it.

            - tye (but my friends call me "Tye")