Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

YAML has a thing called “implicit typing” where unquoted values are translated into language-specific entities. What you expect is what when you write the number 1, you might get an integer back. You do. What you might not expect is that when you type the word yes, you get a boolean back and not the word.

We are using YAML 1.0 but the documentation for this has fallen off the internet. It can still be retrieved from the internet archive at http://web.archive.org/web/20041204101603/http://yaml.org/spec/type.html. YAML 1.1+’s type system is still expansive but it is also still linked from the yaml spec at http://yaml.org/type/.

The following YAML document does all kinds of strange things depending on whether Ruby or Perl is reading and writing it. In general, Ruby’s default library is more magical and requires hand-holding to cope with whatever it did. To force “ordinary” interpretations, quote everything. Be liberal with it. Quote more than you think a person should ever reasonably be expected to quote.

The following YAML document is 99% magic. The only non-magical things are the strings bool, float, int, and date. Everything else is potentially transmogrified. Everything else is potentially transformed by YAML’s implicit typing.

--- bool: true: [ y, Y, yes, Yes, YES, true, True, TRUE, on, On, ON ] false: [ n, N, no, No, NO, false, False, FALSE, off, Off, OFF ] float: [ .inf, +.inf, -.inf, .nan ] int: [ 0b10, 010, 0xf, 19:59 ] null: [ ~, null ] date: [ 2008-10, 2008-10-02T21:21:42Z-08 ]

The canonical representation for booleans in yaml are the unquoted literals y and n but yaml.rb treats them like strings while converting everything else. Perl’s YAML::Syck defaults to treating things purely as strings unless it is asked to apply implicit typing and then it acts like Ruby. Notice the odd base-60 integer conversion from 19:59 -> 1199. Notice that all integer conversions are valid except binary which as inexplicably left off.

Ruby’s yaml.rb

y -> 'y' Y -> 'Y' Yes, Yes, YES, true, True, TRUE, on, On, ON -> true n -> 'n' N -> 'N' no, No, NO, false, False, FALSE, off, Off, OFF -> false 19:59 -> 1199

Perl’s YAML::Syck, $YAML::Syck::ImplicitTyping = 1

y -> 'y' Y -> 'Y' Yes, Yes, YES, true, True, TRUE, on, On, ON -> '1' / 1 n -> 'n' N -> 'N' no, No, NO, false, False, FALSE, off, Off, OFF -> '' / 0

Ruby's yaml.rb

.inf, +.inf -> Infinity -.inf -> -Infinity .nan -> NaN ~, null -> nil

Perl

.inf, +.inf -> 'inf' / inf -.inf -> '-inf' / -inf .nan -> 'nan' / nan ~, null -> undef

I separated inf, nan, and nil/undef out because their target language implementations are different while the meaning is identical. If you treat the perl strings ‘inf’, ‘-inf’, ‘+inf’, ‘nan’ like numbers, they are numified as the equivalent floating point value. Also, Ruby’s nil and Perl’s undef are really the same thing but they go by different names.

Thought I’d share. Remember, quote, quote, quote, when in doubt, quote. When in Ruby, always, always convert with to_i, to_s or whatever is appropriate for the data. There are some other issues related to undesired de-quoting by passing data through yaml.rb that make the actual type of a value unpredictable.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊


In reply to Dweomer in YAML::Syck and Ruby by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-20 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found