0

I am a beginner in perl programming and when i try this code with use strict and use warnings it throws an error saying "Global symbol $str1 requires explicit package name".Do we need to use any other packages to use unless keyword? ..please help

Kamlesh Sam
  • 13
  • 1
  • 3
  • It is from [`use strict;`](https://stackoverflow.com/questions/20889609/how-should-i-use-the-my-keyword-in-perl) You should declare the variable using [`my`](https://perldoc.perl.org/functions/my.html). `my $str1 = 0;`. [Why use strict and warnings?](https://stackoverflow.com/questions/8023959/why-use-strict-and-warnings/8024241) – mkHun Aug 11 '17 at 07:07
  • The `my $str1` is what is checked at compile time, however `$str1` in the same statement (with `defined`) is also checked, and had not been declared yet. You can't declare it (`my $s`) and have it be used as already declared (`defined $s`) in the same statement. Declare first. Having said that, I find this to be a duplicate. – zdim Aug 11 '17 at 08:33

1 Answers1

0

Try this code, you need to declare $str1 explicitly

use strict;
use warnings;
my $str1;
$str1 = 0 unless defined $str1;