Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by tilly (Archbishop)
on Jul 28, 2005 at 07:25 UTC ( [id://478858]=note: print w/replies, xml ) Need Help??


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

Here is an example.

In Ruby when you write x[foo] then Ruby calls the [] method on x with foo as an argument. Unless that is followed by an = in which case you call the []= method instead. So by providing those two methods, that piece of syntax is supported. And lots of core data types provide those two methods.

The result is that x[foo][bar][baz] might be the equivalent of $x[$foo][$bar][$baz] in Perl, or perhaps $x{$foo}{$bar}{$baz} in Perl, or perhaps $x{$foo}[$bar]->($baz) - and that is before you get into user-defined types!

In Perl the first one of those might create anonymous arrays, and the second might create anonymous hashes. In Ruby if you're accessing something that isn't there it has to be an error because there aren't enough syntactical hints around to guess what the programmer meant.

The flip side of that is that in Ruby to create something that, say, acts like a hash backed by a dbm, all that you have to do is define a class with those two methods, and make those methods do the right thing. If you also want to add a locking method for your dbm class, you just have to add the locking method. And it works.

tie in Perl only looks impressive because Perl's syntax is constantly setting up expectations about what the syntax does. Therefore causing that syntax to do something other than what it looks like it should do is very surprising. Ruby's syntax sets no such expectations, and so making the syntax do something new does not look impressive. Until you sit down and realize that making something sophisticated look routine is a pretty impressive accomplishment!

Replies are listed 'Best First'.
Re^4: Perl 5's greatest limitation is...?
by Fletch (Bishop) on Jul 28, 2005 at 13:25 UTC

    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

      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://478858]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found