Skip to content

Latest commit

 

History

History
66 lines (49 loc) · 1.71 KB

File metadata and controls

66 lines (49 loc) · 1.71 KB

Useful One-Liners

YAML Module

Test YAML dump:

./jperl -MYAML -e 'print Dump({ hello => "world", numbers => [1,2,3] })'

Test YAML round-trip:

./jperl -MYAML -e 'my $data = { hello => "world", numbers => [1,2,3] }; print Dump(Load(Dump($data)))'

JSON Module

Test JSON encode:

./jperl -MJSON -e 'print encode_json({ hello => "world", numbers => [1,2,3] })'

Test JSON pretty print:

./jperl -MJSON -e 'my $json = JSON->new->pretty(1); print $json->encode({ hello => "world", numbers => [1,2,3] })'

File Operations

Write and read YAML file:

./jperl -MYAML -e 'DumpFile("test.yml", { hello => "world" }); print Dump(LoadFile("test.yml"))'

Write and read JSON file:

./jperl -MJSON -e 'use JSON; my $data = { hello => "world" }; open my $fh, ">", "test.json"; print $fh encode_json($data); close $fh; open my $in, "<", "test.json"; my $json = do { local $/; <$in> }; print encode_json(decode_json($json))'

Or more simply:

./jperl -MJSON -E 'my $data = { hello => "world" }; say encode_json($data)'

Benchmark

Benchmarking a Perl script to compare performance with a similar operation in PerlOnJava

time perl -MBenchmark -e 'timethis(200000, sub { my $sum = 0; $sum += $_ ** 2 for 1..1000 });'
timethis 200000:  6 wallclock secs ( 6.02 usr +  0.03 sys =  6.05 CPU) @ 33057.85/s (n=200000)

real	0m6.267s
user	0m6.077s
sys	0m0.051s

Benchmarking the same operation using PerlOnJava jperl

time ./jperl -MBenchmark -e 'timethis(200000, sub { my $sum = 0; $sum += $_ ** 2 for 1..1000 });'
timethis 200000:  1 wallclock secs ( 1,59 usr +  0,02 sys =  1,61 CPU) @ 124488,04/s (n=200000)

real	0m2.374s
user	0m2.791s
sys	0m0.200s