I'm having some dramas with understanding the referencing of enums from 'external' files. I think it's a combination of using the enum module and how scoping works in 'library' files... but I haven't been able to get my head 'round it...

I have a single, simple, 'library file' called 'slib.pl' that contains:

package slib; use enum qw(SM_UNK_REC SM_HDR_REC SM_TTL_REC); %SM_REC = ( SM_UNK_REC, "UNK", SM_HDR_REC, "HDR", SM_TTL_REC, "TTL" ); [...] sub ABC { my ($type) = @_; if ($type == SM_HDR_REC) { [...] } elsif ($type == SM_TTL_REC) { [...] }
Now, this all works quite happily for subs in the library - the enums are within the same physical file so they will be visible in the file's name space.

However, I'm having trouble 'seeing' the enums when I try to use a sub in the library from another program. For example I might have a program 'test.pl' like:

require 'slib.pl'; [...] $stat = &slib::ABC(SM_HDR_REC);
...and the SM_HDR_REC isn't translated properly. Initially, this makes sense, as there's nothing called 'SM_HDR_REC' in test.pl... even though I've 'require'ed the file that *does* include it.

So I try:

$stat = &slib::ABC($slib::SM_HDR_REC);
...and it still isn't seen. Finally, I try:

$stat = &slib::ABC(&slib::SM_HDR_REC);
...and it works. ...but I don't understand why the $slib::SM_HDR_REC is wrong. I thought the '&' was only for 'executing' subs and the '$' was used to 'reference' the location where some value is stored.

What can I do about seeing why the '$' doesn't work like I think it should? More simply, is there a better way to do what I'm trying to do, that is be able to use a symbolic name for a piece of data in both the library file AND programs that call the library file?

I'm using ActiveState Perl 5.6.1/Win98 and Perl 5.8.?/WinXP SP2.

Thanks for any pointers.

John


In reply to Problems with the enum Module and File Scoping by ozboomer

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.