in reply to Re^4: @array[1] is valid??
in thread @array[1] is valid??

First saying that you can't think of any case where using a single element list ever (emphasis yours) makes sense, and then dismissing the first counter example as "contrived" doesn't make for a useful discussion.

You couldn't image any way where using a single element list would be useful, and given an example, you can't image you ever using it. Does that carry any weight? Or does that just mean your imagination is limited?

The fact that you would use different code isn't something I care about. More ways of doing something in Perl is a feature of Perl. I already pointed out there are different ways of doing the same. (Not that your code does the same, it stores the results in a scalar, while my code stores it in an array). But that doesn't mean other ways should be warned against - we do like people to use warnings, but too many warnings just means people will turn them off.

Now, to repeat my unanswered question, do you have an example (and it better be a good one, or someone will dismiss it as 'contrived'), where using @array[0] in a non-lvalue context could cause a result different from using $array[0]?

Abigail

Replies are listed 'Best First'.
Re^6: @array[1] is valid??
by Aristotle (Chancellor) on Aug 05, 2003 at 16:47 UTC

    I did not answer it because I never disputed whether a single element slice rvalue does the right thing in the first place.

    Note (again) that I'm specifically talking about the case with a constant index. Maybe my imagination is indeed limited. I'm honestly tempted to grep all of CPAN to find out whether anyone else has ever found it useful.

    But let's accept your argument that it's in the spirit of TMTOWTDI to use it that way as an lvalue and therefor shouldn't be warned against when other similar ways aren't. Let's also accept your agument that it doesn't behave any differently than $array[42] as an rvalue.

    I offer that it is a fact that a (even slightly) seasoned Perl programmer will instinctively use the $array[42] notation.

    Then is there any argument you can make against warning about it when used as an rvalue (but not lvalue)?

    (PS: If you want to talk about too many warnings, you're invited to talk about "used only once"..)

    Makeshifts last the longest.

      Let's also accept your agument that it doesn't behave any differently than $array[42] as an rvalue.

      I offer that it is a fact that a (even slightly) seasoned Perl programmer will instinctively use the $array[42] notation.

      Then is there any argument you can make against warning about it when used as an rvalue (but not lvalue)?

      The fact that most Perl programmers will prefer $array[42] over @array[42] is partially habit, but it's also influenced by this warning being discussed. Seasoned Perl programmers will use warning, if only during development, and if a programmer has two options of doing something, and one option issues a warning, which option do you think will be mostly used?

      Let's not forget that Larry himself decided that $array[42] isn't right, and that it's now going to be @array[42] in perl6. Should then perl5 warn? I don't think so.

      Furthermore, if you accept that (at least in rvalue context), both @array[42] and $array[42] are the same, why should there be a warning for one? If you cannot make a mistake, why suggest to the programmer he could have made one?

      Abigail