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

Pardon this posting which'll probably display my ignorance of how use and namespaces work...

I have a system of cooperating perl scripts that all use a small number of critical parameters -- my so called "big levers", as moving any of them slightly has a big effect on final conclusions. I put these variables in a .pm file and  use them in my different scripts. (see sample below) One of my scripts generates a plethora of excel reports. As these levers matter so much, I'd like to include these big levers at the bottom of every report, to show the scenario the report reflects.

My questions is:

After a script does  use BigLevers;, how can I examine the namespace to determine which global vars the  use set? In this toy example, the answer I'm seeking is the list $NETOVERGROSS_ORDS, $NETOVERGROSS_DOLS, $FNEW_GROSS_ORDS, $FNEW_GROSS_DOLS, so I then I can pump these vars and their values onto my reports.

Parsing these names out of the physical file doesn't seem elegant, surely I can grab them from Perl itself after they've been loaded?

nop
## The Big Levers use strict; use vars qw($NETOVERGROSS_ORDS $NETOVERGROSS_DOLS $FNEW_GROSS_ORDS $FNEW_GROSS_DOLS); $NETOVERGROSS_ORDS = .98; $NETOVERGROSS_DOLS = .97; $FNEW_GROSS_ORDS = .35; $FNEW_GROSS_DOLS = .35; 1;

Replies are listed 'Best First'.
Re (tilly) 1: What vars did it define?
by tilly (Archbishop) on Dec 04, 2000 at 19:47 UTC
    Use Exporter. And package namespaces. Really.

    If you do that, then the list of variables you want is just everything in the arrays @EXPORT and @EXPORT_OK. (In whatever package your module uses.)

      I'd like to throw my support behing this suggestion.

      To add to the advantages, you would no longer have to worry about whether all of your instances of use BigSwitches were done from the same package. Currently, package main; use BigSwitches and package MyModule; use BigSwitches would create two distinct sets of variables that won't stay in sync.

              - tye (but my friends call me "Tye")
Re: What vars did it define?
by snax (Hermit) on Dec 04, 2000 at 19:03 UTC
    Quick 'n' dirty solution: Put your Big Levers in a hash, then do keys on the hash to see what has been defined:
    $BigLever{NETOVERGROSS_ORDS} = .98
    ...etc, in your module, then
    foreach my $key (keys %BigLever) { print "$key defined with $BigLever{$key}\n"; }

    (or whatever) in your code.

Re: What vars did it define?
by davorg (Chancellor) on Dec 04, 2000 at 19:17 UTC

    I'm assuming that the use BigLevers; statement has exported it's variables into your namespace using the standard Exporter mechanisms. In that case, the exported variables will now have entries in your current package's typeglobs.

    Assuming thatyou're working from the main package, you can do something liek this to find out which variables have been exported to your package:

    if (defined *{$main::{NETOVERGROSS_ORDS}}{SCALAR}) { # $NETOVERGROSS_ORDS exists } else { # $NETOVERGROSS_ORDS doesn't exist. }

    If messing about in typeglobs doesn't thrill you, then you can use the Devel::Symdump module which will do it all for you.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      You assume too much! (grin) Let me repeat my apology for ignorance here...
      The code snippet above is it, in its entirety... no exporter, nothing fancy. Just what you see, and used via  use BigLevers; in the other scripts. Your comment suggests I'm not doing this right, which I suspected, which is why I posted seeking advice... any better approaches / tips most welcome! Thanks --

      nop

        Oops. Sorry for overcomplicating :)

        No Exporter, but no different package either - so there's no need to export stuff.

        I think you'll find that my suggestion will still work in that case, as the variables will be created in your current package.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: What vars did it define?
by clemburg (Curate) on Dec 04, 2000 at 20:03 UTC

    You might want to use this:

    #!/usr/bin/perl -w use strict; for my $pkg (@ARGV) { eval "use $pkg;"; no strict 'refs'; my @candidates = keys %{$pkg . "::"}; for my $c (@candidates) { if (defined(${$pkg . "::" . $c})) { print "$pkg" . "::" . "$c = " . ${$pkg . "::" . $c}, "\n"; } } }

    Run on some input:

    > perl try.pl File::Find File::Find::dont_use_nlink = 1 >perl try.pl XML::Parser XML::Parser::VERSION = 2.27 XML::Parser::have_LWP = 1

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: What vars did it define?
by merlyn (Sage) on Dec 04, 2000 at 19:04 UTC
    The source code to the debugger, while being Ilya-encrypted, can give you everything you need. In particular, look around for the code that handles the V command. I think it's something like dumpvar.pl.

    -- Randal L. Schwartz, Perl hacker