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

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: 'my' with 'if 0' retains old value
by hubb0r (Pilgrim) on Apr 21, 2006 at 03:52 UTC
    I know that there is a statement about this in one of the perl docs, although I cannot find it now.

    From perlsyn:

    NOTE: The behaviour of a "my" statement modified with a statement mod +ifier conditional or loop construct (e.g. "my $x if ...") is undefine +d. The value of the "my" variable may be "undef", any previously ass +igned value, or possibly anything else. Don't rely on it. Future ve +rsions of perl might do something different from the version of perl +you try it out on. Here be dragons.
    So the following:
    my $v2 = -2 if 0;

    Will have undefined and probably unexp(?:exted|licible) behavior. Don't do it! Don't use a my assignment with a conditional!

    Define your variable first, then set it using the conditional.

Re: 'my' with 'if 0' retains old value
by ikegami (Patriarch) on Apr 21, 2006 at 03:52 UTC

    Quote diotalevi:

    my() has a runtime effect. You *always* (unless you're doing something freakish) want that to happen. The my $var STATEMENT-MODIFIER allows the runtime part of my() to potentially not happen.

    In other words,
    my $var = ... if/unless/while/for/foreach ...
    is wrong and/or buggy. Use
    my $var;
    $var = ... if/unless/while/for/foreach ...
    instead

    In this case, replace
    my $v2 = -2 if 0;
    with
    my $v2;
    $v2 = -2 if 0;

    PS - You post here regularly. You should start heeding "Clean Your Room" in How (Not) To Ask A Question and start indenting your code.

      You can use Perl::Critic to automatically find such fugly code as well. Neat, eh?

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      my $v2; $v2 = -2 if 0;
      Surely that won't do anything useful. I mean, it'll do the same as
      my $v2;
      Hence: no static variable, no value assigned.

      Anyway:

      my $v2 if 0;
      will create a static variable. But IMO that's a coincidence of the implementation, I wouldn't count on it.
        What if 0 is actually DEBUG? I don't presume to know what the real code is like.
Re: 'my' with 'if 0' retains old value
by liverpole (Monsignor) on Oct 06, 2006 at 14:21 UTC
    From this site on September 30, 2005:
    # New Ticket Created by "herington, dean" # Please include the string: [perl #37315] # in the subject line of all future correspondence about this issue. # <URL: https://rt.perl.org/rt3/Ticket/Display.html?id=37315 > This is a bug report for perl from herington_dean[at]emc.com <mailto:herington_dean[at]emc.com> , generated with the help of perlbug 1.35 running under perl v5.8.4. ----------------------------------------------------------------- Running the following program outputs: Perl 5.008004 undef undef undef 2 but it should output: Perl 5.008004 undef undef undef undef #!/usr/bin/perl -w print "Perl $]\n"; sub show { my ($val) = [at]_; if (! defined $val) { print "undef\n"; } else { print "$val\n"; } } sub sb { my $v1; $v1 = -1 if 0; my $v2 = -2 if 0; show $v1; show $v2; $v1 = 1; $v2 = 2; } sb; sb;

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: 'my' with 'if 0' retains old value
by blazar (Canon) on Apr 21, 2006 at 12:39 UTC

    Yes, it is a well known behaviour not intended as a feature, that someone uses as if it were reliable semantic: although it is to be noted that even if it's not strictly a bug, such behaviour is not reliable and is left undefined by the specs (as of the docs). So it's better not to use it, period.

    Logically, it doesn't make much sense at all to want have a declaration depending on a modifier: would you really like to have a lexical variable exist or not depending on a condition in the surrounding lexical scope?!?