'use strict' does not require that variables be preDEFINED, but that they be 
preDECLARED.  The my statement accomplishes this.

In fact, it doesn't matter where you DEFINE your variables.  You could just as easily 
say:

my ($user,$test);        # or, alternatively
use vars qw/$user $test/;

and then define them later.

-----Original Message-----
From: perl newbie [mailto:[EMAIL PROTECTED]]
Sent: Friday, July 20, 2001 3:25 PM
To: [EMAIL PROTECTED]
Subject: Re: Variable scope and definition


I am trying to understand the correct way to define
variables and their scope.

Here are two simple PERL  ( test1.pl and test2.pl
)scripts. test1.pl work fine, while test2.pl does not.

Would appreciate any help in understanding why
test2.pl is complaining about "Global symbols"

Thanks

PN


test1.pl
========
#!/usr/bin/perl -w
use strict;

my $user = $ENV{USER};
my $test = "Perl";

print " User : $user\n";
print " Test  : $test\n";

test2.pl
========
#!/usr/bin/perl -w
use strict;

$user = $ENV{USER};
$test = "Perl";

print " User : $user\n";
print " Test  : $test\n";

The only difference in code is this:

In test1.pl, the variables are defined using "my",
while in test2.pl, the variables are defined without
use of the keyword "my".

Here are the results of my attempts at running the
scripts :

% perl -c test1.pl
  test.pl syntax OK

% perl -c test2.plperl -c test2.pl
Global symbol "user" requires explicit package name at
test2.pl line 5.
Global symbol "test" requires explicit package name at
test2.pl line 6.
Variable "$user" is not imported at test2.pl line 8.
Global symbol "user" requires explicit package name at
test2.pl line 8.
Variable "$test" is not imported at test2.pl line 9.
Global symbol "test" requires explicit package name at
test2.pl line 9.
test2.pl had compilation errors.

  




__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

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

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

Reply via email to