in reply to Re: Re: •Re: On Declaration
in thread On Declaration

In a sense the term "anonymous" is irrelevent when you are dealing with a reference to an object.

In a practical sense maybe, but I think it draws a very important distinction. Even in practice, it is at least somewhat helpful¹.

From an instructional perspective, I think it is useful to explain the difference between

my $r1 = [ { foo => 'baz', bar => 'qux' } ];
. . . and . . .
my %h = ( foo => 'baz', bar => 'qux' ); my $r2 = [ \%h ];

I think it is especially useful when you are speaking of an initialization and an anonymous composer because, at that point, you know all of the references to the anonymous data are accounted for. Even in the general case though, saying out loud, "a reference to an anonymous array containing a reference to an anonymous hash" is giving more information than saying "a reference to an anonymous array containing a reference to a hash." The latter makes me want to ask, "which hash?"

Fixed: Missing closing brace in first bit o' code. Thanks zby.

Fixed: Yes, I meant %h. Thanks demerphq.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Re: •Re: On Declaration
by demerphq (Chancellor) on Jun 17, 2003 at 18:46 UTC

    Er, I think you made a mistake in your example. Did you really mean @h? Don't you mean %h?

    Assuming that you mean %h, I personally see no difference. $r2 and $r1 are identical in all respects as far as perl is concerned. And this is my point. my $x=do { \my @anon }; is identical in all useful aspects to my $x=[];. Without using magic there is no way for an external entity to tell if a reference to an array is a reference to a named scalar that is now out of scope or if it is a reference to an array that never had a name. And since there is no way for perl to tell the difference IMO you can only be confusing people when you say a "reference to an anoymous array" in any other curcumstance than saying that the [] symbols (normally) create one.

    you know all of the references to the anonymous data are accounted for.

    Im not sure I understand what you mean here. I dont need to account for anonymous data. Perl does it for me. :-)

    "a reference to an anonymous array containing a reference to an anonymous hash" is giving more information than saying "a reference to an anonymous array containing a reference to a hash."

    My problem with this is that if used your description for the documentation for an argument for a subroutine then somebody might think that is ok to say foo([{}]) but that it not ok to say my (%h,@a); @a=(\%h); foo(\@a). If you leave the issue of anonymous objects to the discussions of how to construct one then no such confusion arises.

    The latter makes me want to ask, "which hash?"

    You already have a reference to it, why do you care what and or if it has a name?

    Let me take this a step further. Why dont you add "lexical" and "dynamic" to your description? After all a lexical array is different from an anonymous dynamic array. (Or is it? IMO its not, but whatever :-). So why not say "a named lexical reference to an anonymous dynamic array containing a reference to a lexical anonymous hash"?

    Bah! Truth be told, id rather say "an arrayref of hashrefs" than any of the above.

    :-)


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
      Did you really mean @h?

      Yes. And that's what I get for going back and changing the example.

      when you say a "reference to an anoymous array" in any other curcumstance than saying that the [] symbols (normally) create one.

      We might be agreeing about more than we are disagreeing about. I am talking about when one is created, or to use the the terminology you'll find in the docs, composed. As far as I can tell, that's exactly what we've been talking about through this whole discussion.

      I dont need to account for anonymous data. Perl does it for me. :-)

      The fact that perl does it for you does not entirely eliminate the need to do it yourself. Have you never found yourself needing to be sure that the last reference to some data was going out of scope? When you want the memory occupied by some piece of data to be garbage collected, you need to know that the last reference to it is going away because, otherwise, perl's accounting may work against you.

      You already have a reference to it, why do you care what and or if it has a name?

      I care if it is being used elsewhere in the program. Knowing where it is being used other than "here" helps me understand what is going on both "here" and "there." (Looking at this from the maintenance programmer's POV might help.) I've heard (and played a part in) the conversations many times: "Wait, so this holds a reference to the options hash?" "No, here's the assignment, it's an anonymous hash that holds a copy of the host-specific options." That sort of thing comes up all the time.

      After all a lexical array is different from an anonymous dynamic array.

      I'm not even sure I understand the distinction you are trying to make. In what sense are you associating the words "lexical" or "dynamic" with data? Lexical and dynamic are terms used to describe the names given to the data; i.e. the variables. Data isn't scoped, identifiers are. (In Perl, you don't even have to make the distinction between heap and stack.) Terms like "a named lexical reference" and "an anonymous dynamic array" are nonsensical.

      Bah! Truth be told, id rather say "an arrayref of hashrefs" than any of the above.

      The only thing that matters, of course, is that you communicate your meaning to your audience. If they understand an "arrayref of hashrefs", then use it. I just wouldn't recommend using that terminology with a general audience. ;-)

      -sauoq
      "My two cents aren't worth a dime.";