[Tutor] Perl vs. Python's way of handling references.

Michael Janssen Janssen@rz.uni-frankfurt.de
Fri Apr 11 18:57:01 2003


On Fri, 11 Apr 2003, Scott Chapman wrote:

> Here's two code snippets, one in Perl, the other Python:
>
> > $ perl
> > $one = 1;
> > $two = 2;
> > %dict = ( a => 'one', b => 'two' );

this set up key-string pairs, right? You propably explain what this perl
syntax does, because tutor@python.org isn't the place with the deepest
insight into perl syntax. Me for my part am asking, why the interpreter
doesn't complain about "a" written as non string. Does the interpreter
A) take it implicite as a string? Or B) did you indeed set up a variable
a?

The value part are strings and no nested references or how to call it,
otherwise you need to use $one.... look forward:

> > print ${ $dict{a} }, "\n"

I've detected a very supicios ${ } around the hash lookup "$dict{a}" ;-)
Does $dict{a} indeed returns 'one' as a mere string and the second ${ } is
shell like syntax for variable lookup (like ${HOME} )?

Then, it's the same hack in both languages.


---

What did you wanted to show us? How perl can store "values as variables"
(just to give it a name) into datastructures? How you can transform $one
anywhere and retrieve the updated value from %dict?

My advice is to forget this. Python has its own ways to handle "shared
values": classes or nifty default-argument-tricks or ?. It's already hard
enough with just this stuff. No need to reimplement perl behaviour or
programming style (No, i'm not saying perl's bad, but I'm saying that
python is a different language).

Michael