Miércoles 14 Abril 2004 14:18, Juerd wrote:
> I propose to use ` as a simple hash subscriptor, as an alternative
> to {} and <<>>. It would only be useable for \w+ keys or perhaps
> -?\w+. As with methods, a simple "atomic" (term exists only in
> perlreftut, afaix, but I don't know another word to describe a
> simple scalar variable) scalar should be usable too.

If we really need a ultra-huffman encoding for hash subscriptors, I 
have always dreamt of being able to do:

  %hash/key
  $hashref/foo/bar/baz/quux
  ...

If only because of the filesystem analogy. Because a filesystem is 
really just a very big tied hash, isn't it?

The idea would be be to replace the %hash{'key'} notation by the slash 
one, thus making {} always mean a closure without nothing to do with 
subscripting.

It would work just like with methods so we would have:

   %hash/key         # like $obj.method
   %hash/$key       # like $obj.$method

   %hash/{ some_func() }             # dynamic key
   %hash/«key1 key2»                 # hash slice
   %hash/['key', 'key2']                  # the same

The benefits I see in terms of clarity are:

   * {} means one thing (closure) and just one

   * « and » have only two meanings (literal array and hyperoperator) 
instead of three

   * «a b» and ['a', 'b'] are always substitutable, ever

The cultural assumption of / as a subscripter is further reinforced 
with the omnipresence of xpath these days. We could play some tricks 
with this, too. A xml library could make every node a tied hash, 
effectively embedding a good portion of xpath within perl:

  my $price = $doc/books[14]/price;

  for $doc/books -> $book {
     print "Price is $book/price and title is $book/title";
  }
  
(scalar and array context with help us to overcome the "an xpath 
expression always returns a sequence syndrome")

Pushing the analogy a bit too further away, one could hack the grammar 
so that a leading / does indicate the root directory in the system 
(and a leading ./ indicates the current directory), thus letting me 
write:

   for /home/angel -> $file {
            print $file;                                  
   }

                                                           
Which looks cute for shell scripting, althought a bit dangerous maybe.

And so on...

The beauty (?) of this is not so much in that we should play these 
tricks, but in that we are reusing a good deal of cultural background 
in them.

Oh, and saving a few keystrokes when you are dealing with hashes the 
whole day (say, because you are using DBI, or extracting some data 
from an XML document, or whatever) is not totally unpleasing.

-angel

Reply via email to