Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Random Code Swapper...



l008com
Novice

Aug 24, 2002, 6:16 PM


Views: 2459
Random Code Swapper...

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


Views: 2454
Re: [l008com] Random Code Swapper...

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


Views: 2453
Re: [l008com] Random Code Swapper...


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


Views: 2448
Re: [RedRum] Random Code Swapper...

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


Views: 2447
Re: [davorg] Random Code Swapper...


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)