[SWLUG] perl cgi advice

Dafydd Walters dafydd at walters.net
Thu May 15 18:58:42 UTC 2003


Gareth,

Certainly an interesting little project; you'll learn Welsh and Perl at the
same time.  Ardderchog!  Do you plan to make it available / GPL it when
you're done?

IMO Perl is a good choice for this project. PHP would also be a perfectly
sound choice.

CGI.pm is mainly useful for two things:
1. Extracting form fields passed to your CGI script.
2. Abstracting the generated web page.  Personally, I don't like doing this;
I know HTML pretty well, so I prefer the simplicity of print statements to
generate the output.

It seems that you're going to need two scripts; one to convert each word
into something like this:

<a href='http://yourserver.com/translate.cgi?welsh_word=bendigedig'
target='new_window'>bendigedig</a>

(make sure you uri_encode the welsh word in your script)

and then another script, translate.cgi, that takes the welsh_word (from the
"GET" field), and constructs a "POST" call to a translation engine.  Below
is a slightly modified version of the script I posted earlier that would do
this.  I've put this on a web server, so you can try it now.

Just click the following link:
http://www.dwalters.me.uk/pptx/translate.cgi?welsh_word=bwrdd

Regards,
Dafydd.

--------------------------

#!/usr/bin/perl

use LWP::UserAgent;
use URI::Escape;
use CGI;

$input_query = new CGI;
$welsh_word = $input_query->param('welsh_word');

print "content-type: text/html\n\n";

# Construct a query body containing any POST
# variables to be sent to the foreign script
$query = "Direction=" . uri_escape(" Welsh to English");
$query .= "&Type=" . uri_escape("Whole");
$query .= "&Translate=" . uri_escape("");
$query .= "&Word=" . uri_escape($welsh_word);
$query .= "&Subdir=" . uri_escape("user/Mark.Nodine/released");

# Create user agent object
$ua = new LWP::UserAgent;

# Request
$req = new HTTP::Request
'POST','http://www.cs.cf.ac.uk/user/Mark.Nodine/released/cgi-bin/WD.cgi/';
$req->content_type('application/x-www-form-urlencoded');
$req->content($query);
$result = $ua->request($req);

# result is in $result
if ($result->is_error) {
    print "<html><head><title>Translation</title></head><body>\n";
    print "<p>HTTP error</p>\n";
    print "</body></html>\n";
} else {
    print $result->content;
}

--------------------------


----- Original Message -----
From: "Gareth Lewis" <Gareth.Lewis at patent.gov.uk>
To: <dafydd at walters.net>
Sent: Thursday, May 15, 2003 5:00 AM
Subject: Re: [SWLUG] perl cgi advice


> Hi,
>
> thanks for the advice.
>
> what I'm actually trying to do is help myself in learning welsh by having
some cgi scripts that will take a welsh web page (ie the BBc welsh news)
grab the story text (easy as its flaged by comments)  push this through some
perl to produce a new page where every word is now a hyper link to a second
cgi script that will, if i click on the word - go off and do a dictionary
look up via a third parties website, and return the definition to either a
second window or a second frame.
>
> I've not used any perl modules before - but hopefully a night of hacking
shouldn't be too hard.
>
> I guess I should also use a CGI helper module ? is it CGIpm ?
>
> should I be trying this with CGI scripts, or would it be better to try
javascript or PHP ?  to be honest, the perl CGI is the only way i could
think of.  I've only dabbled slightly with PHP and javascrip, and javascript
is too flakey for me to want to use.
>
> cheers,
>
> Gareth.
>
> >>> "Dafydd Walters" <dafydd at walters.net> 14/05/2003 23:50:56 >>>
> Gareth,
>
> This is fairly straightforward using the LWP::UserAgent module.
>
> Your CGI script would look something like this (this assumes the target
CGI
> script handles form data via the POST method):
> -----------------------------------------------------
>
> #!/usr/bin/perl
>
> use LWP::UserAgent;
> use URI::Escape;
>
> print "content-type: text/html\n\n";
> print "<html><head><title>LWP::UserAgent test</title></head><body>\n";
>
> # Construct a query body containing any POST
> # variables to be sent to the foreign script
> $query = "post_param1=" . uri_escape("value of param 1");
> $query .= "&post_param2=" . uri_escape("value of param 2");
> $query .= "&post_param3=" . uri_escape("value of param 3");
>
> # Create user agent object
> $ua = new LWP::UserAgent;
>
> # Request
> $req = new HTTP::Request
> 'POST','http://foreign_site.com/foreign_script.cgi';
> $req->content_type('application/x-www-form-urlencoded');
> $req->content($query);
> $result = $ua->request($req);
>
> # result is in $result
> if ($result->is_error) {
>     print "<p>HTTP error</p>\n";
> } else {
>     print "<p>Result text is in the variable
> <b>\$result->content</b></p>\n";
> }
>
> print "</body></html>\n";
>
> -----------------------------------------------------
>
>
>
> ----- Original Message -----
> From: "Gareth Lewis" <Gareth.Lewis at patent.gov.uk>
> To: <discuss at swlug.org.uk>
> Sent: Wednesday, May 14, 2003 4:17 AM
> Subject: [SWLUG] perl cgi advice
>
>
> > Hi All,
> >
> > I have an idea for a web site that will involve me wanting to do the
> following :
> >
> > in response to a GET,  my PERL CGI script will need to try and run a
> external CGI script on anothers site and then process the results.
> >  IE  i want my script to do a look up on a third parties web server.
(its
> essentially a form of dictionary lookup on the third site)
> >
> > I assume that this is fairly easy for perl ?
> >
> > I am a newbie on this sort of stuff so any advice that can avoid days of
> confusion would be great !
> >
> > I have a perl cgi book that I'll try reading again.
> >
> > if anyone can point me to any simple & short scripts that do similar
> things for me to study, that would be ideal.
> >
> > cheers,
> >
> > Gareth.
> >
> >
> >
> >
> >
> > _______________________________________________
> > SWLUG Discussion List - Discuss at swlug.org.uk
> > http://swlug.org.uk/mailman/listinfo/discuss
> >
>
>
> _______________________________________________
> SWLUG Discussion List - Discuss at swlug.org.uk
> http://swlug.org.uk/mailman/listinfo/discuss
>
>
>
>
>





More information about the Swlug mailing list