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

With perl 5.10.0, and, it would seem, with several other versions as well, I have just seen a case where the following construction did not cause a syntax error warning, even though use strict; use warnings; was being used:   ($x and $y properly declared, no other use declarations, etc., blah blah blah, meh...)

if ($x eq my $y) ...

my, in this context, is of course an erroneous “bareword.” a situation that is syntactically legal but which should cause a warning (since I have expressly asked for them).   But, why was the apparetn tpyo ( ;-} “apparent typo!”) not detected by Perl?

Also note that an equivalent perl -e 'blah...' one-liner did not reproduce the problem.   (It flagged the bareword produced the warning (see reply in this thread).)   The code file in question is many thousands of lines long, so I can’t practically include either it nor a segment.

So, kindly take for granted, that I know whereof I speak on what I am seeing here, and please help me with my initial response of, “WTF?!?!”   I know that the my keyword can be used “inline” in many places, e.g. for[each](), but I am unaware of any legitimate place for it in an if() statement.

This humble Monk now penitently beseeches thee:   Enlightenment?

Replies are listed 'Best First'.
Re: My, what is "my" doing here?
by moritz (Cardinal) on Jan 27, 2012 at 13:05 UTC
    my, in this context, is of course an erroneous “bareword.”

    Not at all. In Perl, declarations can simply used as expressions, they return the variables they declare.

    If my was a bareword here, it would violate the "no two terms in a row" rule:

    $ perl -ce '"my" $y' Scalar found where operator expected at -e line 1, near ""my" $y" (Missing operator before $y?)

    It is perfectly legal to write things like

    say my $x = 5;

    Which declares variable $x, returns its value, and then prints it, followed by a newline.

    I don't see the reason for using such a declaration in the if-statement without any initialization (it will always be undef), but it is perfectly legal.

Re: My, what is "my" doing here?
by Eliya (Vicar) on Jan 27, 2012 at 13:07 UTC

    Not sure what bareword error you're getting or expecting.  I do get a "Use of uninitialized value" warning (as expected), both as a one-liner and as a regular script, and with various versions of Perl (5.10.0 and later).

    $ perl -Mstrict -we 'my ($x,$y)=(0,0); if ($x eq my $y) {}' Use of uninitialized value $y in string eq at -e line 1.

      The thing that is really flummoxing me here, Eliya, is that so do I, in a one-liner, but that’s not what I am seeing in the larger script.

      perl -e 'use strict; use warnings; my $x=0; my $y=0; if ($x eq my $y) { print "meh"; }'
      Use of uninitialized value $y in string eq at -e line 1.


      (No message is produced, in this one-liner, if use warnings; is omitted, and of course, this is the behavior that I expect to see all the time.)

      But the large Perl file compiles with no such warnings at all!   And that is what is throwing me here.

      I can accept the notion of the use of my being legitimate here, and I expect the warning-message to appear in every case when use warnings; is used.   This is what I expected here.   But, in this particular instant case, “no warning!”   The end result is that a subtle bug went into a production situation, and the only way that it was found was by line-by-line eyeball inspection in which three out of four people looking at it (“Paris in the the spring” ...) saw only what they expected to see.

        But the large Perl file compiles with no such warnings at all!
        Of course. And so does the one liner. The warning is a run-time warning.

        Can you show a trimmed down version of the larger program, that, when run, does not produce the warning when you expect it to do?

        But the large Perl file compiles with no such warnings at all
        The warning is not coming at compile-time. It is a run-time one.
Re: My, what is "my" doing here?
by ikegami (Patriarch) on Jan 27, 2012 at 21:51 UTC

    my creates* a variable and returns it at an lvalue, so $x eq my $y compares $x with newly created and undefined $y. That DOES cause a warning, contrary to what you claim.

    I'm surprised you asked this because you use the return value of my every time you write Perl code.

    • my $x = 4;
    • open(my $fh, ...);
    • etc

    * — Exactly when the variable gets created is a little funky, but that's not relevant here.

Re: My, what is "my" doing here?
by locked_user sundialsvc4 (Abbot) on Jan 27, 2012 at 13:16 UTC

    (wink!)   apparetn tpyo is, of course, a typo for “apparent typo!”   :-}