By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,372 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,372 IT Pros & Developers. It's quick & easy.

scalar variable declaration

P: 89
Hi

I have a set of salaras as $a, $b etc..
Since I am using strict and warnings, how do I declare them. I am not initialising to any value. Is there any way to declare them instead of
my $a;
my $b;
...etc

Thanks.
Jul 23 '08 #1
Share this Question
Share on Google+
2 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
The 'warnings' pragma is very good to use. It allows you to catch a number of syntactical errors before posting your code here in the forums.

On the other hand, the strict pragma is also good as it forces you to create safe constructs. The only way to declare variables with strict on is by preceding them with "my", "out", or "uise", with the most common one being my as you have already done. Once you do that once though, you don't have to do it again.

Regards,

Jeff
Jul 23 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Hi

I have a set of salaras as $a, $b etc..
Since I am using strict and warnings, how do I declare them. I am not initialising to any value. Is there any way to declare them instead of
my $a;
my $b;
...etc

Thanks.
If you have a list of variables to declare you can do this:

my ($var, @var, %var, $var2);

That packages them and you can then use them as needed. But don't get into the habit of just declaring all your variables at the beginning of a script, only declare them when and where needed, otherwise they are just global variables and that defeats the purpose of using lexical variables.

I think if you had read the strict pragmas documentation the list declaration is discussed. Try and get used to reading the documentation, it might be confusing sometimes but the more you read it the clearer it becomes.
Jul 23 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.