dgaramond2 has asked for the wisdom of the Perl Monks concerning the following question:

I need to load some "named symbols" which I encode in YAML as:

!var [foo]

which in Perl becomes (bless ['foo'], 'var'), which will be reblessed into the "real" Perl class for later processing. Since these named symbols will be mentioned a lot, I wonder if I can use a simpler syntax like:

!var foo

which can become (bless(do{\(my $o = 'fooz')}, 'var')). Currently YAML::Syck simply load the above as scalars, while Ruby's YAML.rb does interpret this.

Any ideas of simpler syntaxes for my named symbols?

Thanks in advance.

  • Comment on YAML::Syck and blessed references of scalars

Replies are listed 'Best First'.
Re: YAML::Syck and blessed references of scalars
by ferreira (Chaplain) on Mar 23, 2007 at 10:54 UTC

    Unlike Khen1950fx, I don't expect the syntax for tagged data types to be considered simple. As a matter of fact, YAML::Tiny croaks "YAML::Tiny does not support explicit tags" if you feed them to the module

    There might be some bug in YAML::Syck which is preventing your case to work (!var foo), in contrast to (!var [foo]). Because

    pirl $> YAML::Syck::Load "--- !!perl/array:var [foo]\n" # long tags $var = bless( [ 'foo' ], 'var' ); pirl $> YAML::Syck::Load "--- !var [foo]\n" # short tags $var = bless( [ 'foo' ], 'var' );
    should work just like
    pirl $> YAML::Syck::Load "--- !!perl/scalar:var foo\n" # long tags, ok +! $var = bless( do{\(my $o = 'foo')}, 'var' ); pirl $> YAML::Syck::Load "--- !var foo\n" # short tags, :( $var = 'foo';
    which it doesn't.
      Ah, it's a bug then. Submitted to RT. I'll be using the long tag version for now, hoping that YAML::Syck will be fixed soon to support the short version. Thanks!
Re: YAML::Syck and blessed references of scalars
by Khen1950fx (Canon) on Mar 23, 2007 at 06:16 UTC
    For simpler syntaxes, give YAML::Tiny a try. It's designed to read and write YAML-style files with as little code as possible. It has two functions that I think that you can use. The Dump function turns Perl data into YAML, and the Load function turns YAML data into Perl data.