Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I tried to add a parameter to the following code and it does not work.

Thats because that code is unorganized and full of "Red flags".

Organize your code, box up everything into subroutines (Every loop, every heredoc template, every giant if/else block).

This is a "Red flag" localtime ... $year + 1900, . Time::Piece and DateTime is what you want to use for date stamps/math, none of this +1900 stuff.

You would benefit heavily from reading "Red flag" as it was written exactly for you. Almost every example is one you have posted here.

Subroutines should take arguments not work on globals. You shouldn't mix who prints from where....

This is something like what your program should end up looking like

Main( @ARGV ); exit( 0 ); sub Main { my $pms = empire->new; my $query = CGI->new; my( $header, $body ) = Dispatch( $pms, $query ); print $header, $body; } sub Dispatch { my( $pms, $q ) = @_; if( $pms->authenticate( $q ) ){ my $page = $q->page || 'default'; return ThisPage( $pms, $q ) if $page eq 'thisone'; return ThatPage( $pms, $q ) if $page eq 'thatone'; return DefaultPage( $pms, $q ); } else { return UnauthorizedPage( $pqms, $q ); } } sub UnauthorizedPage { my( $pms, $q ) = @_; return RedirectTo($q, 'notloggedin.cgi'); } sub RedirectTo { my( $q , $page ) = @_; my $header = $q->redirect( UrlFor( $page ) ); my $body = ''; return $header, $body; } sub UrlFor { my( $name ) = @_; return $name; } sub DefaultPage { my( $pms, $q ) = @_; my $members = $pms->list_members; my $schedule = $pms->campaign_schedule; return $q->header, CampaignMembersHtml( $pms, $q, $members, $sched +ule ); } sub CampaignMembersHtml { my( $pms, $q, $members, $schedule ) = @_; my $html = MembersHtml( $q, $members ); $html .= ScheduleHtml( $q, $schedule ); return $html; } sub MembersHtml { ...; } sub ScheduleHtml { my( $q, $schedule ) =@_; my ( $status, $schedule_date, $max_emails, $clast60, $aolflag, $openflag, $yaho­o_flag, $shour, $smin, $old_mid, $old_dname, $cname, ) = @$schedule; ...; return $html; } sub Empire::authenticate { my( $pms, $query ) = @_; my $user = $query->param('user'); my $pass = $query->param('pass'); return !! $pms->check_security( $user, $pass ) } sub Empire::list_members { my( $pms, $q ) = @_; ... my $members = $dbh->selectall_arrayref($sql); ... return $members; } sub Empire::campaign_schedule { my( $pms, $q ) = @_; ... my $schedule = $dbh->selectall_arrayref($sql); ... return $schedule; }

In reply to Re: Object Identifier? (red flags, more subs) by Anonymous Monk
in thread Object Identifier? by damfer21

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2024-03-29 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found