!unlike has asked for the wisdom of the Perl Monks concerning the following question:
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. ;)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Declaring a code ref with arguments
by Abigail-II (Bishop) on Aug 05, 2003 at 09:12 UTC | |
by !unlike (Beadle) on Aug 05, 2003 at 09:30 UTC | |
Re: Declaring a code ref with arguments
by broquaint (Abbot) on Aug 05, 2003 at 09:13 UTC | |
Re: Declaring a code ref with arguments
by BrowserUk (Patriarch) on Aug 05, 2003 at 09:13 UTC | |
Re: Declaring a code ref with arguments
by Juerd (Abbot) on Aug 05, 2003 at 10:13 UTC | |
Re: Declaring a code ref with arguments
by cianoz (Friar) on Aug 05, 2003 at 09:16 UTC | |
by !unlike (Beadle) on Aug 05, 2003 at 09:35 UTC | |
by cfreak (Chaplain) on Aug 05, 2003 at 13:35 UTC | |
by cianoz (Friar) on Aug 05, 2003 at 14:55 UTC |