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

Is there any time when an explicit single element list (as opposed to one where the slice is determined dynamically) ever makes sense? I can't think of any. There are many occasions where my ($foo) makes sense, however. Assuming that warnings are meant to aid the user and do not have to be orthogonal, this warning makes sense IMHO.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: @array[1] is valid??
by Abigail-II (Bishop) on Aug 05, 2003 at 15:09 UTC
    #!/usr/bin/perl use strict; no warnings; my (@firstkey, @numberofkeys); my @list = qw /foo housekey blah carkey baz monkey/; @firstkey [0] = grep {/key$/} @list; $numberofkeys [0] = grep {/key$/} @list; print "@firstkey\n"; print "@numberofkeys\n"; __END__ housekey 3

    And yes, there are a million different ways to write this code, but that's not the point.

    But I can't think of a case where using @array [0] in non-lvalue context could return something else than $array [0].

    Abigail

      That seems contrived. I can't imagine a single case where I'd do it this way, rather than
      my ($firstkey) = grep {/key$/} @list; my $numberofkeys = grep {/key$/} @list;

      I could see that usage if you were doing @firstkey[$of_list_nr_x], but then that doesn't emit a warning.

      Unless your point is that the warning breaks orthogonality, which is not an argument I care about here.

      Makeshifts last the longest.

        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