in reply to Re: open(), go away with bad way, or fail with the right way?
in thread open(), go away with bad way, or fail with the right way?

Perl makes "my" variables available in the statement after the "my". This allows things like

my $foo = 'Bar'; my $foo = $foo x 2; say $foo; # 'BarBar'

No, I do not defend this as a coding practice -- I'm just trying to elucidate how Perl works.

Replies are listed 'Best First'.
Re^3: open(), go away with bad way, or fail with the right way?
by karlgoethebier (Abbot) on Jun 01, 2019 at 17:40 UTC
    "...This allows things like..."

    Yes and no:

    #!/usr/bin/env perl use strict; use warnings; use feature qw(say);; my $foo = 'Bar': my $foo = $foo x 2; say $foo; __END__

    Perl allows it generously but then it complains about  "my" variable $foo masks earlier declaration in same scope at ./foo.pl line 8..

    Iznogoud. But in a block it works flawless - no surprise:

    KARL : { my $foo = $foo x 2; say $foo; }

    But why the effort? You could say $foo x= 2; which results in BarBar. If this is what you wanted. What i assume.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      "But why the effort?"

      It's only a demo. If you read the thread, it would be obvious.

        "...only a demo..."

        Even (or especially) a demo should compile without warnings. And masked variables may lead to pain in the ass (old saying).

        "...read the thread..."

        I have read the thread. Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^3: open(), go away with bad way, or fail with the right way?
by Anonymous Monk on May 31, 2019 at 21:38 UTC
    It's pretty useful in nested scopes and closures.

    For instance

    my $level = $level + 1;