Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

YAML module issue

by devnul (Monk)
on May 12, 2006 at 02:06 UTC ( [id://548882]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I'm having an issue with YAML module. Hopefully someone can help.

Let me be brief:

perl -e 'use YAML;$data{'zip'} = "07102";print YAML::Dump(\%data)' Outputs:
--- #YAML:1.0 zip: 07102
My read of the spec (and experience with the parsers so far) is that a properly functioning parser will treat this as octal and return the value 3650. What I need is:
zip: '07102'
Any ideas?

Devnul

Replies are listed 'Best First'.
Re: YAML module issue
by dragonchild (Archbishop) on May 12, 2006 at 02:44 UTC
    • Do you have a failing test? If so, submit it to Ingy.
    • Use YAML::Syck

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      You made me curious, especially since here at work I'm investigating the possibility of using YAML to serialize some data. So I installed YAML::Syck and tried it on the same data as the OP:

      $ perl -MYAML::Syck -le 'print Dump { zip => "07102" }' --- zip: 07102

      So the behaviour is just the same. But I also get

      $ perl -MData::Dumper -MYAML::Syck -le 'print Dumper Load Dump { zip = +> "07102" }' $VAR1 = { 'zip' => '07102' };

      which is not what he fears. That only happens if $YAML::Syck::ImplicitTyping is set to a true value (by default, it's false):

      $ perl -MData::Dumper -MYAML::Syck -le '$YAML::Syck::ImplicitTyping=1; print Dumper Load Dump { zip => "07102" }' $VAR1 = { 'zip' => 3650 };

      Hmm... how to write a dependency-installation failure test that goes inside the distribution... if I can figure that out, the recursive nature sounds very Ingy-ish.

      # t/yaml_install.t use Test::Install 'YAML';

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        Thanks for your comments.

        The spec seems pretty clear that something like:

        zip: 07102
        would be implicitly typed as an octal integer. I also tried YAML::Syck, but can't get it to produce a "valid" YAML file that can be read with other parsers which force implicit casting of data types.

        - Devnul
Re: YAML module issue
by ingy (Initiate) on May 13, 2006 at 07:06 UTC

    Well it's like this. In YAML, every document has a "schema" which determines exactly what type or tag every node in the document has. For two applications to share a document, they need to agree on a "schema".

    I quote "schema" because it isn't really a tangible thing like XML schema language. In YAML, there is not yet an official schema language.

    A YAML schema can come in one of the following forms:

    • Every scalar is a string. This is (or at least should be) the default schema for your general purpose YAML Dumper/Loader.
    • YAML implicit typing is honored. (Implicit typing is what turns 07102 into 3650) This is usually not the default, but some processors might choose this.
    • The YAML Loader/Dumper chooses a schema of its own. This is in effect what all loader/dumpers should do. Basically it means that it can load whatever it dumped into the same internal structure.
    • The application chooses the schema. This is used when every scalar is a string, usually. The program says, "oh the string at $y->{event}{date} should be a date object".
    • An external schema document can be used to declare the type of each node. Nobody does this yet.

    YAML.pm's strategy is to load all unquoted scalars as strings except for ~ which becomes undef.

    If you want to use YAML.pm with other yaml processors you need to agree on a schema, which really means you need to find out what they do and make sure everything is in sync for your needs.

    Cheers, Ingy

      I agree with everything you've said. Is there some way to make YAML.pm quote all scalars so that I can make it agree with the "schema" of a Loader that doesn't allow much adjustment?

      dEvNuL

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://548882]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found