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

I have what I think is a simple question. Basically, I pull in the CGI module in one package and want to read cookies in another. If I just require CGI::Cookie in both packages, it doesn't work in the second package. I've been told the solution to this is to define a cgi object in one package and export it to another. That makes sense, but I haven't actually been able to accomplish this.

Before I started trying to figure out packages and Perl's OOP and such, I would put everything in one script and do something simple, like:

require CGI::Cookie; my %cookies = CGI::Cookie->fetch;

Then I tried to do the packages thing. I've split a script into packages test and subtest. In package test, I do:

use vars qw!$cgi $othervars!; use CGI qw/:standard :netscape/; $cgi = new CGI;

In package subtest, I can't figure out how to fetch the cookies using $test::cgi. I don't know what to do in place of my %cookies = CGI::Cookie->fetch.

I think this is a really simple question, and I'm missing something obvious. I've tried to figure it out with the Llama book and the Perl Cookbook, but nothing I do works. If somebody would shed a little light, I would really appreciate it.

Peace.

Replies are listed 'Best First'.
Re: Accessing CGI::Cookie->fetch in package
by Aristotle (Chancellor) on Mar 17, 2003 at 03:15 UTC
    Just make your cookie hash a package global.
    use vars qw(%cookie); require CGI::Cookie; %cookie = CGI::Cookie->fetch;
    Now you can get to it as %Package::cookie.

    Makeshifts last the longest.