Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Half-serious quest for prefix anonymous refs taking

by starbolin (Hermit)
on May 29, 2008 at 18:29 UTC ( [id://689063]=note: print w/replies, xml ) Need Help??


in reply to Half-serious quest for prefix anonymous refs taking

blazar writes:

Yet it somehow bothers me to have to assign to a temporary variable only to take a reference to it,
Except that a holder for the data is also created which will persist until the reference you created is destroyed. So it's not really 'temporary' and when viewed in that light the syntax does not appear so arbitrary.

For quick and dirty code I'd write your dopen like this:

sub dopen { my $ref = []; @$ref = map { open my $fh, '<', $_ or warn "Can't open `$_': $!\n"; <$fh>; } @_; return $ref; }
Which communicates the intent of passing a reference. It's also more in line with the syntax of an object constructor should you need to rewrite using objects.

It seems like what you are really trying to do is to re-invent perl objects outside of the symbol table. Which I'm not convinced would be such a good idea.


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

Replies are listed 'Best First'.
Re^2: Half-serious quest for prefix anonymous refs taking
by blazar (Canon) on May 30, 2008 at 08:25 UTC
    Except that a holder for the data is also created which will persist until the reference you created is destroyed. So it's not really 'temporary' and when viewed in that light the syntax does not appear so arbitrary.

    I personally believe that you're right. But the fact that the original variable won't be accessible from outside of the sub makes it "temporary" enough for me. In some cases this is the only way to go: you surely know that there's not an anonymous scalar ref constructor, and that people do do {my $x; \$x} instead: ain't it the same thing?

    It seems like what you are really trying to do is to re-invent perl objects outside of the symbol table.

    I don't have the slightest idea of why you think so. This is far from what I'm trying to do. Actually, I'm not trying "to do" anything: I'm rather reasoning about syntax and semantics. But in some deep sense I cannot grasp, you may be right: can you expand on the subject? In particular, can you explain which part of what I wrote may possibly have to do with the symbol table?!? (It seems to me that I'm only munging lexical variables...)

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

      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}
        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found