I'm creating my first module, to reduce code redundancy in a project with several scripts.
I have read perldocs
perlmod perlmodlib the
Simple Module Tutorial and done a search on
Google's advanced search, but haven't figured out yet what's going wrong.
I want a sub-routine in my module to access variables in main (and I would also like to be able to access variables in my config file with calls such as $conf->{'db_database'} but that isn't working either.
test.pl
#!/usr/bin/perl -wT
use strict;
use lib '/home/4220/straitwa/www.straitway.net/shop_34/lib';
use Shop;
my (%conf, $conf, $dbh, $sth, $rc, @db_conf);
#open the config file and parse it
unless ($conf = do ('/home/4220/straitwa/www.straitway.net/shop_34/con
+f.pl')) {
die ("Could not open config file");
}
my $one = $conf->{'db_database'};
print "Print - ", $one, " - from the main package\n";
Shop::db_connect;
The module Shop.pm
package Shop;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(db_connect);
sub db_connect {
print "Print - ",$main::one, " - from the Shop package\n";
}
1;
The resulting output is
Print - straitway_1 - from the main package
Use of uninitialized value in print at /home/4220/straitwa/www.straitw
+ay.net/shop_34/lib/Shop.pm line 13.
Print - - from the Shop package
Any pointers? Thanks.
Edited by boo_radley : corrected anchor tag
edited - 17 June 2002 (footpad): Corrected another A tag problem.</small
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.