- Timestamp:
- Mar 31, 2020, 9:03:48 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
devel/pb-modules/lib/ProjectBuilder/YAML.pm
r2484 r2495 2 2 use strict; 3 3 use warnings; 4 package YAML::Tiny; # git description: v1.72-7-g8682f63 4 # Original is YAML::Tiny git description: v1.72-7-g8682f63 5 # We rename it here to allow embedded usage during setupv 6 # when no YAML implementation is available on the target system 7 # 8 package ProjectBuilder::YAML; 5 9 # XXX-INGY is 5.8.1 too old/broken for utf8? 6 10 # XXX-XDG Lancaster consensus was that it was sufficient until … … 10 14 11 15 ##################################################################### 12 # The YAML::TinyAPI.16 # The ProjectBuilder::YAML API. 13 17 # 14 18 # These are the currently documented API functions/methods and … … 17 21 use Exporter; 18 22 our @ISA = qw{ Exporter }; 19 our @EXPORT = qw{ LoadDump };20 our @EXPORT_OK = qw{ LoadFileDumpFile freeze thaw };23 our @EXPORT = qw{ pb_Load pb_Dump }; 24 our @EXPORT_OK = qw{ pb_LoadFile pb_DumpFile freeze thaw }; 21 25 22 26 ### 23 27 # Functional/Export API: 24 28 25 sub Dump {26 return YAML::Tiny->new(@_)->_dump_string;29 sub pb_Dump { 30 return ProjectBuilder::YAML->new(@_)->_dump_string; 27 31 } 28 32 … … 30 34 # XXX-XDG I think first would seem more natural, but I don't know 31 35 # that it's worth changing now 32 sub Load {33 my $self = YAML::Tiny->_load_string(@_);36 sub pb_Load { 37 my $self = ProjectBuilder::YAML->_load_string(@_); 34 38 if ( wantarray ) { 35 39 return @$self; … … 43 47 # XXX-XDG I don't think so. I'd support deprecating them. 44 48 BEGIN { 45 *freeze = \& Dump;46 *thaw = \& Load;47 } 48 49 sub DumpFile {49 *freeze = \&pb_Dump; 50 *thaw = \&pb_Load; 51 } 52 53 sub pb_DumpFile { 50 54 my $file = shift; 51 return YAML::Tiny->new(@_)->_dump_file($file);52 } 53 54 sub LoadFile {55 return ProjectBuilder::YAML->new(@_)->_dump_file($file); 56 } 57 58 sub pb_LoadFile { 55 59 my $file = shift; 56 my $self = YAML::Tiny->_load_file($file);60 my $self = ProjectBuilder::YAML->_load_file($file); 57 61 if ( wantarray ) { 58 62 return @$self; … … 67 71 # Object Oriented API: 68 72 69 # Create an empty YAML::Tinyobject73 # Create an empty ProjectBuilder::YAML object 70 74 # XXX-INGY Why do we use ARRAY object? 71 75 # NOTE: I get it now, but I think it's confusing and not needed. … … 73 77 # 74 78 # XXX-XDG I don't support changing it yet. It's a very well-documented 75 # "API" of YAML::Tiny. I'd support deprecating it, but Adam suggested79 # "API" of ProjectBuilder::YAML. I'd support deprecating it, but Adam suggested 76 80 # we not change it until YAML.pm's own OO API is established so that 77 81 # users only have one API change to digest, not two … … 86 90 # characters/strings. These take the data to/from storage to native 87 91 # Perl objects, which the terms dump and load are meant. As long as 88 # this is a legacy quirk to YAML::Tinyit's ok, but I'd prefer not92 # this is a legacy quirk to ProjectBuilder::YAML it's ok, but I'd prefer not 89 93 # to add new {read,write}_* methods to this API. 90 94 … … 134 138 # XXX-INGY 135 139 # I(ngy) need to decide if these values should be quoted in 136 # YAML::Tinyor not. Probably yes.140 # ProjectBuilder::YAML or not. Probably yes. 137 141 138 142 # These 3 values have special meaning when unquoted and using the … … 158 162 159 163 ##################################################################### 160 # YAML::TinyImplementation.164 # ProjectBuilder::YAML Implementation. 161 165 # 162 166 # These are the private methods that do all the work. They may change … … 272 276 } elsif ( ! $in_document && @$self ) { 273 277 # only the first document can be explicit 274 die \" YAML::Tinyfailed to classify the line '$lines[0]'";278 die \"ProjectBuilder::YAML failed to classify the line '$lines[0]'"; 275 279 } elsif ( $lines[0] =~ /^\s*\-(?:\s|$|-+$)/ ) { 276 280 # An array at the root … … 292 296 293 297 # uncoverable statement 294 die \" YAML::Tinyfailed to classify the line '$lines[0]'";298 die \"ProjectBuilder::YAML failed to classify the line '$lines[0]'"; 295 299 } 296 300 } … … 345 349 # Special cases 346 350 if ( $string =~ /^[\'\"!&]/ ) { 347 die \" YAML::Tinydoes not support a feature in line '$string'";351 die \"ProjectBuilder::YAML does not support a feature in line '$string'"; 348 352 } 349 353 return {} if $string =~ /^{}(?:\s+\#.*)?\z/; … … 352 356 # Regular unquoted string 353 357 if ( $string !~ /^[>|]/ ) { 354 die \" YAML::Tinyfound illegal characters in plain scalar: '$string'"358 die \"ProjectBuilder::YAML found illegal characters in plain scalar: '$string'" 355 359 if $string =~ /^(?:-(?:\s|$)|[\@\%\`])/ or 356 360 $string =~ /:(?:\s|$)/; … … 360 364 361 365 # Error 362 die \" YAML::Tinyfailed to find multi-line scalar content" unless @$lines;366 die \"ProjectBuilder::YAML failed to find multi-line scalar content" unless @$lines; 363 367 364 368 # Check the indent depth … … 366 370 $indent->[-1] = length("$1"); 367 371 if ( defined $indent->[-2] and $indent->[-1] <= $indent->[-2] ) { 368 die \" YAML::Tinyfound bad indenting in line '$lines->[0]'";372 die \"ProjectBuilder::YAML found bad indenting in line '$lines->[0]'"; 369 373 } 370 374 … … 400 404 return 1; 401 405 } elsif ( length($1) > $indent->[-1] ) { 402 die \" YAML::Tinyfound bad indenting in line '$lines->[0]'";406 die \"ProjectBuilder::YAML found bad indenting in line '$lines->[0]'"; 403 407 } 404 408 … … 436 440 437 441 } else { 438 die \" YAML::Tinyfailed to classify line '$lines->[0]'";442 die \"ProjectBuilder::YAML failed to classify line '$lines->[0]'"; 439 443 } 440 444 … … 457 461 458 462 } else { 459 die \" YAML::Tinyfailed to classify line '$lines->[0]'";463 die \"ProjectBuilder::YAML failed to classify line '$lines->[0]'"; 460 464 } 461 465 } … … 482 486 return 1; 483 487 } elsif ( length($1) > $indent->[-1] ) { 484 die \" YAML::Tinyfound bad indenting in line '$lines->[0]'";488 die \"ProjectBuilder::YAML found bad indenting in line '$lines->[0]'"; 485 489 } 486 490 … … 506 510 } 507 511 elsif ( $lines->[0] =~ /^\s*\?/ ) { 508 die \" YAML::Tinydoes not support a feature in line '$lines->[0]'";512 die \"ProjectBuilder::YAML does not support a feature in line '$lines->[0]'"; 509 513 } 510 514 else { 511 die \" YAML::Tinyfailed to classify line '$lines->[0]'";515 die \"ProjectBuilder::YAML failed to classify line '$lines->[0]'"; 512 516 } 513 517 514 518 if ( exists $hash->{$key} ) { 515 warn " YAML::Tinyfound a duplicate key '$key' in line '$lines->[0]'";519 warn "ProjectBuilder::YAML found a duplicate key '$key' in line '$lines->[0]'"; 516 520 } 517 521 … … 691 695 my ($self, $array, $indent, $seen) = @_; 692 696 if ( $seen->{refaddr($array)}++ ) { 693 die \" YAML::Tinydoes not support circular references";697 die \"ProjectBuilder::YAML does not support circular references"; 694 698 } 695 699 my @lines = (); … … 720 724 721 725 } else { 722 die \" YAML::Tinydoes not support $type references";726 die \"ProjectBuilder::YAML does not support $type references"; 723 727 } 724 728 } … … 730 734 my ($self, $hash, $indent, $seen) = @_; 731 735 if ( $seen->{refaddr($hash)}++ ) { 732 die \" YAML::Tinydoes not support circular references";736 die \"ProjectBuilder::YAML does not support circular references"; 733 737 } 734 738 my @lines = (); … … 760 764 761 765 } else { 762 die \" YAML::Tinydoes not support $type references";766 die \"ProjectBuilder::YAML does not support $type references"; 763 767 } 764 768 } … … 787 791 sub errstr { 788 792 require Carp; 789 Carp::carp( " YAML::Tiny->errstr and \$YAML::Tiny::errstr is deprecated" )793 Carp::carp( "ProjectBuilder::YAML->errstr and \$ProjectBuilder::YAML::errstr is deprecated" ) 790 794 unless $errstr_warned++; 791 795 $errstr; … … 802 806 use B; 803 807 804 # XXX-INGY Is flock YAML::Tiny's responsibility?808 # XXX-INGY Is flock ProjectBuilder::YAML's responsibility? 805 809 # Some platforms can't flock :-( 806 810 # XXX-XDG I think it is. When reading and writing files, we ought … … 852 856 } 853 857 854 delete $ YAML::Tiny::{refaddr};858 delete $ProjectBuilder::YAML::{refaddr}; 855 859 856 860 1; … … 873 877 =head1 NAME 874 878 875 YAML::Tiny- Read/Write YAML files with as little code as possible879 ProjectBuilder::YAML - Read/Write YAML files with as little code as possible 876 880 877 881 =head1 VERSION … … 888 892 The original pure-Perl implementation L<YAML> costs just over 4 megabytes 889 893 of memory to load. Just like with Windows F<.ini> files (3 meg to load) and 890 CSS (3.5 meg to load) the situation is just asking for a B< YAML::Tiny>894 CSS (3.5 meg to load) the situation is just asking for a B<ProjectBuilder::YAML> 891 895 module, an incomplete but correct and usable subset of the functionality, 892 896 in as little code as possible. 893 897 894 Like the other C<::Tiny> modules, YAML::Tinyhas no non-core dependencies,898 Like the other C<::Tiny> modules, ProjectBuilder::YAML has no non-core dependencies, 895 899 does not require a compiler to install, is back-compatible to Perl v5.8.1, 896 900 and can be inlined into other modules if needed. … … 915 919 Read and write F<file.yml> like this: 916 920 917 use YAML::Tiny;921 use ProjectBuilder::YAML; 918 922 919 923 # Open the config 920 my $yaml = YAML::Tiny->read( 'file.yml' );924 my $yaml = ProjectBuilder::YAML->read( 'file.yml' ); 921 925 922 926 # Get a reference to the first document … … 939 943 940 944 # Create a new object with a single hashref document 941 my $yaml = YAML::Tiny->new( { wibble => "wobble" } );945 my $yaml = ProjectBuilder::YAML->new( { wibble => "wobble" } ); 942 946 943 947 # Add an arrayref document … … 958 962 =head1 DESCRIPTION 959 963 960 B< YAML::Tiny> is a perl class for reading and writing YAML-style files,964 B<ProjectBuilder::YAML> is a perl class for reading and writing YAML-style files, 961 965 written with as little code as possible, reducing load time and memory 962 966 overhead. … … 975 979 =for stopwords normalise 976 980 977 L< YAML::Tiny> does not generate comments, it won't necessarily preserve the981 L<ProjectBuilder::YAML> does not generate comments, it won't necessarily preserve the 978 982 order of your hashes, and it will normalise if reading in and writing out 979 983 again. … … 988 992 Features will only be added if they are human readable, and can be written 989 993 in a few lines of code. Please don't be offended if your request is 990 refused. Someone has to draw the line, and for YAML::Tinythat someone994 refused. Someone has to draw the line, and for ProjectBuilder::YAML that someone 991 995 is me. 992 996 … … 995 999 a C compiler). 996 1000 997 To restate, L< YAML::Tiny> does B<not> preserve your comments, whitespace,1001 To restate, L<ProjectBuilder::YAML> does B<not> preserve your comments, whitespace, 998 1002 or the order of your YAML data. But it should round-trip from Perl 999 1003 structure to file and back again just fine. … … 1005 1009 =head2 new 1006 1010 1007 The constructor C<new> creates a C< YAML::Tiny> object as a blessed array1011 The constructor C<new> creates a C<ProjectBuilder::YAML> object as a blessed array 1008 1012 reference. Any arguments provided are taken as separate documents 1009 1013 to be serialized. … … 1012 1016 1013 1017 The C<read> constructor reads a YAML file from a file name, 1014 and returns a new C< YAML::Tiny> object containing the parsed content.1018 and returns a new C<ProjectBuilder::YAML> object containing the parsed content. 1015 1019 1016 1020 Returns the object on success or throws an error on failure. … … 1019 1023 1020 1024 The C<read_string> constructor reads YAML data from a character string, and 1021 returns a new C< YAML::Tiny> object containing the parsed content. If you have1025 returns a new C<ProjectBuilder::YAML> object containing the parsed content. If you have 1022 1026 read the string from a file yourself, be sure that you have correctly decoded 1023 1027 it into characters first. … … 1045 1049 1046 1050 Prior to version 1.57, some errors were fatal and others were available only 1047 via the C<$ YAML::Tiny::errstr> variable, which could be accessed via the1051 via the C<$ProjectBuilder::YAML::errstr> variable, which could be accessed via the 1048 1052 C<errstr()> method. 1049 1053 … … 1057 1061 =head1 FUNCTIONS 1058 1062 1059 YAML::Tinyimplements a number of functions to add compatibility with1063 ProjectBuilder::YAML implements a number of functions to add compatibility with 1060 1064 the L<YAML> API. These should be a drop-in replacement. 1061 1065 1062 =head2 Dump1063 1064 my $string = Dump(list-of-Perl-data-structures);1066 =head2 pb_Dump 1067 1068 my $string = pb_Dump(list-of-Perl-data-structures); 1065 1069 1066 1070 Turn Perl data into YAML. This function works very much like … … 1077 1081 Dies on any error. 1078 1082 1079 =head2 Load1080 1081 my @data_structures = Load(string-containing-a-YAML-stream);1083 =head2 pb_Load 1084 1085 my @data_structures = pb_Load(string-containing-a-YAML-stream); 1082 1086 1083 1087 Turn YAML into Perl data. This is the opposite of Dump. … … 1091 1095 socket. 1092 1096 1093 my $last_data_structure = Load(string-containing-a-YAML-stream);1097 my $last_data_structure = pb_Load(string-containing-a-YAML-stream); 1094 1098 1095 1099 For consistency with YAML.pm, when Load is called in scalar context, it … … 1101 1105 =head2 freeze() and thaw() 1102 1106 1103 Aliases to Dump() andLoad() for L<Storable> fans. This will also allow1104 YAML::Tinyto be plugged directly into modules like POE.pm, that use the1107 Aliases to pb_Dump() and pb_Load() for L<Storable> fans. This will also allow 1108 ProjectBuilder::YAML to be plugged directly into modules like POE.pm, that use the 1105 1109 freeze/thaw API for internal serialization. 1106 1110 1107 =head2 DumpFile(filepath, list)1111 =head2 pb_DumpFile(filepath, list) 1108 1112 1109 1113 Writes the YAML stream to a file with UTF-8 encoding instead of just
Note:
See TracChangeset
for help on using the changeset viewer.