#!/usr/bin/perl # Author: Kevin P. Inscoe . # File: fun_with_tinyurl.pl # Date of creation: April 28, 2003. # Warranty: None expressed or implied. # License: The Open Software License. V1.1 http://www.opensource.org/licenses/osl.php # # OSI Certified Open Source Software. http://www.opensource.org/licenses/ # # Prerequisites: Perl 5.004 (minimum). # The purpose of this program is to generate a psuedo-random url for the # TinyURL.con site # Requirements require 5.004; use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use LWP::UserAgent; #Turn off Perl buffering $| = 1; # Globals # Locals my $revision="1.0"; my $progname = "index.cgi"; my $baseurl = "http://tinyurl.com"; sub get_rand; sub display_uri; # Display headers my $cgi = new CGI; print $cgi->header() . "\n" . $cgi->start_html( -title => 'Kevin P. Inscoe - Fun with TinyURL', -author => 'kevin\@inscoe.org') . $cgi->h1('Random TinyURL link') . "\n"; my $rand = get_rand(); my $uri = $baseurl . "/" . $rand; print "$uri


\n"; display_uri($uri); print $cgi->end_html . "\n"; exit(0); sub get_rand { # Get primary random sequence # tiny url is now up to /axxx so no need to go above the letter "a" in begining of # the url. Sequence is "0-9,a-z,A-Z" my @chars = ("a", 0..9); my $prand = @chars[map{rand @chars}(1)]; # Get secondary randon sequence my @chars = ("A".."Z", "a".."z", 0..9); my $srand = join("", @chars[map{rand @chars}(1..3)]); my $rand = $prand . $srand; return $rand; } sub display_uri { my ($uri) = @_; my $ua = new LWP::UserAgent; $ua->agent("Perl/5.1" . $ua->agent); my $req = HTTP::Request->new(GET => $uri); my $res = $ua->request($req); my $body = $res->content; if ($res->is_success) { print "$body\n"; } else { print "Could not retrieve URI. " . $res->status_line . "\n"; } }