Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Venerable Monks:

I'm trying to retrieve cookie data globally but I'm having problems recalling it in another script.

use base 'TheLib::Foo'; sub cookie1 : Runmode { $cgi_query = new CGI; $cookie1 = $cgi_query->cookie('sessioninfo'); { our $cookie1; print $cookie1;}} sub cookie2 : Runmode { $cgi_query = new CGI; $cookie2 = $cgi_query->cookie('visitcount'); { our $cookie2; print $cookie2;}} 1;
-Foo.pm-
use HTTP::Template; sub my_infosheet { my $self = shift; my $template = $self->load_tmpl('my_infosheet'); (undef, my $info) = split(/\//, $ENV{'PATH_INFO'});
Which should I use at this point in the script to recall the cookie data?

1. $cookie1 = $cookie1->cookie('sessioninfo')
$cookie2 = $cookie2->cookie('visitcount')

2. $cookie1 = our $cookie1->cookie('sessioninfo')
$cookie2 = our $cookie2->cookie('visitcount')

$template->param(COOKIE1 => $cookie1); $template->param(COOKIE2 => $cookie2); return $template->output; }
Thanks in advance.

Replies are listed 'Best First'.
Re: Making & Recalling Globals
by moritz (Cardinal) on Sep 22, 2010 at 06:39 UTC
    Why not simply return the values, instead of assigning them to some global data structure?

    Global data makes programs hard to understand, debug and reuse, and should be avoided when possible.

    Perl 6 - links to (nearly) everything that is Perl 6.
      Only because I don't know how I would return the dynamic values 'through' a script into the template.
Re: Making & Recalling Globals
by dasgar (Priest) on Sep 22, 2010 at 05:10 UTC

    If I understand correctly, your trying to:

    1. Create a new module.
    2. Call that module from a script.
    3. From that script, call a variable that is defined in the new module that you created.

    If that's correct, I'm probably shouldn't be offering advice, but I'll try anyways. I've never written my own module, but I think what you're needing to do is export the variable/function. I believe that perlmodlib would probably have some useful information. You'll want to look for 'exporting' instead of 'globals'.

      Thanks! I'll take a look.
Re: Making & Recalling Globals
by Anonymous Monk on Sep 22, 2010 at 06:38 UTC
    Why note store it in the object, probably a hash, which these methods share? (Incidentally, those Runmode subs should probably be shifting off a $self, too).

    Note that if you do try to store values in package variables declared with our, you'll run into severe troubles if you instantiate more than one object of this class at a time.

      I'll try the shifting off $shelf.
      "Note that if you do try to store values in package variables declared with our, you'll run into severe troubles if you instantiate more than one object of this class at a time."
      Oh, that's helpful. I thought that I had found the solution. Back to the tutorials :-)