Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > need to declare/import package variable?

Reply
Thread Tools

need to declare/import package variable?

 
 
Helmut Richter
Guest
Posts: n/a
 
      09-17-2009
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?

--
Helmut Richter
 
Reply With Quote
 
 
 
 
Helmut Richter
Guest
Posts: n/a
 
      09-17-2009
On Thu, 17 Sep 2009, Tad J McClellan wrote:

> 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.

--
Helmut Richter
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      09-17-2009
>>>>> "HR" == Helmut Richter <(E-Mail Removed)> writes:

HR> On Thu, 17 Sep 2009, Tad J McClellan wrote:
>> Lexical variables are never visible across file boundaries.
>>
>> A fully qualified package variable should be visible everywhere.
>>
>> Lexical variables are never visible across file boundaries.


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

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Package HTML - Missing package documentation file jochenhebbrecht@gmail.com Java 1 11-27-2007 01:59 PM
Package that imports with name of dependent package David Pratt Python 4 05-13-2006 05:12 PM
single package import v/s the entire package Parvinder Java 6 02-27-2005 02:02 PM
package module import name clash with global package George P Python 3 09-11-2004 01:19 PM
Importing a package and looping through modules in the package Dave Python 2 02-10-2004 08:14 PM



Advertisments