« Pugscode.org services. | Main | Weekly Perl 6 mailing list summary for 21-28 January, 2007 »

2007.02.23

Various uses of embedded Perl 5.

For a long time now, Pugs embeds a Perl 5 runtime alongside its GHC runtime, providing seamless support for CPAN modules, including XS ones like DBI:

use perl5:DBI;
my $dbh = DBI.connect('dbi:SQLite:test.db');
$dbh.disconnect;

In addition to explicit use, there are several less well-known places where Pugs implicitly delegates to the Perl 5 runtime.

For example, today jisom++ asked on #perl6 about Pugs's counterpart to Perl 5's -T operator, i.e. testing if a file looks like plain text). Syntactically, filetest operators in Perl 6 are done via smartmatches against pair literals:

say "text file" if 'README'~~:T;
given 'README' { when :T { say "text file" } }

Semantically, because the heuristics for :T is rather hard to duplicate, we simply delegate to the Perl 5 runtime:

-- Pugs.Prim.FileTest:53
evalPerl5 ("sub { -" ++ testOp ++ " $_[0] }")

Another example is named Unicode charaters in double-quoted string literals:

say "\c[BLACK SMILING FACE]"; # ☻

Again, we simply delegate to charnames.pm in the Perl 5 runtime:

-- Pugs.Parser.Charnames:17
evalPerl5 ("use utf8; use charnames ':full'; ord(qq[\\N{"++name++"}])")

This is certainly a win -- the alternative way involves shipping the entire UnicodeData.txt with Pugs, which would be no fun at all.

The same principle applies to the crypt built-in function, which has this one-line implementation that simply calls the Perl 5 function with the same name:

-- Pugs.Prim:1083
op2 "crypt" = \x y -> opPerl5 "crypt" [x, y]

As moritz++ remarked on #perl6, such pass-through measures is a fine example of laziness, a virtue recognized by Perl folks and Haskell folks alike. Yay for laziness!

Comments

Post a comment