...

View Full Version : switch function



Phil Jackson
01-30-2010, 07:59 PM
Hello again, after previous discussion I have been looking into the HTML::template mod.



#!/usr/bin/perl -w
use HTML::Template;
use strict;
use warnings;
use switch;
my (%post, %get, $load_page);

#handle get requests
for (split /\&/, $ENV{'QUERY_STRING'}) { my ($key, $val) = split /=/; $val =~ s/\+/ /g; $val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge; $get{$key} = $val; }
#handle post requests
for (split /\&/, <STDIN>) { my ($key, $val) = split /=/; $val =~ s/\+/ /g; $val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge; $post{$key} = $val; }

# fill in head peramiters
if( defined($get{PAGE_REF}) ) {
switch($get{PAGE_REF}){
case 1 { $load_page = "signup_" }
case 2 { $load_page = "forum_" }
case 3 { $load_page = "manual_" }
case 4 { $load_page = "FAQ_" }
}
}else{
$load_page = "home_";
}

# open the html template
my $template_HEAD = HTML::Template->new(filename => './head/' . $load_page . '_head.tmpl');
my $template_BODY = HTML::Template->new(filename => './body/' . $load_page . '_body.tmpl');
my $template_FOOTER = HTML::Template->new(filename => './footer/' . $load_page . '_footer.tmpl');

#$template_BODY->param(HOME => $ENV{HOME});
#$template_BODY->param(PATH => $ENV{PATH});

# send the obligatory Content-Type and print the template output
print "Content-Type: text/html\n\n", $template_HEAD->output, "\n", $template_BODY->output, "\n", $template_FOOTER->output;


Problem being with the "switch" function I get the error "syntax error near ){"

switch($get{PAGE_REF}){

FishMonger
01-30-2010, 08:34 PM
You misspelled the module name. It's Switch not switch.

If you're using perl 5.10.x, it would be better to use the new switch feature instead of the Switch module. That module was "fine" prior to 5.10 but since it's a source filter, it has some known "buggyness".

You can load the switch feature like this:

use feature "switch";
or to load all new 5.10 features, you'd do this:

use 5.010;

http://search.cpan.org/~rgarcia/perl-5.10.0-RC2/pod/perlsyn.pod#Switch_statements_____

Why are you refusing to follow the recommendations that you were previously given?

FishMonger
01-30-2010, 08:39 PM
I would highly recommend you get Perl Best Practices
Standards and Styles for Developing Maintainable Code

http://oreilly.com/catalog/9780596001735

FishMonger
02-01-2010, 05:33 PM
Another option you might want to consider is instead of using a switch statement, you could do a simple hash lookup.


my %pages = (1 => 'signup_',
2 => 'forum_',
3 => 'manual_',
4 => 'FAQ_',
);

my $load_page = exists $pages{ $get{PAGE_REF} }
? $pages{ $get{PAGE_REF} }
: 'home_';

And moving further down this path are dispatch tables.

http://en.wikipedia.org/wiki/Dispatch_table
http://search.cpan.org/~rlb/Dispatch-Declare-0.1.2/lib/Dispatch/Declare.pm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum