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.


In reply to Accessing CGI::Cookie->fetch in package by Anonymous Monk

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.