Re: odd and even

2018-04-30 Thread Martin Barth

Are you aware of the %% operator?

$var %% 2 checks wether it is dividable by 2.


Am 30.04.2018 um 08:47 schrieb ToddAndMargo:

Hi All,

I know it would only take me 25 seconds to write one,
but do we have an odd and even function build in?


Many thanks,
-T

$ perl6 -e 'my $x=3; say $x.odd;'
No such method 'odd' for invocant of type 'Int'. Did you mean 'ord'?
  in block  at -e line 1

$ perl6 -e 'my $x=3; say $x.even;'
No such method 'even' for invocant of type 'Int'
  in block  at -e line 1


Re: Error Handling with IO::Socket.:Async::SLL

2017-10-16 Thread Martin Barth

I've changed the code to this, It's better but still not correct.

1) curl with http hangs and must be terminated with Ctrl+C

2) the code now restarts the server socket, which I also dont expect to 
happen.



use v6;
use IO::Socket::Async::SSL;

sub auto-restart(Supply $incoming) {
    supply {
    sub run() {
    whenever $incoming {
    QUIT {
    default {
    say "quit inside auto-restart";
    .note;
    run();
    }
    }
    }
    }
    run();
    }
}

my %ssl-config =
    certificate-file => 'cert.pem',
    private-key-file => 'key.pem';

my $listener = IO::Socket::Async::SSL.listen('localhost', 4433, 
|%ssl-config );

my $supply   = supply {
    whenever $listener -> $conn {
    handle($conn);
    }
}

sub handle($conn) {
    my $req = '';

    # QUIT { say "this is quit inside whenever"}
    whenever $conn {
    $req ~= $_;
    if $req.contains("\r\n\r\n") {
    say $req.lines[0];
    try await $conn.print(
    "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n" ~
    "Hello from a Perl 6 HTTP server\n");
    $conn.close;
    }
    }
};


auto-restart($supply).tap: -> {};
react {
    whenever signal(SIGINT) { done };
}


Am 16.10.2017 um 12:54 schrieb Timo Paulssen:

It took me a while, but I just found jnthn's automatic retry code for
supply-based things again.

It's in these slides
http://jnthn.net/papers/2017-perl6-concurrency-pcp.pdf on page 83

You'll want to use "supply" instead of "react" for your server and pass
that to the auto-retry function.


Error Handling with IO::Socket.:Async::SLL

2017-10-16 Thread Martin Barth

Hi there,

I need your help. I've been playing around with the code example from 
IO::Socket::Async::SSL and I am to stupid to get a propper error 
handling to work.



use v6;
use IO::Socket::Async::SSL;

react {
    my %ssl-config =
    certificate-file => 'cert.pem',
    private-key-file => 'key.pem';

    QUIT { say "this is quit outside whenever", $_.backtrace.full  }
    CATCH { say "this is catch outside whenever", $_.backtrace.full  }

    whenever IO::Socket::Async::SSL.listen('localhost', 4433, 
|%ssl-config) -> $conn {

    my $req = '';

    QUIT { say "this is quit inside whenever", $_.backtrace.full }
    CATCH { say "this is catch inside whenever", $_.backtrace.full }

    whenever $conn {
    $req ~= $_;
    if $req.contains("\r\n\r\n") {
    say $req.lines[0];
    try await $conn.print(
    "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n" ~
    "Hello from a Perl 6 HTTP server\n");
    $conn.close;
    }
    }
    }
}

what I am looking for is an example that dosen't shut down the server, 
or leaves the react block, for incomming http traffic or for ssl 
negotiation errors that might happen.



Thanks

Martin


Re: [perl #131707] [BUG] Private Methods/Attributes in roles do not work as expected

2017-07-10 Thread Martin Barth via RT
 > cat A.pm
class A { ... }

role R {
 has A $!private;

 method r {
 self!s;
 }

 method !s {
 $!private!s if $!private; # line 12
 say $.b;
 }

 method !private-method {
 say "priv method";
 }

 method public-method {
 self!private-method;
 }

 method set_private(A $a) {
 $!private = $a;
 }
}

class A does R {
 has Str $.b = 'secret';
};

 > cat B.pm
use v6;
use A;

class B does R {
 method b { self.r }
}


Am 06.07.2017 um 15:35 schrieb Zoffix Znet via RT:
> On Wed, 05 Jul 2017 09:09:48 -0700, mar...@senfdax.de wrote:
>> Hi there,
>>
>> this is crossposed in the perl6-users mailing list. Since noone stopped
>> me from assuming that this is a bug, i am going to open this now:
>>
>>
>> Assuming those two files A.pm and B.pm.
>>
>> The file A.pm contains a class A and a role R with a private-method and
>> a $!private member. (the files are in the end of the e-mail)
>>
>>
>> 1) I am wondering why a role can all its private methods:
>>
>>   > perl6 -I. -e 'use A; use B; my $b = B.new; $b.public-method()'
>> priv method
>>
>>
>> 2) but can't write its private members:
>>
>>   > perl6 -I. -e 'use A; use B; my $b = B.new; $b.set_private(A.new); $b.r'
>> No such private method '!!private' for invocant of type 'B'
>> in method set_private at /home/martin/.workspace/p6/realerror/A.pm
>> (A) line 24
>> in block  at -e line 1
>>
>> WHEN! the set_private looks like this:
>>
>>   method set_private(A $a) {
>>   self!private = $a;
>>   }
>>
>>
>> 3) It seems to work (well, it gets a error a bit later in $b.r):
>>
>>   > perl6 -I. -e 'use A; use B; my $b = B.new; $b.set_private(A.new); $b.r
>> '
>> P6opaque: no such attribute '$!private' in type B when trying to get a
>> value
>> in method s at /home/martin/.workspace/p6/realerror/A.pm (A) line 11
>> in method s at /home/martin/.workspace/p6/realerror/A.pm (A) line 11
>> in method r at /home/martin/.workspace/p6/realerror/A.pm (A) line 6
>> in block  at -e line 1
>>
>> WHEN set_private looks like this:
>>
>>   method set_private(A $a) {
>>   $!private = $a;
>>   }
>>
>> But method !s seems to be broken now.
>>
>>
>> I dont get why :( i would expect this to work. :-(
>>
>> In addition I think the error message is broken: "No such private method
>> '!!private' for invocant of type 'B'" it was differently in 2016.11
>>
>
> Hi, what's the full code in A.pm and B.pm files?
>


Re: [perl #131707] [BUG] Private Methods/Attributes in roles do not work as expected

2017-07-07 Thread Martin Barth

> cat A.pm
class A { ... }

role R {
has A $!private;

method r {
self!s;
}

method !s {
$!private!s if $!private; # line 12
say $.b;
}

method !private-method {
say "priv method";
}

method public-method {
self!private-method;
}

method set_private(A $a) {
$!private = $a;
}
}

class A does R {
has Str $.b = 'secret';
};

> cat B.pm
use v6;
use A;

class B does R {
method b { self.r }
}


Am 06.07.2017 um 15:35 schrieb Zoffix Znet via RT:

On Wed, 05 Jul 2017 09:09:48 -0700, mar...@senfdax.de wrote:

Hi there,

this is crossposed in the perl6-users mailing list. Since noone stopped
me from assuming that this is a bug, i am going to open this now:


Assuming those two files A.pm and B.pm.

The file A.pm contains a class A and a role R with a private-method and
a $!private member. (the files are in the end of the e-mail)


1) I am wondering why a role can all its private methods:

  > perl6 -I. -e 'use A; use B; my $b = B.new; $b.public-method()'
priv method


2) but can't write its private members:

  > perl6 -I. -e 'use A; use B; my $b = B.new; $b.set_private(A.new); $b.r'
No such private method '!!private' for invocant of type 'B'
in method set_private at /home/martin/.workspace/p6/realerror/A.pm
(A) line 24
in block  at -e line 1

WHEN! the set_private looks like this:

  method set_private(A $a) {
  self!private = $a;
  }


3) It seems to work (well, it gets a error a bit later in $b.r):

  > perl6 -I. -e 'use A; use B; my $b = B.new; $b.set_private(A.new); $b.r
'
P6opaque: no such attribute '$!private' in type B when trying to get a
value
in method s at /home/martin/.workspace/p6/realerror/A.pm (A) line 11
in method s at /home/martin/.workspace/p6/realerror/A.pm (A) line 11
in method r at /home/martin/.workspace/p6/realerror/A.pm (A) line 6
in block  at -e line 1

WHEN set_private looks like this:

  method set_private(A $a) {
  $!private = $a;
  }

But method !s seems to be broken now.


I dont get why :( i would expect this to work. :-(

In addition I think the error message is broken: "No such private method
'!!private' for invocant of type 'B'" it was differently in 2016.11



Hi, what's the full code in A.pm and B.pm files?



Re: Version of a Module

2017-07-04 Thread Martin Barth

Hi there,

is this a bug?

> perl6 -Ilib -e 'require Bailador; say Bailador.^ver'
No such method 'ver' for invocant of type 'Perl6::Metamodel::PackageHOW'
  in block  at -e line 1

vs

> perl6 -Ilib -e 'use Bailador; say Bailador.^ver'
v0.0.7

it seems that there a 2 different metaclasses being used:

> perl6 -Ilib -e 'use Bailador; say Bailador.HOW'
Perl6::Metamodel::ModuleHOW.new

> perl6 -Ilib -e 'require Bailador; say Bailador.HOW'
Perl6::Metamodel::PackageHOW.new


Am 28.06.2017 um 14:16 schrieb Simon Proctor:


See I'm using mi6 to generate my META6.json file from the report and 
it picks the version up from the module file. Other options like that 
seem sensible to me.


Simon


On Wed, 28 Jun 2017, 13:01 Martin Barth, <mar...@senfdax.de 
<mailto:mar...@senfdax.de>> wrote:


Hello,

but your approach means you have to state the version in the
META6.json AND in the Module.pm6 file again. This would be the
similar to having $VERSION in perl5. Shouldnt there be a simpler way?


Am 28.06.2017 um 08:45 schrieb Fernando Santagata:


Hi Martin,

This works for me:

File mytest.pm6

use v6;
unit module mytest:ver<0.0.1>;

sub myver is export
{
  mytest.^ver;
}

File mytest.p6

#!/usr/bin/env perl6
use lib '.';
use mytest;

say myver;

Console output:

$./mytest.p6
v0.0.1

I this what you meant?

On Tue, Jun 27, 2017 at 10:37 PM, Martin Barth <mar...@senfdax.de
<mailto:mar...@senfdax.de>> wrote:

Hello everyone,

I wanted to repeat the question that I asked today on #perl6.
I am looking for a way to retrieve the version of a
Perl6-Module from within the module itself.

 there is often a our $VERSION in perl5 modules. is
this still idiomatic/a good way to go in perl6
 i think the version is meant to be part of the
meta info and such?
 but that means the module itself does not know about
its version, just e.g. zef does?
 i'm convinced there ought to be a way to get the
full meta info from whatever installation your script was
grabbed from
 but i don't know what or how




-- 
Fernando Santagata






Private Methods in Roles

2017-07-03 Thread Martin Barth

Hi there,

Assuming those two files A.pm and B.pm.

The file A.pm contains a class A and a role R with a private-method and 
a $!private member. (the files are in the end of the e-mail)



1) I am wondering why a role can all its private methods:

> perl6 -I. -e 'use A; use B; my $b = B.new; $b.public-method()'
priv method


2) but can't write its private members:

> perl6 -I. -e 'use A; use B; my $b = B.new; $b.set_private(A.new); $b.r'
No such private method '!!private' for invocant of type 'B'
  in method set_private at /home/martin/.workspace/p6/realerror/A.pm 
(A) line 24

  in block  at -e line 1

WHEN! the set_private looks like this:

method set_private(A $a) {
self!private = $a;
}


3) It seems to work (well, it gets a error a bit later in $b.r):

> perl6 -I. -e 'use A; use B; my $b = B.new; $b.set_private(A.new); $b.r
'
P6opaque: no such attribute '$!private' in type B when trying to get a value
  in method s at /home/martin/.workspace/p6/realerror/A.pm (A) line 11
  in method s at /home/martin/.workspace/p6/realerror/A.pm (A) line 11
  in method r at /home/martin/.workspace/p6/realerror/A.pm (A) line 6
  in block  at -e line 1

WHEN set_private looks like this:

method set_private(A $a) {
$!private = $a;
}

But method !s seems to be broken now.


I dont get why :( i would expect this to work. :-(

In addition I think the error message is broken: "No such private method 
'!!private' for invocant of type 'B'" it was differently in 2016.11



Cheers

Martin


> cat A.pm
class A { ... }

role R {
has A $!private;

method r {
self!s;
}

method !s {
$!private!s if $!private; # line 12
say $.b;
}

method !private-method {
say "priv method";
}

method public-method {
self!private-method;
}

method set_private(A $a) {
$!private = $a;
}
}

class A does R {
has Str $.b = 'secret';
};

> cat B.pm
use v6;
use A;

class B does R {
method b { self.r }
}


Re: Version of a Module

2017-06-29 Thread Martin Barth

yap, when mi6 updates your META6.json this makes sense to me as well.

This leads me to another question

Can zef/perl6 handle differend modules version based on the suffix 
':ve<0.0.1>' or based on the META6.json. I mean, could there be 2 
scripts each using a different version of my module withouth having this 
suffix in it?



Am 28.06.2017 um 14:16 schrieb Simon Proctor:


See I'm using mi6 to generate my META6.json file from the report and 
it picks the version up from the module file. Other options like that 
seem sensible to me.


Simon


On Wed, 28 Jun 2017, 13:01 Martin Barth, <mar...@senfdax.de 
<mailto:mar...@senfdax.de>> wrote:


Hello,

but your approach means you have to state the version in the
META6.json AND in the Module.pm6 file again. This would be the
similar to having $VERSION in perl5. Shouldnt there be a simpler way?


Am 28.06.2017 um 08:45 schrieb Fernando Santagata:


Hi Martin,

This works for me:

File mytest.pm6

use v6;
unit module mytest:ver<0.0.1>;

sub myver is export
{
  mytest.^ver;
}

File mytest.p6

#!/usr/bin/env perl6
use lib '.';
use mytest;

say myver;

Console output:

$./mytest.p6
v0.0.1

I this what you meant?

On Tue, Jun 27, 2017 at 10:37 PM, Martin Barth <mar...@senfdax.de
<mailto:mar...@senfdax.de>> wrote:

Hello everyone,

I wanted to repeat the question that I asked today on #perl6.
I am looking for a way to retrieve the version of a
Perl6-Module from within the module itself.

 there is often a our $VERSION in perl5 modules. is
this still idiomatic/a good way to go in perl6
 i think the version is meant to be part of the
meta info and such?
 but that means the module itself does not know about
its version, just e.g. zef does?
 i'm convinced there ought to be a way to get the
full meta info from whatever installation your script was
grabbed from
 but i don't know what or how




-- 
Fernando Santagata






Re: Version of a Module

2017-06-28 Thread Martin Barth

Hello,

but your approach means you have to state the version in the META6.json 
AND in the Module.pm6 file again. This would be the similar to having 
$VERSION in perl5. Shouldnt there be a simpler way?



Am 28.06.2017 um 08:45 schrieb Fernando Santagata:


Hi Martin,

This works for me:

File mytest.pm6

use v6;
unit module mytest:ver<0.0.1>;

sub myver is export
{
  mytest.^ver;
}

File mytest.p6

#!/usr/bin/env perl6
use lib '.';
use mytest;

say myver;

Console output:

$./mytest.p6
v0.0.1

I this what you meant?

On Tue, Jun 27, 2017 at 10:37 PM, Martin Barth <mar...@senfdax.de 
<mailto:mar...@senfdax.de>> wrote:


Hello everyone,

I wanted to repeat the question that I asked today on #perl6.
I am looking for a way to retrieve the version of a Perl6-Module
from within the module itself.

 there is often a our $VERSION in perl5 modules. is this
still idiomatic/a good way to go in perl6
 i think the version is meant to be part of the meta
info and such?
 but that means the module itself does not know about its
version, just e.g. zef does?
 i'm convinced there ought to be a way to get the full
meta info from whatever installation your script was grabbed from
 but i don't know what or how




--
Fernando Santagata




Version of a Module

2017-06-27 Thread Martin Barth

Hello everyone,

I wanted to repeat the question that I asked today on #perl6.
I am looking for a way to retrieve the version of a Perl6-Module from 
within the module itself.


 there is often a our $VERSION in perl5 modules. is this still 
idiomatic/a good way to go in perl6
 i think the version is meant to be part of the meta info and 
such?
 but that means the module itself does not know about its 
version, just e.g. zef does?
 i'm convinced there ought to be a way to get the full meta 
info from whatever installation your script was grabbed from

 but i don't know what or how


Array containing only elements that implement a role.

2017-01-03 Thread Martin Barth

Hi There,

I am not sure if my RoleType @array; is correct, or if there is a better 
way to declare such an array. But I find the error that is happening 
when there are no elements in the array confusing on the first sight. It 
took me a while to realize that my array was empty and therefore it 
"means" the class/role itself.



> perl6 -e 'role Foo {method n{...}}; class Bar does Foo {method 
n{"n"}}; my Foo@a = (); @a.push: Bar.new; say @a[0].n'

n

> perl6 -e 'role Foo {method n{...}}; class Bar does Foo {method 
n{"n"}}; my Foo@a = (); say @a[0].n'

Method 'n' must be implemented by Foo because it is required by a role
  in any compose_method_table at gen/moar/m-Metamodel.nqp line 2824
  in any apply at gen/moar/m-Metamodel.nqp line 2834
  in any compose at gen/moar/m-Metamodel.nqp line 3006
  in any make_pun at gen/moar/m-Metamodel.nqp line 1692
  in any find_method at gen/moar/m-Metamodel.nqp line 1720
  in block  at -e line 1

> perl6 -e 'role Foo {method n{...}}; class Bar does Foo {method 
n{"n"}}; my @a = (); say @a[0].n'

No such method 'n' for invocant of type 'Any'
  in block  at -e line 1