Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Perl 5's greatest limitation is...?

by Fletch (Bishop)
on Jul 28, 2005 at 13:25 UTC ( [id://478958]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl 5's greatest limitation is...?
in thread Perl 5's greatest limitation is...?

But you can setup almost-autovivification if you want it, however it's not as transparent as in Perl. The Hash.new method can take a block which is passed the instance and the missing key when you try and access a non-existent key. So if you wanted to have it autovivify with an Array instance you do something like:

h = Hash.new { |h,k| h[k] = Array.new }

Similarly Array.new has a block initializer which will let you pre-fill it with a computed value (although it is missing something analogous to the Hash version mentioned above; then again there's nothing to stop you from extending the builtin Array class to do so either; in fact you might even could use reflection to see if the block takes one or two arguments and emulate the old behavior as well).

Update: And yes, this is very different from the Perl case. Ruby is relying on explicit instructions from the programmer rather than syntactic hints (which I think is what you were getting at, rather than saying you absolutely can't do it in Ruby).

--
We're looking for people in ATL

Replies are listed 'Best First'.
Re^5: Perl 5's greatest limitation is...?
by tilly (Archbishop) on Jul 28, 2005 at 22:45 UTC
    I was indeed trying to get at what you think I was trying to get at. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found