After you create the C::A instance, you can always stick it in a global variable:
my $cgi = new CGI::Applicaton(...); $::global_var = $cgi; ... use $::global_var now anywhere ...

A variable like $::global_var is a fully-qualified name which is as close as Perl gets to global variables. It is equivalent to $main::global_var or to using our in package main:

... { package main; our $global_var; # same as $::global_var and $main::global_var ... }

You can create a tie interface if you want to access parameters like you did with a hash. See man perltie for more details. However, if you are writing new CGI applications, I wouldn't do this. Just extract parameters into simple variables if you need to use them more than once. A tie interface would allow you to use hash-like syntax for the following operations:

print $hash{'key'} # same as print $webapp->param('key') $hash{'key'} = 3; # same as $webapp->param('key', 3);
However, I would make sure it is really worth implementing because it will add complexity and degrade your application performance.

If you really need to access the values as a hash, perhaps you can first extract all the CGI parameters to a hash like this:

our %hash; my @all_params = $webapp->param(); for my $p (@all_params) { $hash{$p} = $webapp->param($p); # } ... use %hash instead of calling $webapp->param(...) ...

In reply to Re: CGI::Application - alternative to using $self->param? by pc88mxer
in thread CGI::Application - alternative to using $self->param? by isync

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.