in reply to Improving the efficiency of this piece of code


In asking for help making this code more efficient, you imply that this code runs. It does not. Let's work on that first.

First, you have inadvertantly picked a subroutine name that conflicts with with a built-in Perl function. 'index' is a perl function and so Perl cannot see your 'index' subroutine. Let's name it index_page.

Next, your test:

   if (param('page') eq "") coughs up an error if param('page') does not exist as is the case after the index_page() subroutine. So we reword that as:

   if ( ! defined(param('page')) ) Now your script looks like this:

#!/usr/bin/perl -wT use strict; use CGI ':all'; use CGI::Carp qw(fatalsToBrowser); if ( ! defined(param('page')) ) { index_page(); } elsif (param('page') eq "page2") { page2(); } elsif (param('page') eq "page3") { page3(); } sub index_page { print header(), start_html('INDEX'), '<p>This is the index page!<br><br>', '<a>index</a><br>', '<a>page2</a><br>', '<a>page3</a><br>', end_html(); } sub page2 { print header(), start_html('PAGE_2'), '<p>This is page2!<br><br>', '<a>index</a><br>', '<a>page2</a><br>', '<a>page3</a><br>', end_html(); } sub page3 { print header(), start_html('PAGE_3'), '<p>This is page3!<br><br>', '<a>index</a><br>', '<a>page2</a><br>', '<a>page3</a><br>', end_html(); }
Which runs and happily bounces back and forth to the different virtual pages when you hit the various links in your browser.

Hope this helps. I don't see any obvious 'efficiency' issues here.

Upward and onward. Keep at it. You have good, clean presentation but you did not test this code before posting it. If you had, your question would have likely been something like: "Why do I get...

Not enough arguments for index at C:\DocsMisc\PerlCode\ line 8... etc. etc.'
...when I run this script?"

The things I have mentioned here, I learned by reading the error messages your code produced, fixing the error, running again, fixing the next thing, etc. Error messages are your friends. Very helpful little beasties!