CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Lookup and re-direct

 



graemep
New User

Jul 1, 2006, 8:46 AM

Post #1 of 5 (1750 views)
Lookup and re-direct Can't Post

hi all,

I need a simple script that takes a field input to a web page, looks it up in column 1 of a 2 column flatfile and redirects the browser to the url in column 2.

Thanks for your help


rork
User

Jul 2, 2006, 7:52 AM

Post #2 of 5 (1747 views)
Re: [graemep] Lookup and re-direct [In reply to] Can't Post


Code
#!/usr/bin/perl 

use CGI qw(:standard);
# use CGI::Carp qw(fatalsToBrowser)
use strict;
use warnings;

my $q = new CGI;

my $from_url = $q->param('url');
my $to_url
my $dbase = "dbase.txt";

open(FILE, "<", $dbase) or die "Can't open $dbase: $!";
while(<FILE>) {
chomp;
my ($url1, $url2) = split(/\t/, $_);
if ($url1 eq $from_url) {
$to_url = $url2;
last;
}
}
close(FILE);

if ($to_url) {
print $q->redirect($to_url);
}
else {
print $q->header;
print "Couldn't find $from_url";
}


Name your text input 'url'
You might have to modify the delimiter in the split function (currently tab), the filename of your database and the shebang line. To retrieve better error messages in CGI uncomment use CGI::Carp

The code is untested but should give you an idea.
--
Don't reinvent the wheel, use it, abuse it or hack it.


graemep
New User

Jul 2, 2006, 1:03 PM

Post #3 of 5 (1744 views)
Re: [rork] Lookup and re-direct [In reply to] Can't Post

Excellent! Thanks very much - have got it working fine now!

Graeme


davorg
Thaumaturge

Jul 3, 2006, 5:18 AM

Post #4 of 5 (1738 views)
Re: [rork] Lookup and re-direct [In reply to] Can't Post


In Reply To

Code
use CGI qw(:standard); 

# ...

my $q = new CGI;

my $from_url = $q->param('url');



When you use the ':standard' import tag, CGI.pm puts references to many of its subroutines into your symbol table so that you can call the subroutines directly and don't need to go via an object.

So it makes sense to use:


Code
use CGI; 

# Note: Don't use indirect object notation
my $q = CGI->new;

my $from_url = $q->param('url');


And it makes sense to use:


Code
use CGI qw(:standard); 

my $from_url = param('url'); # No object needed


But it doesn't make any sense to combine the two modes as you've done in your code.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


rork
User

Jul 3, 2006, 11:46 PM

Post #5 of 5 (1735 views)
Re: [davorg] Lookup and re-direct [In reply to] Can't Post

old costums never die, they just faint away...

I've learned it the second method and later started to use the object oriented method without dropping the qw(:standard). I learned oo with "new CGI", seems to be hard to drop that too for I usually use Module->new.

Well, I'll try to stop doing it now.
--
Don't reinvent the wheel, use it, abuse it or hack it.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives