Skip to content

Instantly share code, notes, and snippets.

@ernix
Forked from dtonhofer/test_dumper.pl
Created January 19, 2019 15:02
Show Gist options
  • Select an option

  • Save ernix/1a68cbfdc148d5506fa0fbc8e84b729d to your computer and use it in GitHub Desktop.

Select an option

Save ernix/1a68cbfdc148d5506fa0fbc8e84b729d to your computer and use it in GitHub Desktop.
Comparing behaviour of Perl Data::Dumper when using "Pure Perl" and "XS" mode for non-iso-8859-1 codepoints
#!/usr/bin/perl
use strict;
use warnings;
use utf8; # Meaning "This lexical scope (i.e. file) contains utf8"
use open qw(:std :utf8);
use autodie;
use Encode;
use File::Temp qw(tempdir);
use File::Spec::Functions qw(catfile);
use Data::Dumper;
use Test::More;
# ---
# Just call dat main!
# ---
# ===
# 1) Define data
# 2) Create temporary directory
# 3) For Perl and XS implementation of Data::Dumper:
# 1) Dump data to file in said temporary directory
# 2) Read file back and eval it
# 3) Compare original data and data resulting from eval
# ===
our $DATA = {
EGKK => "London Gatwick",
BGAA => "Aasiaat (Egedesminde)",
BGSF => "Kangerlussuaq (Søndre Strømfjord)",
BGGH => "Nuuk (Godthåb)",
USHQ => "Белоя́рский",
USCC => "Челя́бинск",
TFFR => "Aéroport Guadeloupe - Pôle Caraïbes",
BKPR => "Aeroporti Ndërkombëtar i Prishtinës 'Adem Jashari'",
};
our $OUTDIR = tempdir(CLEANUP => 1);
note "Output goes to files in directory '$OUTDIR'\n";
is_deeply eval_var('xs'), eval_var('perl');
done_testing;
sub eval_var {
my ($impl) = @_;
my $fqf = catfile($OUTDIR, "$impl.dump");
{
open(my $fh, ">", $fqf);
if ($impl eq 'perl') {
print $fh Data::Dumper->new([$DATA])->Useperl(1)->Purity(1)
->Sortkeys(1)->Dump;
}
else {
print $fh Data::Dumper->new([$DATA])->Useperl(0)->Purity(1)
->Sortkeys(1)->Dump;
}
close $fh;
}
my $reco = slurp_and_eval($fqf, "data");
return $reco;
}
sub slurp_and_eval {
my ($filename, $name) = @_;
open my $fh, '<', $filename;
my $txt = do { local $/; <$fh> };
close $fh;
# Assume the text to eval assigns $VAR1
my $VAR1;
# Danger Will Robinson!! We are using EVAL, so the data better be gud!
# >>>
$VAR1 = eval $txt
or die "Error in eval of $name content from file '$filename': $@";
# <<<
my $len = scalar(keys %$VAR1);
note "Read '$name' content from file '$filename' ($len elements found in undumped hash)\n";
return $VAR1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment