You are absolutely correct, that the my is included inside the { } block.  But that is 
exactly where the confusion sets in, having had this problem when I started coding in 
perl.  The { } are required when using an if/else construct -- part of the syntax to 
perform this function.  Whereas a block is *optional*, like in C/C++.  
 
So yes, technically, any code inside a  { } will be a block and the 'my' looses scope. 
 But I totally understand the original poster's confusion!
 
 
-JW
 
 

James Edward Gray II <[EMAIL PROTECTED]> wrote:
On Apr 27, 2004, at 5:39 AM, mike wrote:

> I am getting a bit confused here, if I uncomment out the print 
> statement
> within the if block $date1 prints, however the $date1 after the if 
> block
> doesn't
>
> if ($date eq ''){
> my $date1=localtime;
> #print $date1;
> }
> else {
> my $date1=~$date;
> };
> print $date1;
>
> any ideas

my(...) scopes a variable locally, which usually means it ceases to 
exist at the next }, or end of the block. If you need the same 
variable through and outside a block, you have to declare it outside:

my $date1;
if ($date eq '') { $date1 = localtime; }
else { $date1 = $date; } # not what you had, but I bet it's what you 
meant
print "$date1\n";

While we're talking, you should look into adding the following two 
lines to the top of all your code:

use strict;
use warnings;

This would have kept the code you posted from compiling and it would 
have told you why. Build good habits.

Good luck.

James


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



                
---------------------------------
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs 

Reply via email to