Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about 'packages' 2

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
Please explain ... SPECIFICALLY ... why, when running the following code ... the print $A::variable; and print $B::variable; will not print anything.

What is exactly happening here. I am kind of confused with the way my $variable is being handled. If you can be as specific as possible.

Thanks ..................



my $variable = "Not in either package\n";

package A;
$variable = "In package A\n";

package B;
$variable = "In package B\n";

print $A::variable;
print $B::variable;
===

doesn't print anything. Why?


Gary

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
try putting 'my' calls in front of the declarations in each package (?). i'm not sure exactly why, but to be honest, most things i do involving questionable variable declarations OR uses usually don't turn out right the first time, not until i play with them a bit, sometimes even a little randomly, will it come out right. i've spend lots of time trying to learn all the exact nuances of this, but still seem to be a litle less than fully in command. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
stillflame,

I apologize. I am not trying to figure out how to FIX this. When I was reading this ... they said that:

print $A::variable;
print $B::variable;


wouldn't print anything.

In analyzing the code, I was simply trying to understand specifically WHY.

It has something to do with the my declaration in the line:

my $variable = "Not in either package\n";

And ... how by calling the variable in the print statements for $A and $B ... something gets changed around.

I am just trying to understand why?

Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
curiouser and curiouser...

this fragment, under ActiveState 5.6 on Win98

my $vari = "Not in either package\n";

package A;
$var = "In package A\n";

package B;
$var = "In package B\n";

print "Hello world\n";
print "A $A::var\n";
print "B $B::var\n";
print "plain $vari\n";

displays:

C:\Perl\bin>perl pack.pl
Hello world
A In package A

B In package B

plain Not in either package


The difference is that the variable in main is now named $varis and not the same name as in A and B..... Hmmmm

Here's a clue, from the Perl documentation:

A package statement affects only dynamic variables--including those you've used local on--but not lexical variables, which are created with my.

So...

$var = "Not in either package\n";

package A;
$var = "In package A\n";

package B;
$var = "In package B\n";

package main;

print "Hello world\n";
print "A $A::var\n";
print "B $B::var\n";
print "main $main::var\n";

displays:
Hello world
A In package A

B In package B

main Not in either package
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Mike,

Please, if you can, just explain why and what specifically is causing the output I am getting from the following code:

#!/usr/bin/perl -w

my $variable = "Not in either package\n";

package A;
$variable = "In package A\n";

package B;
$variable = "In package B\n";

print $A::variable;
print $B::variable;



OUTPUT IS:

Name "A::variable" used only once: possible typo at test.pl line 11.
Name "B::variable" used only once: possible typo at test.pl line 12.
Use of uninitialized value at test.pl line 11.
Use of uninitialized value at test.pl line 12.


What is happening with the code shown above that is causing $A::variable and $B::variable to either get lost or what? Very confused. When I take the 'my' away from the line that says my $variable ... and run the script, I get the following output.


Name "main::variable" used only once: possible typo at test.pl line 3.
In package A
In package B


Why is the missing 'my' now causing the problem?

THANKS!!
Gary


Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Gary,

This business of using multiple packages in the same file looks to be a can of worms. I suggest we put the lid back on it, I only ever use packages in their own files and don't have any trouble with them. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Mike,

I appreciate your suggestion.

But, all I am actually trying to do ... is understand the specific "WHY" regarding what seems to be happening in each of these situations.

Again, your suggestion is great .. but I just want to understand exactly "WHAT" is causing the output I'm getting from these situations.

:)
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
As the quote from the docs alluded, variables decared with 'my' are affected differently by subsequent reassigning in other packages.
i don't know the exact reasons why or how, but i do know this: in perl, you'll probly never going to be making multiple packages declarations in the same file. i've never seen it done in perl, in fact. things like this are for a much different kind of programming than you'll likely be using perl for, at least for the first year or ten you're using it.
Mike's advice was right, don't worry about this little detail. it's not going to affect your ability to program. if you really can't stand not understanding this completely, experiment with it alot. do every little variation on it you can conceive of until you find something out. then you'll have probly learned even more than an answer here could have taught you. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Ok.

Thanks.

It was just something from a tutorial book I was reading .. and I thought it would benefit me to know WHY their example gave me the result it did. They told me what to expect, but didn't explain WHY it was happening .. to any degree.

But, I'll forget this for now and I APPRECIATE ALL YOUR HELP!! Everyone.

Thanks to everyone,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top