http://qs321.pair.com?node_id=689378


in reply to Re^2: Half-serious quest for prefix anonymous refs taking
in thread Half-serious quest for prefix anonymous refs taking

The allusion to perl objects came more from the resemblance your dopen subroutine has to an object constructor plus my miscomprehension that you wanted to assign to an anonymous array. After rereading your post, I realize all you asking for was a indirect-object syntax for list constructors equivalent to the indirect-object syntax for list operators such as print and join. I still see similarities to perl objects, not in the Class and Package sense, but in that perl objects take arguments, possibly in indirect-object syntax, and return a reference. Only, in perl objects, the reference is blessed into a class which allows it to be used as a symbolic reference. Thus the symbol table reference.

Yes, if perl allows indirect-object syntax for list operators then it could allow the same for list constructors. However the syntax would have the same ambiguities. Who hasn't typed something like print ( $x - 1 ) / 2; and had to puzzle out why the parser dropped the last term? In the OP you mentioned nested anonymous assignments but the proposed syntax would not allow nested assignments due to the ambiguous syntax.

s/indirect-object/imperative/


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

Replies are listed 'Best First'.
Re^4: Half-serious quest for prefix anonymous refs taking
by blazar (Canon) on May 31, 2008 at 09:51 UTC
    After rereading your post, I realize all you asking for was a indirect-object syntax for list constructors equivalent to the indirect-object syntax for list operators such as print and join.

    I personally believe that either you should reread my posts again or that I have terribly serious expressive problems for all that I "want" (please, notice the quotes) is a prefix operator that creates an anonymour arrayref or hashref out of a list, alternative to the currently existing circumfix ones: [] and {}. For completeness and consistency, I'm also asking the same thing wrt subrefs and scalar refs. Thus, instead of:

    my $aref = [1..42]; my $href = { foo => 1, bar => 2 }; my $cref = do { my @x=1..42; sub () { @x } }; my $sref = do { my $x=42; \$x };

    I would like, say:

    my $aref = \@ <== 1..42; my $href = \% <== foo => 1, bar => 2; my $cref = \& <== 1..42; my $sref = \$ <== 42;

    Except that this would clash with current Perl syntax, since perl would try to parse e.g. the construct in the first line like a reference to the @< variable, and similarly for the others.

    Thus I'm asking about your own ideas for a good syntax: [] and {} only take two charachters, and I would like something just as lightweight; in addition the operators should intuitively and immediately suggest the kind of reference that they create, hence my idea of somehow using sigils. I'm not asking this out of a practical need, but just for fun and brainstorming. (But if a very good suggestion pops out, maybe...)

    --
    If you can't understand the incipit, then please check the IPB Campaign.

      The word prefix is still throwing me. I think a better term would be predicate. Again, the notation you propose would be ambiguous. In your example: my $href = \% <== foo => 1, bar => 2; That could either mean a hash whose first key is a hashref 'foo' or a reference to a hash with one key 'foo' followed by the comma operator and a two element list ( bar, '2' ). Although in the case provided it may be clear what you intended, the ambiguity increases if one were to attempt to nest operators:

      my $ref = \%< ship => Serenity, crew => \@< Mal, Wash, Jane, Zoe, pas +sengers => \@< Simon, Book
      Perl has no way of knowing you want 'passengers' to be in the same hash as 'ship' but instead you would end up with crew =>[Mal, Wash, Jane, Zoe, passengers, [Simon, Book]]Certainly not what was intended.


      s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
        The word prefix is still throwing me. I think a better term would be predicate.

        I personally believe that it won't and I can't understand why it's throwing you, given that it's the correct technical term. In Perl 6 you can actually declare prefix, postfix, circumfix, etc. operators, which are basically regular functions.

        Again, the notation you propose would be ambiguous.

        It was just a very wild shot at it, to explain the kind of beast I "want," not a possible description of the beast. Actually I pointed out it couldn't be, for a well defined reason. That's why I'm asking here.

        In your example: my $href = \% <== foo => 1, bar => 2; That could either mean a hash whose first key is a hashref 'foo' or a reference to a hash with one key 'foo' followed by the comma operator and a two element list ( bar, '2' ). Although in the case provided it may be clear what you intended, the ambiguity increases if one were to attempt to nest operators:

        I call that an ordinary precedence issue, (except that I didn't specify the precedence of the operator nor do I have any precise idea about it) to be resolved with the use of parens when not doing the right thing without them.

        --
        If you can't understand the incipit, then please check the IPB Campaign.