plural and singular syntax in Perl5, PHP and Perl6

Discussion in 'Perl Misc' started by Andrew, Dec 31, 2012.

  1. Andrew

    Andrew Guest

    Hi, fellow perlers

    Before posting this I spent some time googling the web at large as well as searching this group's archive for posts related to the idea I wish to express, and, on cursory search, have not found any post, to date, which expresses the following -- assertively and emphatically. Yet, I feel (I compute), it DOES need to be said, emphatically:

    I absolutely, unequivocally LOVE, support and herald (as in, "Hats off to Larry Wall's genius") the syntactical distinction Perl5 (and older) makes between SINGULARITY and MULTIPLICITY. Syntactically, per se. In other words, EXPLICITLY, by means of two distinct symbols ("$" and "@"). The additional syntax that deals with the INTERPLAY between singular and multiple is equally brilliant:



    (the pre-pended symbols instantly signal (to the human reader) whether we are dealing with a multiplicity version of 'array' or just one of its elements (which is a scalar))

    The brilliant play with "@" (plural) and "$" (singular) continues (in termsof language design), with:


    and then, the whopper:

    @hash{@list_of_keys} or @hash{qw(yada dooba boom}}

    and then the enormously convenient (and code-economical) flexibility of nesting all of the above in a gazillion ways:


    @hash_one{keys %hash_two}

    etc, etc, etc!


    So, the converse of my exuberance over this syntactical excellence of Perl5(as I assess it to be) is my complete bewilderment and puzzlement (puttingit mildly) over PHP's LACK of such distinction (syntactically) between singularity and multiplicity.

    in PHP,




    I believe (correct me if I'm wrong) that in PHP I am left guessing, by context, whether I'm looking at a scalar or an array.

    So, I can only bash my head against the wall (note: LOWER-case "wall"), over the notion that not only does PHP's shortcoming (the lack of such succinct syntactical acrobatics as I've tried to suggest above, with the nested arrays and hashes in Perl) has gone COMPLETELY unnoticed (ignored/forgiven/even embraced as solid standard!?), I have learned (again, please correct me if I am wrong) that Perl itself (!!!) -- in its new and radically revised VERSION 6, is tossing out this baby with the bath water -- this gem of a syntax, obliterating the "$" versus "@" distinction, and (as I would put it) is descending to the painfully banal and ambiguous "$" for both scalars and arrays (?!) (that we observe in PHP)

    I would also like to add that "singular versus plural" (one versus many) isunequivocally a FUNDAMENTAL dichotomy, NOT JUST in computer programming, but in human (natural) language and human psychology and logical processing/thinking. (Perhaps on a par with "zero versus one", or "existing versus non-existing", etc., but just as fundamental)

    The syntactical differentiation has given us clarity and absence of ambiguity.

    So there, I've said it. I hope it did NOT come across as a rant, but more as a rallying cry... at least a rallying to acknowledge the value of the "$" and "@" syntax and the fact that losing it would mean losing a whole lot of clever, succinct, and quintessentially Perl coding freedom and flexibility.
    Andrew, Dec 31, 2012
    1. Advertisements

  2. * Andrew wrote in comp.lang.perl.misc:

    Note that the sigil for fetching a single element has changed from
    $ to @; perhaps a better way to think of it is that the sigil of a
    variable is now a part of its name, so it never changes in
    subscripting. This also applies to hashes.

    So the situation is not quite as you put it.
    Bjoern Hoehrmann, Jan 1, 2013
    1. Advertisements

  3. Eh ... sorry, but it is exactly as he puts it: The original idea that
    $ is roughly the same as a 'singular article' while @ is a plural one,
    has been replaced by the more 'conventional' ('conventional' here
    supposed to mean 'more like what other language designers also did'
    and absolutely not 'conventional' as 'what people usually do' since
    'people' usually don't) concept that namespaces of differently-typed
    variables are separate (I don't really have an opinion in favor of one
    versus the other approach[*]).

    [*] OTOH, Perl6 is about throwing away the baby (who would want an
    ugly kid like that, anyway) while trying to keep the bathwater (can't
    OUR [ugly] kid become just as popular with the help of something like
    'also deemed to be useful by superficial association'?).
    Rainer Weikusat, Jan 2, 2013
  4. Andrew

    Andrew Guest

    That is precisely the syntactic/semantic "algorithm" (if you will) of "%" and "@" !

    The sigil "%" is used to refer to associative array container as a whole, but a "$" prepended to the same variable name instead, with curly braces assubscript, is one, singular reference (to a singular or plural value)

    The sigil "@" (without its subscript, square bracket [....]) is, similarly,used to refer the entire array, but with the "$" instead of "@" one gets areference to a singular (scalar) value of that array.

    Thus, both the associative array and the list, in Perl5, are each a syntactical/semantic SET. A set of (two) symbols, working together: [ "%" and "$"], and [ "@" and "$" ]. So, "%" shouldn't be seen as fixed and permanent,but one working in conjunction with "$", which syntactically replaces it whenever a singularity it contains is referred to.

    With the associative array, you actually have A SET of (all) THREE sigils working together semantically/syntactically ( [ "%", "$", and "@" ] ):

    %pets=qw(John dog Jane cat Bobby gerbil Laura monkey Mike cobra Betty falcon Sally dog Sheila rabbit Chuck dog Jeremy horse);

    print join(', ', @pets{qw(Bobby Betty Jane)}), "\n";


    @slackers=qw(Mike Bobby Laura Sheila);

    print join(', ', @pets{@slackers}), "\n";

    or, using plural subscripting for both hash and array, one nested in the other:

    print "The pets of the first and last slackers: ", join(', ', @pets{@slackers[0,$#slackers]}), "\n";

    It seems to me that the above is brilliant language design, which allows for efficient (economical), beautiful code.

    And my examples are just the surface of all that's possible, in terms of the coding power this interplay between the (distinct) plural and singular syntax grants. (Again: when you get to multi-dimensional arrays, multi-dimensional hashes and multi-dimensional array/hash hybrids, this sigil interplay in Perl5 is INVALUABLE, and probably unrivaled by any other language out there! (correct me if I'm wrong))

    Also, again, conceptually, the notion of "singular" versus "plural" is FUNDAMENTAL to human psychology and reason. It is healthy and profitable to meticulously address and sort out logic related to it. This Perl syntax FORCES one to fully wrap one's mind around what's going on, conceptually... which is a good thing, in the end.
    Andrew, Jan 22, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.