spacer
Web Development Tutorials CGI-PERL Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Random subroutines in Perl

By Tony Lawrence
2007-11-23


Random subroutines in Perl

I'll bet your first question might be "why on earth would I ever want to call subroutines randomly?". Admittedly, it isn't a need that comes up that often, but (for example) it's used right here on this very page that you are reading.

Ayup, you guessed it, it's ads again. I know, I know: you hate the ads, but they pay the bills, and actually every now and then there's something that actually might be of interest, so there it is and there they are. If you need to see the page without ads, disabling Javascript will get rid of a lot of them, and clicking on the "Printer Friendly" link will absolutely remove them all.

OK, now that you've accepted the inevitable, let's look at things from the publisher's point of view. We have various ads to run, and places to run them. What runs where? Well, duh, you put the best performing ads first. OK, then what? Second best? Sure, but what happens when you don't know what works best? Like right about to the left of this paragraph, what works best there?

Or maybe I just want them random to help combat that awful "ad blindness" that comes from people seeing the the same thing in the same place and therefor not really seeing it at all. Maybe mixing things up will help.

Whatever the reason, I have a choice of three or four ads I could stick over there, but I'm not sure which I want first. Maybe I want random Perl subroutines?

Why subroutines rather than just variables containing the text of the various ads? Well, you could do it that way, but subroutines seem cleaner to me. It comes down to doing something like this:



# define a subroutine
$rads[0] = sub {
print "This is the zeroth sub\n";
print "Testvar is set\n" if $testvar;
}


Or this:

$temp="This is the zeroth sub\n";
$temp .= "Testvar is set\n" if $testvar;
$rads[0]=$temp;


I think the first form (an anonymous subroutine) is neater and less confusing to modify. So let's use that. This example defines a few anonymous subroutines and then runs through them randomly::


#!/usr/bin/perl
#
# define some subroutines
#
$s[0]=sub {
print "s1\n";
};


$s[1]=sub {
print "s2\n";
};


$s[2]=sub {
print "s3\n";
};


$s[3]=sub {
print "s4\n";
};

#
# mix the order up randomly
#


shuffle(\@s);

#
# how many did we define?
#


$x=scalar(@s);


#
# run through the subs
#


while ($x--) {
$s[$x]->();
}


#
# randomly shuffle an array
#


sub shuffle {
my $array=shift;
my $i;
for ($i = @$array; --$i; ) {
my $j= int rand ($i+1);
next if $i == $j;
@$array[$i,$j]= @$array[$j,$i];
}
}

The "shuffle" subroutine was cribbed from Perl Cookbook; I'm far too lazy to cobble that up myself.

Easy enough, isn't it? At the time I am writing this, I am using something very much like this to produce the ads in the left column of this page. However, it's always possible that you are reading this years later, so I may be doing something completely different - I might not even have a left column! But you get the idea, right?



Tutorial Pages:
» Random subroutines in Perl


© Copyright 2005 A.P. Lawrence


 | Bookmark Print |   Write For Us
Related Tutorials:
» Log Script Use
» Creating Perl Modules for Web Sites
» Bit Vector, Using Perl Vec
» Build a Perl/CGI Voting System
» Perl Range Operator
» Creating Perl Modules for Websites



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: