You could put all the link particulars in a module so that changes can be made in one place. Here is a Links.pm to give you ideas. It would be better to go a step further and have the module read a configuration file, but I didn't want to spoil all your fun.

If you only have a handful of links to maintain, a database may be overkill. By putting this in a module, you should be able to make the change from a config file to database with little, if any, change in the calling scripts.

#!/usr/bin/perl package Links; use strict; use CGI qw(:standard); my $Links = { musings => { text => 'Musings', link => '/cgi-bin/musing.cgi', id_list => [ 'musing_id', ], }, rants => { text => 'Rants', link => '/cgi-bin/rants.cgi', id_list => [ 'rant_id', 'ref_id', ], }, stuff => { text => 'Other Stuff and Junk', link => '/cgi-bin/misc.cgi', id_list => [ ], }, }; #----------------------------------------------------------- sub make { my $key = shift; my $vals = shift; (defined($Links->{$key})) or return; my $info = $Links->{$key}; my @id_list; for my $id (@{$info->{id_list}}) { if (defined($vals->{$id})) { push @id_list, "$id=$vals->{$id}"; } else { # what if? } } my $url = $info->{link}; if (@id_list) { my $id_str = join('&', @id_list); $url = "$url?$id_str"; } return a({-href=>$url}, $info->{text}); } 1;
#!/usr/bin/perl use strict; use Links; my $vals = { musing_id => 42, rant_id => 1, ref_id => 2, foo_id => 3, }; my $link = Links::make('musings', $vals) or die; print "$link\n"; my $link = Links::make('rants', $vals) or die; print "$link\n"; my $link = Links::make('stuff', $vals) or die; print "$link\n";

In reply to Re^3: How's My Style Now? by YuckFoo
in thread How's My Style Now? by Spenser

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.