IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Here's a smalltalk version
| den norm |

den := Dictionary newFrom: {
'one'->'a'.
'two'->'b'.
'three'->'c'.
'four'->'b'.
'five'->'c'.
'six'->'a'.
'seven'->'b'
}.

norm := Dictionary new.
den keysAndValuesDo: [:key :val |
(norm at: val ifAbsentPut: (OrderedCollection new)) add: key].

The wackiness in the perl seems to be that you cannot use a reference as the thing to which it refers. Which to me implies that reference is poorly named. C++ references are indistinguishable from the things they reference. Ditto file references in unix (symbolic links act just like the files to which they refer most of the time).

I'd be inclined to think of the perl reference as a "pointer" which requires dereferencing to actually use.

Will this thinking mess me up?



That was lovely cheese.

     --Wallace, The Wrong Trousers
New Yes, think of a reference as a pointer
My understanding of the reasoning behind the name is that a reference is supposed to keep track of what it points to and garbage collect it if needed. Perl does that. Therefore it isn't a pointer and shouldn't be called on.

As for whether a reference should look like what it is pointing at, I'd call that choice consistent with Perl's design. Think C for a second. Suppose that foo is a struct and bar a pointer to foo. You can access a field baz in the struct either through foo.baz or bar->baz.

Let's carry this analogy through to references and OO syntax. If you think of objects, etc as immediate, then a C-trained person would be inclined to use the . notation. Unfortunately Perl had already used . for string concatenation and so in Perl 5 they didn't want to use it for method calls etc. However for a C-trained person it makes just as much sense to say that there is a level of indirection visible everywhere, and you use -> instead. Which is exactly what Perl did. You access things through a reference using the notation that you would for a pointer in C, and this notation is used for method calls as well.

The idea of this indirection goes surprisingly far. Take, for instance, the following:
\nmy $bar = my $baz = bless {}, 'Package::A';\nbless $bar, 'Package::B';\nprint ref($baz); # prints Package::B\n
What happened is that since an object is a blessed reference, and everything OO in Perl happens through a layer of indirection, the package an object belongs to is stored on the data that is object, not in the reference pointing to the object. So the effect of calling bless on one reference to the object is visible from every other copy of said object.

Unfortunately for Perl, nobody else made that design choice. So as reasonable a choice as it may have seemed when it was made a decade ago, it causes pain and confusion now. Which is why Perl 6 (whose syntax is deliberately not backwards compatible with Perl 5) will reverse that choice.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
     Perl frustrations - (tuberculosis) - (45)
         Simple solution - (ben_tilly) - (10)
             Bleh. - (admin) - (3)
                 That's because you approached it wrong - (ben_tilly) - (2)
                     I'm gradually warming up to it - (deSitter)
                     That should be in the man page. - (static)
             Is this a sort of typecasting? - (tuberculosis) - (2)
                 It's a reference -NT - (Simon_Jester)
                 Sort of - (ben_tilly)
             Interesting.... - (Simon_Jester) - (2)
                 The solution would have its own problems - (ben_tilly) - (1)
                     <bow> thank you. -NT - (Simon_Jester)
         THank you for stepping on this rake - (Arkadiy) - (33)
             He was overcomplicating it - (ben_tilly) - (32)
                 Well, maybe - (tuberculosis) - (17)
                     Agreed - (ben_tilly) - (16)
                         Agree its a mistake - (tuberculosis) - (15)
                             Same here. - (admin)
                             Right - (ben_tilly)
                             Shame about the inertia. Python's design is "least surprise" - (FuManChu) - (12)
                                 When I started with Perl... - (ben_tilly) - (11)
                                     On Perl 6 - (pwhysall) - (3)
                                         Re: On Perl 6 - (Yendor)
                                         I had a nice response to this typed up - (ben_tilly) - (1)
                                             Many thanks for that - (pwhysall)
                                     We're having a little brown bag on Ruby - (tuberculosis) - (4)
                                         Re: We're having a little brown bag on Ruby - (JimWeirich) - (3)
                                             Shhh! Anonymous Todd works at some other... - (CRConrad)
                                             I might have been there - (tuberculosis) - (1)
                                                 Re: I might have been there - (JimWeirich)
                                     Re: strict -- have you seen pychecker? - (FuManChu) - (1)
                                         No I hadn't, thanks - (ben_tilly)
                 BTW Arkadiy, I'm still waiting for a response - (ben_tilly) - (13)
                     I am not saying it's shorter in C... - (Arkadiy) - (10)
                         And now for my real comment - (ben_tilly) - (2)
                             May be it's a hindsight thing - (Arkadiy) - (1)
                                 It could be many things - (ben_tilly)
                         I don't know - multimap - (Simon_Jester) - (6)
                             Re: I don't know - multimap - (Arkadiy) - (5)
                                 Why the worry about efficiency? - (ben_tilly) - (4)
                                     I worry about efficiency - (Arkadiy) - (1)
                                         Ah - (ben_tilly)
                                     C vs Perl - efficiency - untrue for me - (broomberg) - (1)
                                         Point - but you may want to rebenchmark - (ben_tilly)
                     Here's a smalltalk version - (tuberculosis) - (1)
                         Yes, think of a reference as a pointer - (ben_tilly)

From Cap'n Billy's Whizz Bang!
89 ms