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 ) ); }