develooper Front page | perl.perl6.language | Postings from July 2006

Anonymous Self-referential Datastructure Literals

From:
Brad Bowman
Date:
July 11, 2006 02:45
Subject:
Anonymous Self-referential Datastructure Literals
Message ID:
44B37351.40900@bereft.net

Just some random thoughts about self-referential structures
and their literal representations:

$ perl -MData::Dumper -e '$a=[1,\$a]; print Dumper($a)'
$VAR1 = [
          1,
          \$VAR1
        ];

$ perl -MYAML -e '$a=[1,\$a]; print Dump($a)'
--- &1
- 1
- !perl/ref:
  =: *1

$ pugs -e 'my @a = (1,\@a); say @a.perl'
$_ := [1, \$_]

So the current pugs implementation is using $_ at compile time to
stitch the value together, is that right?

(Does the $_ binding need to be local/temp, even with $_'s new scoping
and the fact that this is a compile time thing?)

Are there any situations where a yaml-like literal representation
has an advantage over one that requires binding code?

The two main use cases would proabably be debugging and serialization.
For debugging .perl's fine, although many indirections in a more complex
structure may be tougher to decipher than a yaml style label.

Deserialization requires an eval anyway (unless there's some 
yet-to-be-invented hook to parse and return a literal only)
so the binding doesn't seem to matter.  The .perl method is
unlikely to be the serialization method of choice anyhow.

Nevertheless, I have a vague feeling that the YAML approach may 
be better in some way...

For another perspective:

$ python -c 'a=[1]; a.append(a); print repr(a)'
[1, [...]]

Ahem, time for the appropriate signature, 

Brad

-- 
 There are times when a person gets carried away and talks on without
 thinking too much. But this can be seen by observers when one's mind is
 flippant and lacking truth. -- Hagakure http://bereft.net/hagakure/



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About