in reply to Main linker script

Here's a quickie.
#!/usr/bin/perl -Tw use strict; use CGI; my %Pages = (1 => sub {open FILE, 'frontpage.html'; print while <FILE>}, 2 => sub {open FILE, 'aboutus.html'; print while <FILE>}, 3 => sub {open FILE, 'login.html'; print while <FILE>}, 42 => \&showStats); my $Num = CGI::param('Num') || 1; #If we don't get a number, show the +front page print CGI::header(); &{$Pages{$Num}};
Notes:

Russ
Brainbench 'Most Valuable Professional' for Perl

Replies are listed 'Best First'.
RE: RE: Main linker script
by chromatic (Archbishop) on Jul 24, 2000 at 00:26 UTC
    Ick, duplicated code. :) You can use a hash, but why not an array?
    my @pages = ( 'frontpage.html', 'aboutus.html', 'login.html', ); # extra comma is there so adding new pages is easier my $num = CGI::param('Num') || 1; page($num - 1); # or put a dummy at the start of @pages sub page { $| = 1; my $num = shift; if (open(FILE, $pages[$num])) { print while <FILE>; } }
    A hash would work too, but if you're using numeric offsets already...

    You could even make page 0 show stats.