For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > May 2004 > Problems declaring variables in external packages









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problems declaring variables in external packages
Richard Heintze

2004-05-22, 11:32 am

I'm using these statements in my main program:

use DisplayPCE qw($order $fnx );

....
print join("", map { qq[<td>$_</td>] }
@{$DisplayPCE::order});
....

When I use the debugger, I find that order is
undefined! When I use the browser to view the page,
the value of undef is confirmed.

When I abandon packages and put the definition of
order in the main program, it works!

Can anyone help me understand why my package is not
working?
Siegfried

Here is the contents DisplayPCE.pm:

package DisplayPCE;
our @EXPORT = qw($order );
my $order = ["First Name", "Last Name", "E-Mail",
"User Name", "Address", "City", "State", "Zip Code",
"Country", "Phone", "Org.", "Gender", "Ethnicity",
"Age", "Religious Affiliation" ];

1;





__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
http://photos.yahoo.com/ph/print_splash
Wiggins D'Anconia

2004-05-22, 11:32 am

Richard Heintze wrote:

> I'm using these statements in my main program:
>
> use DisplayPCE qw($order $fnx );
>
> ...
> print join("", map { qq[<td>$_</td>] }
> @{$DisplayPCE::order});
> ...
>
> When I use the debugger, I find that order is
> undefined! When I use the browser to view the page,
> the value of undef is confirmed.
>
> When I abandon packages and put the definition of
> order in the main program, it works!
>
> Can anyone help me understand why my package is not
> working?
> Siegfried
>
> Here is the contents DisplayPCE.pm:
>
> package DisplayPCE;
> our @EXPORT = qw($order );
> my $order = ["First Name", "Last Name", "E-Mail",
> "User Name", "Address", "City", "State", "Zip Code",
> "Country", "Phone", "Org.", "Gender", "Ethnicity",
> "Age", "Religious Affiliation" ];
>
> 1;
>


@EXPORT isn't a Perl standard variable, it is just a convention provided
by the standard Exporter module. So you will need to let Perl know that
your package is an exporter and then it will automatically export what
is in @EXPORT, you may want to consider using @EXPORT_OK instead.

package DisplayPCE;
require Exporter;
@ISA = qw( Exporter );

For more information:

perldoc Exporter
perldoc -f import
perldoc perlmod
perldoc -f use

http://danconia.org
Randy W. Sims

2004-05-22, 11:32 am

Wiggins d'Anconia wrote:
> Richard Heintze wrote:
>
>
> @EXPORT isn't a Perl standard variable, it is just a convention provided
> by the standard Exporter module. So you will need to let Perl know that
> your package is an exporter and then it will automatically export what
> is in @EXPORT, you may want to consider using @EXPORT_OK instead.
>
> package DisplayPCE;
> require Exporter;
> @ISA = qw( Exporter );


Using Exporter and putting vars in @EXPORT_OK is the *right way*, but...
note that using 'my' creates a lexical variable. No matter which way you
do it you need to use package variables in order for them to be seen
outside the package. You can use either:

use vars qw($order);

-or-

our $order;

to create package variables. Then you can access them from another
package by using the fully qualified name:

use Data::Dumper;
print Dumper($DisplayPCE::order);

If you want to access the variable without the fully qualified name,
you'll need to use Exporter as already described.

Randy.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com