Here's my current attempt to approach y'all's desires, without doing too much that I find offsensive :). I considered switching to backslash escaping, but that meant replacing the splits with more complex regexes, so discarded that approach. The nested hash stuff is an extra bonus and shows a benefit of using different delimiters between key and value than between different vars.

I tried to increase readability of the stored string (which seemed important to demerphq) by only escaping the necessary characters &, =, and %. I did not consider using unprintable characters and am a little uneasy about not escaping any there are in the data; I'd prefer it to be always all printable. This also meant bypassing calls to escape/unescape which would slow things down with a lot of vars.

cmpVars is what we need for current patch detection to work reliably; I haven't tested it at all yet.

sub packVars { my $varsref = $_[0]; # current format version: 01 return join "&", "==01", map { my $typ; my $v = $varsref->{$_}; # special data to pack? if (ref $v) { # only hash refs supported now $typ = "H"; $v = packVars( $v ); } # undef becomes empty, protect empty values elsif (!$v) { $v = $typ = ''; } join '=', map {s/([%&=])/ sprintf '%%%02x', ord($1) /ge; $_} $_, $v, (defined $typ ? $typ : ()); } sort keys %$varsref; } sub unpackVars { my $vars_str = $_[0]; my $format_version = "00"; # version 00: original format # version 01: keys are escaped, not just values $format_version = $1 if $vars_str =~ s/^==(\d\d)&//; return {} unless $vars_str; my %vars; if ($format_version eq "01") { for (split /&/, $vars_str) { my ($k,$v,$typ) = map { s/%(\w\w)/ chr(hex($1)) /ge; $_ } split /=/, $_, + -1; # special data to unpack? if ($typ) { # nested hash if ($typ eq "H") { $v = unpackVars($v); } } $vars{$k} = $v; } } # format version "00" else { %vars = map split(/=/, $_, 2), split /&/, $vars_str; unescape( values %vars ); $vars{$_} eq ' ' and $vars{$_} = '' for keys %vars; } return \%vars; } sub cmpVars { my ($var1str, $var2str) = @_; # return false immediately if strings match, # otherwise return true if both are current format version, # otherwise compare current format version. return $var1str cmp $var2str && ( $var1str =~ /^==01&/ && $var2str =~ /^==01&/ ) || packVars( unpackVars ( $var1str ) ) cmp packVars( unpackVars ( $var2str ) ); }

In reply to Re: patchable settings by ysth
in thread patchable settings by ysth

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.