I don't see why this should be an error. Just as

my $x = 5 if $t;

could be semantically equivalent to

my $x; $x = 5 if $t;

I think it all boils down to a matter of personal interpretation and taste; your example doesn't convince me at all: yes, it could, but I see no reason why it should. More precisely, it's true that the single

my $x = 5;

statement is semantically equivalent to the two

my $x; $x = 5;

statements. But is it true in general than when STATEMENT is equivalent to STATEMENT1; STATEMENT2; ... STATEMENTn; then also

STATEMENT if condition;

is equivalent to

STATEMENT1; STATEMENT2; ... STATEMENTn if condition; # ?!?

IMHO not only is the answer no but FWIW I see no aesthetical appeal of such an "algebraic" property.

OTOH since

STATEMENT if condition;

is generally equivalent to

if (condition) { STATEMENT }

and

if ($t) { my $x = 5 }

is perfectly valid, albeit fundamentally useless, perhaps the "incriminated" construct should not trigger an error, but just be a happily useless thingy. Granted, Perl 5 already has so many ad hoc deviations from orthogonality for the sake of beauty, pragmatics and magic that one may want to throw in yet another one: yet I still fail to see how that particular construct could be of any good with any of those.


In reply to Re^5: Curious result of using "my ... if ..." by blazar
in thread Curious result of using "my ... if ..." by jrw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.