Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

global variables defined in an external file

by fionbarr (Friar)
on May 06, 2013 at 15:26 UTC ( [id://1032324]=perlquestion: print w/replies, xml ) Need Help??

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

hi I have a program in which I need to declare a great many global variables. I would prefer to do this in an external .pm file so as not to clutter up my procedural code. I've tried many permutations of Export with no success. Any thoughts please?
  • Comment on global variables defined in an external file

Replies are listed 'Best First'.
Re: global variables defined in an external file
by choroba (Cardinal) on May 06, 2013 at 15:49 UTC
    Without seeing any code or signs of "no success", one can only speculate. You probably did not use Exporter correctly.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: global variables defined in an external file
by Old_Gray_Bear (Bishop) on May 06, 2013 at 16:00 UTC
    Whenever I see 'global variables' I immediately think "Configuration File".

    There are many, many modules on CPAN in the Config name-space. My particular favorites are Config::Simple and Config::Tiny.

    Isolating your global variables into one place and explicitly calling that place 'configuration' is a Goodness for the maintainer.

    ----
    I Go Back to Sleep, Now.

    OGB

      got it! thanks
      package x; use warnings; use strict; use Exporter 'import'; our @EXPORT = qw/$a_template $sc/; our $a_template = "a12 a10 a5"; our $sc = 'All'; our @ar = qw/the variables/; our %ha = (exported => ' succesfully.'); 1;
      many thanks
Re: global variables defined in an external file
by NetWallah (Canon) on May 06, 2013 at 15:50 UTC
    Show us the last thing you tried, or preferably, some small sample code that compiles, and represents what you are trying to do.

    Alternatively, read up and follow the instruction in Exporter.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      use strict; use warnings; package x; use Exporter; my $VERSION = 1.00; my @EXPORT = (); my @ISA = qw(Exporter); my $a_template = "a12 a1 a4 a11 a2 a10 a12 a20 a15 a15 a1 a10 a1 a1 a +1 a2 a2 a1 a3 a3 a9 a1 a15 a2"; @EXPORT = qw(xxx $a_template); sub xxx {print "in package x\n";} 1;
        In addition to aitap's correction, I would recommend that you follow Exporter's advice:
        Exporting variables is not a good idea. They can change under the hood, provoking horrible effects at-a-distance, that are too hard to track and to fix. Trust me: they are not worth it.

        To provide the capability to set/get class-wide settings, it is best instead to provide accessors as subroutines or class methods instead.

                     "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
                -- Dr. Cox, Scrubs

        my creates a block-scoped variable, while you need to create (using vars or our, as already suggested) a package-scoped variable (available in the symbol table), so it actually can be exported.

        Read Coping with Scoping for more information on this topic.

Re: global variables defined in an external file
by Anonymous Monk on May 06, 2013 at 15:48 UTC

    Really, the question to ask is how many are the global variables? I know it's good practice not Export variables, but if it must be done, you can do: The pm file:

    package Def; use warnings; use strict; use vars qw(@ISA @EXPORT $foo $bar $boo); BEGIN{ require Exporter; Exporter->import(); } @ISA = qw(Exporter); @EXPORT = qw($foo $bar $boo); 1;
    The file which uses the pm file
    #!/usr/bin/perl use warnings; use strict; use Def; # the variable defined module $foo = 'you can\'t foo me'; $bar = 'Let\'t meet in the bar'; $boo = 'The fans boo you today'; print $boo,$foo,$bar;

      It is the ohter way round:

      Def.pm:

      package Def; use warnings; use strict; use Exporter 'import'; our @EXPORT = qw/$sc @ar %ha/; our $sc = 'All'; our @ar = qw/the variables/; our %ha = (exported => ' succesfully.'); __PACKAGE__
      The script:
      #!/usr/bin/perl use warnings; use strict; use Def; print "$sc @ar ", %ha;
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      thanks for your very clear response....however, I'd like to do this
      $foo = 'you can\'t foo me'; $bar = 'Let\'t meet in the bar'; $boo = 'The fans boo you today';
      in the module and reference these variables in the calling program as: print $boo, etc.
Re: global variables defined in an external file
by Laurent_R (Canon) on May 06, 2013 at 23:22 UTC

    Hum, just one question: are you really sure that you need all these global variables?

    I do not have a religious zeal against global variables, but I know by experience it is often better to avoid them when possible (provided this does not lead you to stupidly complicated contortions). But still, almost every time I am using one, I am asking myself whether this is the best way to go.

Re: global variables defined in an external file
by fionbarr (Friar) on May 06, 2013 at 15:44 UTC
    that is: Exporter
Re: global variables defined in an external file
by educated_foo (Vicar) on May 07, 2013 at 14:05 UTC
    hi I have a program in which I need to declare a great many global variables.
    You don't need to declare global variables in Perl, but if you want to initialize them, just do so in "file.pl", then put do "file.pl"; at the top of your program. (BTW, if you use the phrase "global variable" on this site, you'll probably get a cascade of useless boilerplate answers.)
    Just another Perler interested in Algol Programming.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1032324]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found