bladx

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 href=index.pl?page=>index</a><br>', '<a href=index.pl?page=page2>page2</a><br>', '<a href=index.pl?page=page3>page3</a><br>', end_html(); } sub page2 { print header(), start_html('PAGE_2'), '<p>This is page2!<br><br>', '<a href=index.pl?page=>index</a><br>', '<a href=index.pl?page=page2>page2</a><br>', '<a href=index.pl?page=page3>page3</a><br>', end_html(); } sub page3 { print header(), start_html('PAGE_3'), '<p>This is page3!<br><br>', '<a href=index.pl?page=>index</a><br>', '<a href=index.pl?page=page2>page2</a><br>', '<a href=index.pl?page=page3>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\index.pl 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!

Cheers,
dv


In reply to Re: Improving (the efficiency of) this piece of code by dvergin
in thread Improving the efficiency of this piece of code by bladx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.