Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

efficientcy

by john1987 (Acolyte)
on Jan 26, 2001 at 04:44 UTC ( [id://54445]=perlquestion: print w/replies, xml ) Need Help??

john1987 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I want to make this script more effient. Damian1301, who is my perl god, has shown me one way to do it. I would also like to here what others have to say so here is the code:
#!/usr/bin/perl use CGI qw(param standard); use strict; my $query = new CGI; my $location = $query->param("place"); print $query->header; # im jus doing 2 for quickness if (!$location){ &home } elsif ($location eq ("news")){ &news } sub home{ print "this is home"; } sub news{ print "this is news"; }
Thanks for all of your help. Later. -john

Replies are listed 'Best First'.
Re: efficiency
by runrig (Abbot) on Jan 26, 2001 at 04:57 UTC
    You don't need to import any symbols (the qw(...) stuff on the 'use CGI' line) if you are using the OO interface (calling CGI methods instead of functions). You imply there might be more than two sections, so I might do it this way (slightly more inefficient maybe but I think neater than a big if..elsif..else section):
    #!/usr/bin/perl use CGI; use strict; my $query = new CGI; print $query->header; my %func_map = ( home=>\&home, news=>\&news, ); my $default = "home"; my $location = $query->param("place") || $default; $location = $default unless exists $func_map{$location}; $func_map{$location}->(); sub home{ print "this is home"; } sub news{ print "this is news"; }
      slightly inefficient but yet a work of art as all perl scripts are. thanx for the help. later. -john
Re: efficientcy
by chromatic (Archbishop) on Jan 26, 2001 at 04:51 UTC
    If you use the object form of CGI (with your $query object), you can skip importing tags on the use line.

    Besides that, there's not much to optimize here.

    You ought to use the -w and -T flags, though (see perldoc perltaint).

      thank you for ur help, i was thinking of using a switch (flag) and i probably will. thanks once again... -john
Re: efficientcy
by repson (Chaplain) on Jan 26, 2001 at 06:59 UTC
    If you are making more than a couple of screens then you may like to have a look at CGI::Screen, which is specifically designed for the creation of multi-screen CGIs. I've never used it, but it looks interesting.
Re: efficiency (Spelled it wrong :-)
by damian1301 (Curate) on Jan 26, 2001 at 09:21 UTC
    Well, thank you John. This is the version I gave him for all you people wondering out there.
    #!/usr/bin/perl -w use CGI qw(:standard); use strict; #always! my $location = param("place"); print header; my %subs = ( 'home'=>\&home, # Just add a key and then a value then put your 'news'=>\&news, # stuff in a subroutine :) Yay! ); if (!$location){ &home }elsif (defined(my $action = $subs{$location})){# Thanks Chromatic $action->(); } # Q&A -> Subroutines -> How to make subroutine accessible as foo.pl?fo +o # Thanks again :-) ############### # Subroutines # ############### sub home{ print "this is home. I am John, hear me roar"; } sub news{ print "this is news"; }


    Wanna be perl hacker.
    Dave AKA damian
      Further to this, if this is a high traffic site, then every bit helps. Under mod_perl, you can get a few more executions per second if you collect all your output and print it with one statement. ie:
      #!/usr/bin/perl -w
      
      package Foo;
      use CGI qw(:standard);
      use strict;  #always!
      
      sub handler {
          my $location = param("place");
          my %subs = (
             'home'=>\&home, # Just add a key and then a value then put your 
             'news'=>\&news, # stuff in a subroutine :) Yay!
          );
          my $output="Constant preamble"; 
          if (!$location){
             $output .= &home;
          }
          elsif (defined(my $action = $subs{$location})){
             # Thanks Chromatic 
             $output .= $action->();                                
          }
          print header,$output;
          return;
      }
      # Q&A -> Subroutines -> How to make subroutine accessible as foo.pl?foo
      # Thanks again :-) 
      
      ###############
      # Subroutines #
      ############### 
      sub home{
             return "this is home. I am John, hear me roar";
      }
      sub news{
             return "this is news";
      }
      
      The effect of the single print statement is to collect your network write(s) into a single one or two if possible. While for a cgi executed once in a while, the utility is limited, it is a dramatic benefit on UF where a given CGI will run literally 30 times a second, so every write to the tcp/ip stack matters, big time.
      --
      Jay "Yohimbe" Thorne, alpha geek for UserFriendly

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54445]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-16 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found