Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Some Insights from a Traveler Between Languages

by tlm (Prior)
on Apr 23, 2005 at 19:55 UTC ( [id://450789]=note: print w/replies, xml ) Need Help??


in reply to Some Insights from a Traveler Between Languages

Recently there was an exchange on some of the points you raise.

Even though tye and merlyn make very good points to the contrary in the thread I just cited, I still think that Perl should 1) provide a list operator as counterpart to scalar, 2) revoke the rule that "a list cannot exist in a scalar context" (after all, it is possible for a scalar to exist in a list context); and 3) define the value of a list in a scalar context as the length of the list. The latter would not prevent a function from responding idiosyncratically in scalar context; e.g.:

my $date = localtime; my $n = list localtime; print "$date\n"; print "$n\n"; __END__ Sat Apr 23 14:52:09 2005 9
tye pointed out in the thread cited above that most uses of this list function would be to get the number of items a function would return in list context, and therefore that it would be better to call it count instead. I agree that most uses of list would be for counting, but I still think that it should be called list. My reason for this is largely formal. Perl imposes no restrictions on how a programmer (or even perl itself) may exploit context information. For example, Perl does not proscribe functions that have side effects only when called in a list context. Since Perl allows programmers complete freedom in how they exploit context information, it is only fair that it also provide ways for programmers, who routinely use code written by others, to control the context in which functions evaluate without having to resort to tricks like =()=.

(Before I get thoroughly roasted for all this heresy I'm quick to add that the I am aware that any formal merits of the scheme above may not be sufficient to warrant the refactoring of perl that would be required to implement it.)

As a bonus, I think it would be easier to teach context rules, and Perl gurus like tye and merlyn would not have to spend so much time getting blockheads like me to understand that "lists can't exist in scalar context."

the lowliest monk

Replies are listed 'Best First'.
Re^2: Some Insights from a Traveler Between Languages
by TimToady (Parson) on Apr 23, 2005 at 20:02 UTC
    Fixed in 6. :-)
Re^2: Some Insights from a Traveler Between Languages
by Joost (Canon) on Apr 23, 2005 at 23:20 UTC
    I have two reasonably short potential equivalents to your list() function. Which of these do you want? Remember that it will should work the same for all arguments, so the fact that we're using localtime here is irrelevant.
    my ($n) = localtime;
    or
    my $n = (localtime)[-1];
    Context is given by the left hand side of an assignment, but its interpretation is done by the right hand side.

    update: also note that the current interpretation of scalar context by a list means that the last element is returned, wich would mean that your list() function would act like:

    (1,2,3,4) != list(1,2,3,4);

      Neither. As I showed in my original post, after

      my $n = list localtime;
      $n should end up with 9.

      the lowliest monk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found