Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

storing data in a package/module

by nmerriweather (Friar)
on Oct 29, 2003 at 18:14 UTC ( [id://303072]=perlquestion: print w/replies, xml ) Need Help??

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

i'd like to use a package/module to store certain data

good form says 'use my' when declaring variable

in practice, using 'my' keeps me from addressing the variable in the package. from my main script.

without the 'my', i can handle that data fine.

there has to be something better than not using 'my' in this case. and it doesn't seem to be 'local'.

pointer please?

Replies are listed 'Best First'.
Re: storing data in a package/module
by particle (Vicar) on Oct 29, 2003 at 18:18 UTC

    try use vars or our

    ~Particle *accelerates*

      use vars and my are simplest. Another option that might be better in some situations (or worse in others) is to use a lexical (my) variable, and have functions that get/set/return a reference to it.

      ------------
      :Wq
      Not an editor command: Wq
Re: storing data in a package/module
by Ovid (Cardinal) on Oct 29, 2003 at 19:45 UTC

    I would use Exporter and create accessors for the data rather than accessing the data directly.

    package Foo; use warnings; use strict; use base 'Exporter'; our @EXPORT_OK = qw( some_data some_more_data fribble ); sub some_data { 'foo' } sub some_more_data { 'bar' } my %gnarfles = ( this => 1, that => 'one' ); sub fribble { my $key = shift; if (exists $gnarfles{$key}) { return $gnarfles{$key}; } else { die "No such value ($key)"; } } 1;

    And in your actual program:

    use Foo qw'fribble some_data'; my $result = fribble($key);

    By properly encapsulating your data in accessors, you make it much easier to extend and make it easier to validate. Also, by adding it to @EXPORT_OK instead of @EXPORT, you ensure that people only import the functions that they need and, more importantly, it is very easy to track down where the function came from.

    Cheers,
    Ovid

    New address of my CGI Course.

Re: storing data in a package/module
by hmerrill (Friar) on Oct 29, 2003 at 19:12 UTC
    package Private_Stuff; use strict; use CGI::Carp qw(cluck); ### for log_error subroutine use Time::Local; ### for datetime_to_epoch_secs subroutine use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $db_name); require Exporter; @ISA = qw(Exporter AutoLoader); # Items to export into callers namespace by default. Note: do not expo +rt # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. @EXPORT = qw(); @EXPORT_OK = qw(); $VERSION = '0.01'; $db_name = "Some_Database_Name"; 1; ### end of module ------------------------------------ test.pl ------- use Private_Stuff; print "The database name is ", $Private_Stuff::db_name, "\n";
    There are other ways to do that - you should read up on creating and using modules by doing
    perldoc perlmodlib
    at a command prompt.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found