Hi again,

I'm trying to factor out the declaration and setting of a bunch of variables that a suite of programs will need, as well as defining some subroutines that will be needed. I've been mainly referencing Programming Perl 5, but I am mighty confused at this point.

I'm particularly confused about how variables that are declared in my Package manage to become visible to main (although that's exactly what I want to happen). As well as the whole issue of what happens when (at BEGIN, at use, etc.). I feel sure I've greatly complicated things; as I say, all I want is a way to initialize a handful of "global" variables, and provide a few subroutines.

When I ran this, here's the output I got:

$ testmod
Hello from Dummy.BEGIN
Hello from testMod.BEGIN
Global symbol "$g_project" requires explicit package name at ./testmod line 33.
Execution of ./testmod aborted due to compilation errors.
$

Could anyone tell me what subset of Perl modules I need to be concerned with, and what basic structure to follow, to accomplish this?

'testmod' is the main program; Dummy.pm is the module....

Thanks very much,
Chap Harrison


testmod:
----------

#!/usr/bin/perl

use strict;
use warnings;

#--- BEGIN runs before the rest of testMod gets compiled. One of the things
#    that BEGIN does is to 'use Dummy', which causes Dummy.pm to be
# compiled, which causes certain 'my' variables in Dummy to be defined. # These variables somehow become accessible to testMod, which is odd but nice: # the main body of testMod references these variables, so they need to be
#    defined at this point.
#---

BEGIN {                         # This BEGIN runs after Dummy.pm's BEGIN
    print "Hello from testMod.BEGIN\n";

#--- The path to my development library is stored as an ENV variable

use lib "$ENV{LMI_DEVELOP_PATH}"; # This updates @INC early in runtime
    use Dummy qw/start/;              # enabling Dummy.pm to be found
}

#--- Main code

my $program = qx(basename $0);
chomp $program;
print "Hello from $program.main\n";

#--- P U L L   A N D   V A L I D A T E   A R G U M E N T S

# This compiles okay, so obviously $g_project is declared at this point.

$g_project = "VA-Orange"; # Simplification; actually passed as cmdline arg

start();                        # Call Dummy initialization routine

print "And now, goodbye from $program\n";

Dummy.pm
---------------

package Dummy;

use strict;
use warnings;

BEGIN {
    print "Hello from Dummy.BEGIN\n";
}
use Exporter;

our @ISA = qw/Exporter/;
our @EXPORT_OK = qw/start/;
our $VERSION = "0.0.1";

my $g_project;                  # Where should I live??

sub start {

#--- C O N S T A N T S   A N D   G L O B A L S

print "Hello from Dummy::start() - looks like project='$g_project'\n";

    my $gk_env_databases_path = "LMI_DATABASE_PATH";

$ENV{$gk_env_databases_path} = "~/%s"; # fixup env. for purposes of test

    my $g_all_databases_path = $ENV{$gk_env_databases_path};
    die "Environment variable '$gk_env_databases_path' is not set.  "
        . "Need to run lmi-admin.\n"
        if ! defined $g_all_databases_path;

    lop(\$g_all_databases_path);

    my $g_database_path = sprintf $g_all_databases_path, $g_project;

    if ( ! -e $g_database_path ) {
        print "The district database, $g_database_path, does not exist.\n"
            . "Did you specify the project name correctly? ($g_project)\n"
            . "\nExiting.\n\n";
        exit;
    }

}
#--- S U B R O U T I N E S

sub lop {
    my $path_ref = shift;
    ${$path_ref} =~ s[/+$][];   # lop off any trailing slashes
    return;
}

1;                              # Succeeded in loading module


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to