Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Strange YAML behavior with lists of qr regexp values

by naChoZ (Curate)
on Jan 28, 2013 at 17:42 UTC ( #1015742=perlquestion: print w/ replies, xml ) Need Help??
naChoZ has asked for the wisdom of the Perl Monks concerning the following question:

I'm just wondering if I discovered some kind of bug, or if I'm actually doing something wrong.

Here's a test script to illustrate.

#!/usr/bin/perl use strict; use warnings; use YAML::Any qw(DumpFile LoadFile); my $yaml_file = 'test-re.yaml'; my @default_patterns = ( qr/^foo.*bar.*baz/i, qr/^bar.*baz.*quux/i, qr/^quux$/, ); my $yaml = {}; if ( -w $yaml_file ) { $yaml = LoadFile( $yaml_file ); } if ( scalar @ARGV ) { push @{$yaml->{patterns}}, qr/$_/ for @ARGV; } else { # If no args, assume we want a fresh pattern dump, seed # with the default list of patterns. # $yaml->{patterns} = []; push @{$yaml->{patterns}}, $_ for @default_patterns; } DumpFile( $yaml_file, $yaml ) or die "Error writing yaml file: $!\n"; print "Wrote $yaml_file\n";

Here's the output from my shell as I use my test script.

$ ls test-re.yaml ls: cannot access test-re.yaml: No such file or directory $ test-yaml-re.pl Wrote test-re.yaml $ cat test-re.yaml --- patterns: - !!perl/regexp (?^i:^foo.*bar.*baz) - !!perl/regexp (?^i:^bar.*baz.*quux) - !!perl/regexp (?^:^quux$) $ test-yaml-re.pl '^foo$' Wrote test-re.yaml $ cat test-re.yaml --- patterns: - !!perl/regexp (?^:(?^i:^foo.*bar.*baz)) - !!perl/regexp (?^:(?^i:^bar.*baz.*quux)) - !!perl/regexp (?^:(?^:^quux$)) - !!perl/regexp (?^:^foo$) $ test-yaml-re.pl '^bar$' Wrote test-re.yaml $ cat test-re.yaml --- patterns: - !!perl/regexp (?^:(?^:(?^i:^foo.*bar.*baz))) - !!perl/regexp (?^:(?^:(?^i:^bar.*baz.*quux))) - !!perl/regexp (?^:(?^:(?^:^quux$))) - !!perl/regexp (?^:(?^:^foo$)) - !!perl/regexp (?^:^bar$) $ test-yaml-re.pl '^baz$' Wrote test-re.yaml $ cat test-re.yaml --- patterns: - !!perl/regexp (?^:(?^:(?^:(?^i:^foo.*bar.*baz)))) - !!perl/regexp (?^:(?^:(?^:(?^i:^bar.*baz.*quux)))) - !!perl/regexp (?^:(?^:(?^:(?^:^quux$)))) - !!perl/regexp (?^:(?^:(?^:^foo$))) - !!perl/regexp (?^:(?^:^bar$)) - !!perl/regexp (?^:^baz$)

So, every time I add a single qr quoted pattern to the yaml file, it's like it's nesting everything above it, even though it's not like I'm rerunning the qr quoting on every value in the list.

I discovered this while changing one of my scripts where I'm comparing a value to a list of patterns with the smart match operator: if ( $_ ~~ @patterns )

Also, I didn't think of it til just before I was going to submit this post, I added a line to the test script to display the result of YAML::Any->implementation and it showed YAML::Syck to be the selected version. The problem occurs on perl-5.14.2 with YAML::Syck 1.19 as well as perl-5.16.0 with YAML::Syck 1.20.

--
Andy

Comment on Strange YAML behavior with lists of qr regexp values
Select or Download Code
Re: Strange YAML behavior with lists of qr regexp values
by Anonymous Monk on Jan 28, 2013 at 18:33 UTC

    It doesn't seem to have anything to do with the adding of new patters.

    Instead, YAML::Syck's LoadFile() function seems to always add a level of brackets around all regexes it reads from a YAML file, even if they already have outer brackets.

    Example:

    test-re.pl:

    #!/usr/bin/perl use strict; use warnings; use YAML::Syck qw(LoadFile); use Data::Dumper; my $yaml = LoadFile( 'test-re.yaml' ); print Dumper($yaml->{patterns}), "\n";

    test-re.yaml:

    --- patterns: - !!perl/regexp (?^i:^foo.*bar.*baz) - !!perl/regexp ^quux$

    Output:

    $VAR1 = [ qr/(?^:(?^i:^foo.*bar.*baz))/, qr/(?^:^quux$)/ ];

    Output when changing use YAML::Syck to use YAML:

    $VAR1 = [ qr/(?^ui:^foo.*bar.*baz)/, qr/(?^u:^quux$)/ ];

    So yeah, seems to be a YAML::Syck particularity. Whether it is a bug is a matter of opinion I guess, since the regexes do behave the same so nothing should break.

      I notice that YAML::XS is similarly affected. Storing regular expressions themselves obviously must not be on too many radars. ;)

      --
      Andy

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-10-23 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls