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...:
Random Code Swapper...

 



l008com
Novice

Aug 24, 2002, 6:16 PM

Post #1 of 5 (1358 views)
Random Code Swapper... Can't Post

I need a perl script. Its pretty simple so maybe someone could write it for me? I need a script that does this very simple task
•Take a predefined folder
•Choose a random item in that folder
•read the contents of that item, and spit it to the screen
It would be used as a SSI, and thats all i need it to do.


davorg
Thaumaturge

Aug 27, 2002, 12:55 AM

Post #2 of 5 (1353 views)
Re: [l008com] Random Code Swapper... [In reply to] Can't Post

You might be able to adapt sometihng like the random image program from nms.

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


Paul
Enthusiast

Aug 27, 2002, 4:29 AM

Post #3 of 5 (1352 views)
Re: [l008com] Random Code Swapper... [In reply to] Can't Post


Quote
•Take a predefined folder
•Choose a random item in that folder
•read the contents of that item, and spit it to the screen



Code
my $dir   = '/a/b/c'; 
my @files = ();

opendir DIR, $dir or die "Can't open dir $dir : $!";
@files = grep { !/^\./ } readdir(DIR);
closedir DIR;

open FILE, $files[rand @files] or die "Can't read file: $!";
read FILE, my $string, -s FILE;
close FILE;

print "Content-type: text/html\n\n";
print $string;



(This post was edited by RedRum on Aug 27, 2002, 4:31 AM)


davorg
Thaumaturge

Aug 27, 2002, 5:26 AM

Post #4 of 5 (1347 views)
Re: [RedRum] Random Code Swapper... [In reply to] Can't Post

The only improvement I could suggest is that you might want to be able to change the Content-type header depending on the file that you choose.

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


Paul
Enthusiast

Aug 27, 2002, 5:34 AM

Post #5 of 5 (1346 views)
Re: [davorg] Random Code Swapper... [In reply to] Can't Post


Code
my $headers = { 
'gif' => 'image/gif',
'html => 'text/html',
'txt' => 'text/plain'
etc....
};

my ($rand, $ext) = $files[rand @files] =~ /^([^.]+\.([^.]+))$/;

open bla bla

print "Content-type: $headers->{$ext}\n\n";
print $string;



(This post was edited by RedRum on Aug 27, 2002, 5:39 AM)

 
 


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

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