need to declare/import package variable?

H

Helmut Richter

It was my understanding that importing variables from packages is only
needed for using them by their unqualified names, but this seems to be
wrong. Here is the source:

There is a module CGI_Fiona.pm starting with the following code:

---- start code example ----

package CGI_Fiona;
use strict;
use vars qw(@ISA @EXPORT);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(script_begin script_end);

my ($postlude, $included_file);

sub script_begin {
...
$included_file = "/some/filename";
...
}

---- end code example ----

The package is used by a script include_file.pl starting with the follong
code:

---- start code example ----

#! /usr/bin/perl

use strict;
use CGI::Carp qw(fatalsToBrowser);
use Encode;
use lib '/info/www/PERLLIB/';
use CGI_Fiona;

my ($text, $textlength, $outtext);

script_begin();

open (INCLUDED, "<$CGI_Fiona::included_file") || die "unable to open
\"$CGI_Fiona::included_file\": $!\n";

---- end code example ----

Everything compiles fine, but on execution the variable
$CGI_Fiona::included_file has no value inside include_file.pl although
the variable $included_file (which I assumed to be the same variable) has
been set in script_begin() inside CGI_Fiona.pm .

I could try to repair the situation by also exporting the variable with
the same means as the subs, but first I want to understand why this is
necessary. A fully qualified variable should be visible everywhere,
shouldn't it?
 
H

Helmut Richter

Lexical variables are never visible across file boundaries.

You need to make $included_file a package variable for it to work
like you expect, but that is bad design.

Yes, it is bad design. When writing the script, I noticed that it would
*nearly* do what was already in the package's subroutines. It is certainly
better to add the functionality to the package as a new subroutine so that
the script can call this functionality without knowing the package's
internals.
A fully qualified package variable should be visible everywhere.

Lexical variables are never visible across file boundaries.

There is one thing I have still not understood: Why is the variable with
the name $CGI_Fiona::included_file accepted at all? With "use strict" it
should be rejected because it is nowhere declared.

In any case, thank you very much for your assistance.
 
U

Uri Guttman

HR> There is one thing I have still not understood: Why is the variable with
HR> the name $CGI_Fiona::included_file accepted at all? With "use strict" it
HR> should be rejected because it is nowhere declared.

because that is called a fully qualified package variable. just as tad
said. the :: tells perl you know where this variable is from and because
of that it works with strict. strict only checks simple variable names
and makes sure they are declared (which is always in the current scope
for lexicals). you can declare globals with our or use vars to work with
strict as well.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top