Personally I love this kind of stuff. Its a nice combination of the elegance of the functional (LISP, ML) paradigm and your more standard brute-force imperative/side-effect paradigm. You assign/pattern-match the array returned from each (functional), then the while statement uses the side effect value produced when it reduces the values in its conditional clause to a scalar value (imperative/side-effect). This is as close as Perl comes to a pattern matching syntax for functions (pattern matching != reg-ex in this context). Which, IMHO is one of the coolest peices of syntactic suger out there.

Consider this ML code for finding the length of a list:

fun length [] = 0 | length (head::tail) = 1 + length tail;
The first version of the length function is for empty lists (hence the []). The second version (after the | character) matches the head (the first element) of the list and the tail (the rest of the list). (The "::" is actually ML's list operator, and far to esoteric to explain here). The equivalent Perl would be:
sub length { return 0 unless @_; # manual pattern matching of the array here my ($head, @tail) = @_; return 1 + length(@tail); }

Or this (oh so cool) Erlang code for testing list membership:

member (X, []) -> false; member (X, [X|_]) -> true; member (X, [_|T]) -> member(X, T).
Erlang uses the "_" (underscore) to mean "A variable whose value i dont care about", and all variables can only be assigned to once and must begin with capital letters (its a language for real-time systems, so it is strict like this for good reason). This function can be read like this:

Erlang evaluates this function by moving down the argument-lists/guard-clauses, and runing the body of the function that best matches the actual given arguments. If you think this looks kinda like Prolog, it does, the original Erlang interpreter was written in Prolog.

It is not hard to do an equivalent of this in perl, it would roughly be this:

sub member { my ($X, $head, @tail) = @_; return 0 unless defined($head) && scalar @tail; return 1 if $X == $head; return member($X, @tail) if $X != $head; }
Sure, all this Perl code is recursive, and therefor not terribly efficient. But the Erlang and ML code is compiled in such a way that they are efficient. ML can reach C-like speeds at times, and Erlang can get up there too.

IMHO Perl is one of the coolest langauges out there; its very multi-paradigm, super-flexible in syntax and you can you can open it up and play with its guts (symbol tables, the B modules, etc etc) without "voiding the warranty" :). A very nice combination.

-stvn

NOTE: The ML and Erlang code are not mine, stole them from 2 very cool books. Concurrent Programming in Erlang and ML for the Working Programmer.


In reply to Re: list assignment in scalar context by stvn
in thread list assignment in scalar context by ysth

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.