Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: switch function

  1. #1
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts

    switch function

    Hello again, after previous discussion I have been looking into the HTML::template mod.

    Code:
    #!/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}){
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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:
    Code:
    use feature "switch";
    or to load all new 5.10 features, you'd do this:
    Code:
    use 5.010;
    http://search.cpan.org/~rgarcia/perl...tatements_____

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

  • Users who have thanked FishMonger for this post:

    Phil Jackson (01-30-2010)

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    I would highly recommend you get Perl Best Practices
    Standards and Styles for Developing Maintainable Code

    http://oreilly.com/catalog/9780596001735

  • Users who have thanked FishMonger for this post:

    Phil Jackson (01-30-2010)

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Another option you might want to consider is instead of using a switch statement, you could do a simple hash lookup.
    Code:
    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...tch/Declare.pm


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •