Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trying to use Perl5 modules

11 views
Skip to first unread message

Richard Hainsworth

unread,
Sep 11, 2006, 12:52:27 AM9/11/06
to perl6...@perl.org
I am trying to find out how to use (in perl6) perl5 modules that contain
subroutines.

Here are two scripts from standard modules. Both work in perl5, but I
cant find a way to use them using pugs (I am using the Debian package
with version 6.2.10-4build1 on GNU/Linux/Ubuntu)

<start of perl5 script - straight out of the Time::gmtime documentation>
use strict;
use Time::gmtime;

my $gm = gmtime();
printf "The day in Greenwich is %s\n",
(qw(Sun Mon Tue Wed Thu Fri Sat Sun))[ $gm->wday() ];
<end of script>

<terminal output, under linux>

$ perl ./p5test.pl
Tue

<start of perl6 script>
use perl5:Time::gmtime;

my $gm = gmtime();
printf "The day in Greenwich is %s\n",
(qw(Sun Mon Tue Wed Thu Fri Sat Sun))[ $gm.wday() ];
# note the change from -> to .
<end of script>

<terminal output>

$ pugs ./p5test.p6
*** No compatible subroutine found: "&gmtime"
at ./p5test.p6 line 6, column 10-18

<second example>

<perl5 script>
use strict;
use Text::Balanced qw(extract_tagged);

my $txt = '<atag>now and then</atag>some text';

my @ret_pars = extract_tagged($txt);

print join("\n",@ret_pars),"\n";
print "$@\n" if $@;
<end script>

<terminal output>
$ perl ./p5test.pl
<atag>now and then</atag>
some text

<atag>
now and then
</atag>

<perl6 script>
use perl5:Text::Balanced qw(extract_tagged);

my $txt = '<atag>now and then</atag>some text';

my @ret_pars = extract_tagged($txt); # this is line 8

print join("\n",@ret_pars),"\n";
#print "$@\n" if $@;
# commented this out as caused a compile error, probably another
# variable should be used for errors.

<terminal output>
$ pugs ./p5test.p6
*** No compatible subroutine found: "&extract_tagged"
at ./p5test.p6 line 5, column 16-36
<terminal output end>

What is going wrong? What have I done wrong? I tried several
alternatives, like putting the & sigil on the function. I just got
different sorts of errors.

Richard


Trey Harris

unread,
Sep 11, 2006, 2:07:26 AM9/11/06
to Richard Hainsworth, perl6...@perl.org
In a message dated Mon, 11 Sep 2006, Richard Hainsworth writes:
> I am trying to find out how to use (in perl6) perl5 modules that contain
> subroutines.

Imports from Perl 5 modules don't currently work.

You can workaround this using .can, see below.

> use perl5:Time::gmtime;
>
> my $gm = gmtime();
> printf "The day in Greenwich is %s\n",
> (qw(Sun Mon Tue Wed Thu Fri Sat Sun))[ $gm.wday() ];
> # note the change from -> to .

use v6-alpha;
use perl5:Time::gmtime;
our &gmtime := Time::gmtime.can('gmtime');

my $gm = gmtime();
say "The day in Greenwich is {(<Sun Mon Tue
Wed Thu Fri Sat Sun>)[ $gm.wday ] }";

There is no printf. You can use a string closure as above, or
say/print with sprintf.

The same goes for your other example:

> use perl5:Text::Balanced qw(extract_tagged);
>
> my $txt = '<atag>now and then</atag>some text';
>
> my @ret_pars = extract_tagged($txt); # this is line 8
>
> print join("\n",@ret_pars),"\n";
> #print "$@\n" if $@;
> # commented this out as caused a compile error, probably another
> # variable should be used for errors.

use perl5:Text::Balanced;
our &extract_tagged := Text::Balanced.can('extract_tagged');

my $txt = '<atag>now and then</atag>some text';

my @ret_pars = extract_tagged($txt); # this is line 8

.say for @ret_pars;
say $! if $!;


$@ is no more, it's $! for all errors whatever their provender. In this
case there's no need for a join, but you could have written it:

say @ret_pars.join("\n");

if you liked.

All that said, there's a problem with Text::Balanced running under pugs;
@ret_pars is reversed from what it should be. I'm not sure what's going
on.

Trey

Richard Hainsworth

unread,
Sep 13, 2006, 11:46:32 AM9/13/06
to Trey Harris, perl6...@perl.org
Where is .can documented?

I saw .can in one of the examples in the pugs distribution, but I didnt
know where it came from, viz., was it related to perl6 or the module
that had been imported.

Not quite sure how the following two statements can be consistent:
'imports from Perl5 modules dont work' and 'there is a workaround with
.can'. From the code examples below, .can seems to be importing the
methods from the modules.

If .can is a universal workaround, then surely a pugs wrapper could be
written for any perl5 module along the lines

use v6-alpha;
use perl5:SomeModule::SomeSubModule;
our &aPublicSub := SomeModule::SomeSubModule.can('aPublicSub');

and so on for all the sub's in the module.

Regards,
Richard

Audrey Tang

unread,
Sep 13, 2006, 12:20:24 PM9/13/06
to Trey Harris, Richard Hainsworth, perl6...@perl.org

在 Sep 11, 2006 2:07 PM 時,Trey Harris 寫到:

> In a message dated Mon, 11 Sep 2006, Richard Hainsworth writes:
>> I am trying to find out how to use (in perl6) perl5 modules that
>> contain subroutines.
>
> Imports from Perl 5 modules don't currently work.

Actually, explicit imports do work (as of a couple weeks ago):

use perl5:Time::gmtime <gmtime>;
say gmtime.wday;

Implicit imports is not yet supported, though...

Audrey

Mark Stosberg

unread,
Sep 16, 2006, 10:57:19 PM9/16/06
to perl6...@perl.org

Thanks for the status update. I've reflected this on the related wiki page:
http://rakudo.org/perl6/index.cgi?using_perl_5_embedding

I welcome other tips about Perl 5 embedding to also appear there!

Mark

0 new messages