Is there any chance you could post your code to this? It's a little difficult to tell what you mean without seeing it, to be honest.

If you're scoping the variable with 'my' in order to get your script to work under 'use strict' then yes, $main::COOKIE_ID would help. 'My' stops the variable existing after the routine exits, but $main::COOKIE_ID is the global variable.

A cleaner way would be to return the values to the main routine, so something like the following would happen... again tricky to show without knowing what this is being draped around, but here goes.

use strict; ... # Call the subroutine to get the info. my ($cookieid, $cookiename) = getCookieInfo(); ... # The subroutine itself. sub getCookieInfo { # Keep these localised... my ($cookieid, $cookiename); ... # ... and then return them. return ($cookieid, $cookiename); }

Still possibly cleaner ways would be to return hashes, with the keys of id, name etc and the suitable values. Slightly less transparent than the above though so I'll stick to just describing the above, but easy enough to get to here from there. This is especially useful when you're getting a lot of return values in my experience.


In reply to Re: Setting Global Variable in Sub by Molt
in thread Setting Global Variable in Sub by sshevlin

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.