Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Understanding using external program data

by NovMonk (Chaplain)
on Feb 14, 2007 at 15:59 UTC ( #599971=perlquestion: print w/replies, xml ) Need Help??

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

Kind Monks,

I've read the chapters on this in Beginning Perl and some of the tutorials on this site, but I'm just not getting it. I need to use an external file as a lookup table. It's a simple hash. No subroutines, just the hash. When I turn Off "use strict;" in my main program, it runs and returns values for me. But not using strict is Bad and Evil and I want to do this right. More importantly, I want to understand why it's right and where I can go to read more until the light does come on. Looking at the examples I can find, they all seem to be Object Oriented, focusing on using subroutines, and much more complex than this simple thing I'm trying to do.

Here's my program:

#! /usr/bin/perl use strict; use warnings; require "/usr/local/include/dpcs_stdlib.pl"; require "/usr/local/include/dpcs_delim.pl"; use lookup; open INFILE, "rentroll.txt" or die "Could not open rentroll.txt $!"; open OUTFILE, ">parsed.rnt.txt" or die "could not open parsed.rnt.txt +for write $!"; my $line= q{}; my @Page = (); #my $lastLine = q{Ending Balance:}; #my $new_rec = 0; #my $cnt = 0; #my (@pos_nums, @neg_nums); while ($line = <INFILE>) { chomp $line; push @Page, $line; if ($line =~/^Development:/) { my $devel = substr($line,13); print OUTFILE "Development = $devel\n"; my $check = $devel_table{$devel}; print OUTFILE "check = $check\n"; if ($check =~ "M") { print OUTFILE "Middletown\n"; } elsif ($check =~ "H") { print OUTFILE "Hamilton\n"; } else { print OUTFILE "Community not in lookup table\n"; } } #last if ($line =~ /$lastLine/); }

And my lookup.pm:

#!/usr/bin/perl use strict; use warnings; #require Exporter; #our %devel_table; #our @EXPORT = qw(%devel_table); #our @ISA = qw(Exporter); our %devel_table; %devel_table = ("Townhomes West" => "M", #lots more values in here, of course "Winding Creek Family" => "H", "Dayton Lane Gardens" => "H"); 1;

The error I get when I turn strict on in the main program is this:

Variable "%devel_table" is not imported at .//sopw.pl line 29. Global symbol "%devel_table" requires explicit package name at .//sopw +.pl line 29. Execution of .//sopw.pl aborted due to compilation errors.

I believe it has something to do with how I'm not exporting the information correctly, but as I said, the only examples I find are for exporting subroutines, not a hash by itself. Having the lines above commented in or out makes no difference to how the program runs, with or without strict.

Thanking you in advance for leading me back to the right path,
NovMonk

Replies are listed 'Best First'.
Re: Understanding using external program data
by SheridanCat (Pilgrim) on Feb 14, 2007 at 16:16 UTC
    You should make lookup.pm a module and "use" it. Here's an example:
    #!/usr/bin/perl use strict; use warnings; use Lookup; use Data::Dumper; print Dumper \%devel_table;
    And then the Lookup.pm module (I usually capitalize modules):
    package Lookup; use strict; use warnings; require Exporter; our @EXPORT = qw(%devel_table); our @ISA = qw(Exporter); our %devel_table = ("Townhomes West" => "M", #lots more values in here, of course "Winding Creek Family" => "H", "Dayton Lane Gardens" => "H"); 1;
      So, in the module, you Don't use the #! line-- you say "package <packagename>;". Such a simple thing, but I just couldn't see it. Everything works now. Thanks!
        Oh dear, that's a very superficial description of the situation.

        The "hashbang" line in a script and a "package" line in a module have very little in common. One is Perl, the other isn't, for one essential difference. Each can usefully appear in the other kind of file, so they're not even distinctive.

        Please read up on the function of each. See perldoc perlrun for the hashbang line and perldoc perlmod (plus perldoc -f package for the package statement.

        Anno

Re: Understanding using external program data
by philcrow (Priest) on Feb 14, 2007 at 16:10 UTC
    Exporter will only work if you 'use' the external module. Using it with 'require' does not invoke the import method which puts the export list into the main namespace. You should probably rename the script with the hash as .pm and 'use' it.

    Phil

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2023-09-26 04:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?