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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

followup for prex1 on Global/Local variable

Status
Not open for further replies.

ejaggers

Programmer
Feb 26, 2005
148
US
Your solution...

while ( 1 ) {
print 'main>';
chomp(my $main = <STDIN>);
my $yy = XYZ($main);
print "main $yy : $Last_Value\n";
}
exit(0);

{my $Last_Value = 'initial';
sub XYZ {
my $xyz = shift;
print "sub $xyz : $Last_Value\n";
$xyz = $Last_Value unless $xyz;
$Last_Value = $xyz;
return($xyz);
}
}

... is perfect. $Last_Value can't be printed in main. Now
my question is:

At first glance I thought the print in sub XYZ, would print 'initial' the first time thru, but it printed nothing for $Last_Value. Can you explain why?
 
You're trying to do something that probably doesn't need to be done. What is the reason for hiding $Last_Value from the rest of the program?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Correct: initialization of variables occurs at run time and that block is possibly never executed. You made me remind that, on the rare occasions when I used that construct, I added an initializing piece of code, something like
Code:
{my $Last_Value;
  sub XYZ {
     $Last_Value='initial'unless defined$Last_Value;
     ....
However perlsub manpage offers at least three much more elegant solutions:
-place that block before the main code or in a require'd file, so it gets executed first
-make that block a BEGIN block
-in perl 5.10 use the new keyword [tt]state[/tt] in place of [tt]my[/tt] to declare the variable (inside the sub) as a persistent private variable.
And, Kevin, that, a persistent private variable (a C's static), is exactly the goal of those constructs.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Yes, I am aware of what it does, but I am not sure its necessary.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, Isn't it good practice to hide variable from routines that don't need them? I don't understand why you say,"You're trying to do something that probably doesn't need to be done".

prex1, I tried using the keyword state, but must not be using it right. The following does not work:

sub XYZ {
state $Last_Value;
my $xyz = shift;
state $Last_Value;
print "sub $xyz : $Last_Value\n";

$xyz = $Last_Value unless $xyz;
$Last_Value = $xyz;

return($xyz);
}
 
I was just developing something for work and ran into a related problem.

I had code like this:

Code:
print "Getting extension list...\n";
{
   my $dbh = Database::mysql_connect();
   my $sth = $dbh->prepare ("...");
   $sth->execute(...);

   while (my $row = $sth->fetchrow_hashref) {
     ..
   }
}

With the "my $dbh" line inside of the empty block there, strict gave a warning about using $dbh without an explicit package name etc... so I moved it up above the opening brace there and then it was fine.

But it doesn't care about "my $sth" or "my $row" or any of that.

Kinda odd. :)

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top