App-Dex
view release on metacpan or search on metacpan
scripts/dex view on Meta::CPAN
| PRESERVE_FLOW_STYLE | PRESERVE_ALIAS
);
my ($hash, $styles, $flow) = $yp->load_file($file);
$yp->dump_file($hash, $styles, $flow);
Then dumping it will return the same output.
Only folded block scalars '>' cannot preserve the style yet.
Note that YAML allows repeated definition of anchors. They cannot be preserved
with YAML::PP right now. Example:
---
- &seq [a]
- *seq
- &seq [b]
- *seq
Because the data could be shuffled before dumping again, the anchor definition
could be broken. In this case repeated anchor names will be discarded when
loading and dumped with numeric anchors like usual.
Implementation:
When loading, hashes will be tied to an internal class
(C<YAML::PP::Preserve::Hash>) that keeps the key order.
Scalars will be returned as objects of an internal class
(C<YAML::PP::Preserve::Scalar>) with overloading. If you assign to such
a scalar, the object will be replaced by a simple scalar.
# assignment, style gets lost
$styles->[1] .= ' append';
You can also pass C<1> as a value. In this case all preserving options will be
enabled, also if there are new options added in the future.
There are also methods to create preserved nodes from scratch. See the
C<preserved_(scalar|mapping|sequence)> L<"METHODS"> below.
=back
=head2 load_string
my $doc = $ypp->load_string("foo: bar");
my @docs = $ypp->load_string("foo: bar\n---\n- a");
Input should be Unicode characters.
So if you read from a file, you should decode it, for example with
C<Encode::decode()>.
Note that in scalar context, C<load_string> and C<load_file> return the first
document (like L<YAML::Syck>), while L<YAML> and L<YAML::XS> return the
last.
=head2 load_file
my $doc = $ypp->load_file("file.yaml");
my @docs = $ypp->load_file("file.yaml");
Strings will be loaded as unicode characters.
=head2 dump_string
my $yaml = $ypp->dump_string($doc);
my $yaml = $ypp->dump_string($doc1, $doc2);
my $yaml = $ypp->dump_string(@docs);
Input strings should be Unicode characters.
Output will return Unicode characters.
So if you want to write that to a file (or pass to YAML::XS, for example),
you typically encode it via C<Encode::encode()>.
=head2 dump_file
$ypp->dump_file("file.yaml", $doc);
$ypp->dump_file("file.yaml", $doc1, $doc2);
$ypp->dump_file("file.yaml", @docs);
Input data should be Unicode characters.
=head2 dump
This will dump to a predefined writer. By default it will just use the
L<YAML::PP::Writer> and output a string.
my $writer = MyWriter->new(\my $output);
my $yp = YAML::PP->new(
writer => $writer,
);
$yp->dump($data);
=head2 preserved_scalar
Since version 0.024
Experimental. Please report bugs or let me know this is useful and works.
You can define a certain scalar style when dumping data.
Figuring out the best style is a hard task and practically impossible to get
it right for all cases. It's also a matter of taste.
use YAML::PP::Common qw/ PRESERVE_SCALAR_STYLE /;
my $yp = YAML::PP->new(
preserve => PRESERVE_SCALAR_STYLE,
);
# a single linebreak would normally be dumped with double quotes: "\n"
my $scalar = $yp->preserved_scalar("\n", style => YAML_LITERAL_SCALAR_STYLE );
my $data = { literal => $scalar };
my $dump = $yp->dump_string($data);
# output
---
literal: |+
...
=head2 preserved_mapping, preserved_sequence
( run in 0.706 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )