http://qs321.pair.com?node_id=1230542


in reply to Re: Origin of 'md5sum' and 'sh' logs
in thread Origin of 'md5sum' and 'sh' logs

In general a "my" variable cannot be declared within a conditional statement.
Of course it can. Why should this be a problem?

You can declare a "my" variable in any lexical scope - the branches of an if-statement are just one example:

use strict; my $whatever; if($whatever) { my $something = "here"; } else { my $something = "there"; } print "look I compiled\n";

Replies are listed 'Best First'.
Re^3: Origin of 'md5sum' and 'sh' logs
by Marshall (Canon) on Feb 26, 2019 at 01:43 UTC
    can declare a "my" variable in any lexical scope. Well, ok but you can't use it outside of that scope. The OP's indenting is confusing to me. Your code works until you try to use $something outside of lexical scope.
    #!/usr/bin/perl use strict; use warnings; my $whatever; if($whatever) { my $something = "here"; } else { my $something = "there"; } print "look I compiled $something\n"; __END__ Global symbol "$something" requires explicit package name (did you for +get to declare "my $something"?) at Monks\Badcode.pl line 14. Execution of Monks\Badcode.pl aborted due to compilation errors. Process completed with exit code 255
      Of course you can't use it outside of it's scope.

      There is a reason it's called "my".

      I did not read the OP - I was just picking up on your statement.