Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Improving (the efficiency of) this piece of code

by dvergin (Monsignor)
on Feb 25, 2001 at 06:16 UTC ( [id://60713]=note: print w/replies, xml ) Need Help??


in reply to Improving the efficiency of this piece of code

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-06-25 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.