CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Accessing package variables

 



PapaGeek
User

Mar 2, 2014, 12:08 PM

Post #1 of 3 (1569 views)
Accessing package variables Can't Post

 
How do I access an array of hashes that is declared as global in a package?

Here is a stripped down version of the package:


Code
use Modern::Perl '2013'; 
use Win32::GUI();
use DBI;
use GetWeb;

my @tickerSymbolsHashArray;

package DBaccess;

sub LoadDataBase
{
my $hash_ref;
my $dbh = DBI->connect('dbi:ODBC:Funds', '', '', { RaiseError => 1, odbc_cursortype => 2});
my $sth = $dbh->prepare('SELECT * FROM Symbols');
$sth->execute;
while ( $hash_ref = $sth->fetchrow_hashref) {
my %dataHash;
$dataHash{"ticker"} = ${$hash_ref}{Ticker};
$dataHash{"tickerID"} = ${$hash_ref}{ID};
$dataHash{"asOf"} = ${$hash_ref}{AsOf};
$dataHash{"p1w"} = ${$hash_ref}{p1w};
push (@tickerSymbolsHashArray, \%dataHash);
}
print scalar (@tickerSymbolsHashArray) . "\n";

}


I also created a separate perl file that will eventually update the database and global hash array. Right now all I want to do is print the data in the hash reference array to make sure I can see it outside the package. Here is the update perl file:

Code
use Modern::Perl '2013'; 
use Win32::GUI();

use DBaccess;
use DateTime;

sub UpdateMarkets
{
my $marketDate = @_;

print "U P D A T E\n";
print scalar (@DBaccess::tickerSymbolsHashArray) . "\n";

for (my $index = 0; $index < scalar (@DBaccess::tickerSymbolsHashArray); $index++)
{
my $hashRef = $DBaccess::tickerSymbolsHashArray[$index];
print $$hashRef{"tickerID"} . " " . $$hashRef{"asOf"} . "\n";
}

print "E N D\n";

}


The initial calling sequence from the mainline is:


Code
use Modern::Perl '2013'; 
use Win32::GUI();

use DBaccess;
use DateTime;

main();

sub main
{
do 'UpdateMarkets.pl';

DBaccess::LoadDataBase();

my $testDate = DateTime->now(time_zone => 'local')->subtract(days => 50);
UpdateMarkets($testDate);


The output of the process is:


Code
 1: PPCIX    1  2014-01-01 00:00:00    2.4 
2: NSEIX 1 2014-01-01 00:00:00 5.0
3: FSCRX 1 2014-01-01 00:00:00 1.5
4: POGRX 1 2014-01-01 00:00:00 8.0
5: PSSIX 1 2014-01-01 00:00:00 6.0
6: OAKIX 1 2014-01-01 00:00:00 3.0
7: DVLRX 2 2014-01-01 00:00:00 9.0
8: GETGX 2 2014-01-01 00:00:00 12.0
8
U P D A T E
0
E N D

The process reads in the 8 test records that I have in my database table and the “8” just about the word update comes from the last print statement within the package. The “0” after the work update comes from trying to access this global data from outside the package.

What is the proper way to access global data outside the package?”


FishMonger
Veteran / Moderator

Mar 2, 2014, 1:45 PM

Post #2 of 3 (1563 views)
Re: [PapaGeek] Accessing package variables [In reply to] Can't Post

@tickerSymbolsHashArray is not a global array. It's a lexical array which has file scope in the module where it's declared.

To make it a global array and access it with its FQN you need to use the 'our' keyword instead of the 'my' keyword.


Code
package Example; 

use Modern::Perl '2013';

our @tickerSymbolsHashArray = qw( 1 2 3 4 a b c d);

1;


test.pl

Code
#!/usr/bin/perl 

use Modern::Perl '2013';
use Example;

say scalar @Example::tickerSymbolsHashArray;


ouput:
D:\test>test.pl
8

Using global vars like that is rarely what you should be doing. In most cases it's better to declare it as a lexical var and either use an accessor sub that returns the var, or you can use Exporter to export the var.


PapaGeek
User

Mar 2, 2014, 3:12 PM

Post #3 of 3 (1555 views)
Re: [FishMonger] Accessing package variables [In reply to] Can't Post

LOL, this should have been posted in the beginner forum!

I made your change and changed my to our, didn’t work! Copied your code in a new project, it worked??

Dumb Dumb, if you look at the code for the package in my first post, the package line comes after the hash definition! I needed to use our as you suggested and also move the package definition to the top of the file!

Thanks for the help!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives