ozboomer has asked for the wisdom of the Perl Monks concerning the following question:
I have a single, simple, 'library file' called 'slib.pl' that contains:
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.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) { [...] }
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:
...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.require 'slib.pl'; [...] $stat = &slib::ABC(SM_HDR_REC);
So I try:
...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.$stat = &slib::ABC(&slib::SM_HDR_REC);
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems with the enum Module and File Scoping
by Errto (Vicar) on Sep 03, 2005 at 02:10 UTC | |
by ozboomer (Friar) on Sep 03, 2005 at 05:56 UTC | |
|
Re: Problems with the enum Module and File Scoping
by ikegami (Patriarch) on Sep 03, 2005 at 02:12 UTC |