need to declare/import package variable?

Discussion in 'Perl Misc' started by Helmut Richter, Sep 17, 2009.

  1. 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
    Helmut Richter, Sep 17, 2009
    #1
    1. Advertising

  2. 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
    Helmut Richter, Sep 17, 2009
    #2
    1. Advertising

  3. Helmut Richter

    Uri Guttman Guest

    >>>>> "HR" == Helmut Richter <> 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.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Sep 17, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parvinder
    Replies:
    6
    Views:
    744
    Thomas G. Marshall
    Feb 27, 2005
  2. George P
    Replies:
    3
    Views:
    677
    Alex Martelli
    Sep 11, 2004
  3. Replies:
    18
    Views:
    623
    Fredrik Lundh
    Nov 30, 2005
  4. Andrea Raimondi
    Replies:
    2
    Views:
    458
    randy.buchholz
    Jul 18, 2008
  5. Gabriel Rossetti
    Replies:
    1
    Views:
    493
    ryles
    Sep 20, 2009
Loading...

Share This Page