Hi All,

I have written a multipage CGI script using the technique described in recipe 19.12 of the Perl Cook book. Namely put all the HTML generation/program logic in subroutines and store those subs as values of a hash.

This all works fine until you need to pass an argument when declairing the code ref within the hash. When this is done the subroutine is executed (I assume this has to be done because the sub has to be exaluated because of the arg).

This totally breaks my multipage CGI script as one page is only ever displayed. How do I fix this?

Here is an example that demonstrates the problem:

sub print_me { my $arg = shift || "Hello World!\n"; print $arg; } %hash = {no_arg => \&print_me, with_arg => \&print_me("Hello World, again!\n" }; $hash{no_arg}->();

__OUTPUT__

Hello World, again!\n";

I'm using Perl 5.6.1 on Linux

!unlike

I write my Perl code like how I like my sex: fast and dirty. ;)


In reply to Declaring a code ref with arguments by !unlike

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.