in reply to Re: First attempt at bringing in file for input/output
in thread First attempt at bringing in file for input/output

It is not allowed to optionally create a variable in an if statement.

I cannot see where this is attempted; can you point this out? (I assume you're referring to the OPed code, but I can't see it anywhere in the thread.) (Update: As an aside, note that conditional creation of a variable is "allowed" in the sense that it is not a syntactic error, but it's not a good idea for reasons discussed here and documented here.)

open my $FH_infile, '<', "$in_file" or die "Can't open $in_file";

And just to satisfy my idle curiosity, can you say why you use the  "$in_file" idiom (stringizing a string)? (I notice talexb also uses the idiom here.)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: First attempt at bringing in file for input/output
by haukex (Archbishop) on Oct 22, 2018 at 08:23 UTC
    As an aside, note that conditional creation of a variable is "allowed" in the sense that it is not a syntactic error

    Not really a syntax error, no, but:

    $ perl -wMstrict -Mdiagnostics -e 'my $x if 0' Deprecated use of my() in false conditional. This will be a fatal erro +r in Perl 5.30 at -e line 1 (#1) (D deprecated) You used a declaration similar to my $x if 0. Ther +e has been a long-standing bug in Perl that causes a lexical variabl +e not to be cleared at scope exit when its declaration includes a fa +lse conditional. Some people have exploited this bug to achieve a kin +d of static variable. Since we intend to fix this bug, we don't want p +eople relying on this behavior. You can achieve a similar static effect + by declaring the variable in a separate block outside the function, e +g sub f { my $x if 0; return $x++ } becomes { my $x; sub f { return $x++ } } Beginning with perl 5.10.0, you can also use state variables to ha +ve lexicals that are initialized only once (see feature): sub f { state $x; return $x++ } This use of my() in a false conditional has been deprecated since Perl 5.10, and it will become a fatal error in Perl 5.30.

    See also #133543.