Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: @array[1] is valid??

by Abigail-II (Bishop)
on Aug 05, 2003 at 15:09 UTC ( [id://281012]=note: print w/replies, xml ) Need Help??


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

#!/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

Replies are listed 'Best First'.
Re^4: @array[1] is valid??
by Aristotle (Chancellor) on Aug 05, 2003 at 15:51 UTC
    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

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://281012]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found