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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
How do you use 'my' with package variables?



Aug 11, 2000, 8:22 AM

Post #1 of 5 (574 views)
How do you use 'my' with package variables? Can't Post

I have 'use strict' on in my scripts. I have declared all my variables with 'my'. But the compiler says I also need to declare a package variable that I'm using. The variable is currently declared as:
use g_var qw($database );

How do I use 'my' with '$database'?

User / Moderator

Aug 11, 2000, 1:32 PM

Post #2 of 5 (574 views)
Re: How do you use 'my' with package variables? [In reply to] Can't Post

use vars qw ($database);

If you have 'my', 'local', or 'our' variables, they are trapped within the block where they are declared. But if you 'use vars' they are put into the package namespace.

So if the declaration above is in the main package (you have not used 'package' to change it), then you can use these names for the variable...

$database #but only in this package

This becomes important when you make modules. The other types of variables are only good inside the file, but if you 'use vars' you can export them to the places that use your module.


Aug 12, 2000, 8:30 AM

Post #3 of 5 (574 views)
Re: How do you use 'my' with package variables? [In reply to] Can't Post

Can you show me with this example? This is a package named
package g_var;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw($database);
$database = "test";

This is a script, test.cgi, that uses the package variable $database.
use strict;
use g_var qw($database);

How do I declare $database in test.cgi with 'use strict'?

I also haven't figured out how to incorporate 'use strict' in the package itself.

[This message has been edited by timaratz (edited 08-12-2000).]


Aug 12, 2000, 9:09 PM

Post #4 of 5 (574 views)
Re: How do you use 'my' with package variables? [In reply to] Can't Post

I'm leaving Perl 5.6 out of this, because the our() declaration can be confusing (even to me).

Modules can only export variables that are in their namespace. These are variables that are NOT my()d variables. What my() does is create a variable that does NOT exist in any namespace, it only exists in the block of code in which it was created:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my $X;
package A;
$X = 1;
$Y = 2;
package B;
$X = 3;
$Y = 4;
package A;
print "$X, $Y\n"; # 3, 2
package B;
print "$X, $Y\n"; # 3, 4

As you can see, $Y is different in the two packages. Given a package Foo, if $bar is not a my() variable, then saying $bar is like saying $Foo::bar. If you say $::bar, then that is like saying $main::bar, which is $bar in the default package, called main.

If a variable has been my()d, then it is visible until the block it is in is closed:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my $foo = 10;
sub func { print $foo }
# func() is defined in the same scope as
# this $foo, so it can see it

func(); # prints 10
my $foo = 20;
func(); # still prints 10

In the inner block, where $foo is 20, the function func() is called. But the INNER $foo is not visible OUTSIDE its block of code, and therefore, its value of 20 isn't used.

As for 'use strict' and 'use vars', strict requires variables to be declared with my(), given full package names, or be validated using 'use vars'. 'use vars' just makes a variable safe to be a package variable.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

package Foo;
use strict;
use vars qw( @INC @EXPORT_OK $this );
# all have to be package variables -- they
# can't be my() variables
# and you can only export package variables,
# not my() variables
require Exporter;
@INC = qw( Exporter );
@EXPORT_OK = qw( $this );
$this = "whatever";
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use Foo qw( $this );
use strict;
use vars qw( $this );
print $this; # whatever

Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


Aug 13, 2000, 6:56 AM

Post #5 of 5 (574 views)
Re: How do you use 'my' with package variables? [In reply to] Can't Post

Thank you for the very informative and helpful response. Now I get it.


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

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