You can inspect a complex data structure in Perl 6 just by adding the.perl method:
say %h.perl
Perl 5 can't even speak Perl 5 like that without some help from a module:
use Data::Dumper; print Dumper (%h) ;
Pugs not only speaks Perl 6 easily, it's also fluent in Perl5 and YAML.
Easily integrating Perl 5's HTML::Template with CGI::App-p6
Today I rather painlessly integrated initial support for Perl 5's HTML::Template to the Perl 6 port of CGI::Application. Here's what the test looks like:
my $t = $app.load_tmpl('t/lib/templates/test.tmpl');
$t.param( ping => 'pong' ) ;
like( $t.output, rx/pong/, "basic load_tmpl functionality" );
There's nothing glaringly "Perl5ish" here to see. "$t" is a Perl5 object, but you tell that without peaking where you shouldn't. Later, the load_tmpl()
method could load a pure Perl 6 version of HTML::Template, and this code would still work.
The integration work itself was. It was basically this:
use perl5:HTML::Template;
return HTML::Template.new( filename => $file );
Now, Perl 5 embedding is not all roses. Because Perl 6 implements references differently, I couldn't figure out a way to pass a scalar reference from Perl 6 to Perl 5 that worked. It might be possible, but it's not as easy as the above example if it is!
Natively Read and Write YAML
Above I gave an example of how easy it is to dump out any data structure with pugs. YAML is spoken fluently as well, for both reading and writing. Dumping out YAML is just:
%h.yaml.say;
To read in YAML, you can use eval
, specifying that what you are eval'ing is a different language that should be translated to Perl 6 for. Here we read in YAML and then format it again with the .perl
method:
eval(q{
-
a: 1
b: 2
}, :lang<yaml>).perl.say;
The result:
[{("a" => "1"), ("b" => "2")},]
This all works now, with pugs.
That's very cool and useful. I look forward to actually using it.
Posted by: beppu | 2006.09.03 at 11:04 AM
I recommend you adding native JSON, too.
Posted by: Bártházi András | 2006.09.04 at 01:15 AM