Re: IO::Socket::INET timeout?

2024-01-21 Thread Paul Procacci
On Sat, Jan 20, 2024 at 5:02 PM William Michels 
wrote:

> Hi Paul,
>
> Did you get any resolution on this? I've only found these links:
>
> https://docs.raku.org/type/IO/Socket/INET
>
>
> https://stackoverflow.com/questions/72639883/how-to-deal-with-exceptions-in-iosocketinet
>
> I imagine a solution is possible using a Supply, but I haven't gotten
> there yet:
>
> https://docs.raku.org/type/Supply
>
> Best, Bill.
>
>
> On Jan 8, 2024, at 18:06, Paul Procacci  wrote:
>
> Hey all,
>
> What's the right way to specify a timeout for an IO::Socket::INET?
>
> What I have:
> $!socket := IO::Socket::INET.new(:host($!server), :port($!port));
>
> What I'd like to do which isn't possible:
> $!socket := IO::Socket::INET.new(:host($!server), :port($!port),
> :timeout($!timeout);
>
> Can your suggestion apply to reads/writes as well?
> To further this, I've reading on Promises and Supply's and have found
> suggestions that only Supply's can capture errors and whatnot, etc.
>
> I'm not sure what to make if anything I've read.  Does anyone have a clear
> example of connecting a socket, and reading/writing to that socket with
> timeouts?
>
> Thanks,
> Paul Procacci
>
>
> --
> __
>
> :(){ :|:& };:
>
>
>
Hey William,

I haven't made any progress.  I have enough things going on that I can
afford to put it aside for a while and then circle back to this in a bit.
If you find out, please let us know!  I too think Supply is the way forward
here.

~Paul
-- 
__

:(){ :|:& };:


IO::Socket::INET timeout?

2024-01-08 Thread Paul Procacci
 Hey all,

What's the right way to specify a timeout for an IO::Socket::INET?

What I have:
$!socket := IO::Socket::INET.new(:host($!server), :port($!port));

What I'd like to do which isn't possible:
$!socket := IO::Socket::INET.new(:host($!server), :port($!port),
:timeout($!timeout);

Can your suggestion apply to reads/writes as well?
To further this, I've reading on Promises and Supply's and have found
suggestions that only Supply's can capture errors and whatnot, etc.

I'm not sure what to make if anything I've read.  Does anyone have a clear
example of connecting a socket, and reading/writing to that socket with
timeouts?

Thanks,
Paul Procacci


-- 
__

:(){ :|:& };:


Re: Your idea on how to detect random directory names

2023-12-08 Thread Paul Procacci
On Sat, Dec 9, 2023 at 2:06 AM Bruce Gray 
wrote:

>
>
> > On Dec 9, 2023, at 00:37, ToddAndMargo via perl6-users <
> perl6-users@perl.org> wrote:
> >
> > Hi All,
> >
> > I am writing a clean up routine to relive my spot back ups
> > of junk in Blink browser directories that are meaningless to
> > a backup and that take up a lot of space.
> >
> > These directories are long and have random characters
> > for files names, such as "adcocjohghhfpidemphmcmlmhnfgikei"
> >
> > How who you go about figuring out who was random and
> > who was not.  I am thinking first the length and then
> > the absence of capitol letters and spaces and underscores.
> >
> > Your take?
>
>
> --snip--
>
> # Brave Browser temp directories: exactly 32 contiguous lowercase alpha
> characters.
> my $brave_junk_directories_re = / ^ <[a..z]> ** 32 $ /;
> my %to_skip = @filenames.grep($brave_junk_directories_re).Set;
>
>
I know this really wasn't mentioned clearly, but as a precaution you might
also want to consider filtering on whether the inode is referencing a file
or not:

my @to_skip = dir(".", test => { .IO.d && / ^ <[a..z]> ** 32 $ / } ) ;

If that possibility doesn't exist, then I personally would use:

my @to_skip = dir(".", test => { / ^ <[a..z]> ** 32 $ / } ) ;

As always, there's more than one way to skin a cat.

~Paul
-- 
__

:(){ :|:& };:


Re: Benchmark for Scala, Ruby and Perl

2022-01-15 Thread Paul Procacci
On Sat, Jan 15, 2022 at 5:03 AM Jon Smart  wrote:

>
> Thanks Paul. I am surprised that mmap has that huge IO advantages
> comparing to the classic way. So ruby take more benefit from this mmap
> calling. Just get learned from your case.
>
> Regards
>
>
It's not always beneficial.  There are cases where one is more beneficial
over the other.
. and unfortunately it's not really well defined when to use one over
the other.

The general consensus has been that if you plan on jumping around a lot in
a file, then mmap will be superior to read.
Otherwise when sequentially reading, then read will be superior to mmap.

The inclusion of my perl mmap version clearly disproves that ON MY OS.
It may be different ON YOUR OS.

This is partially why micro benchmarking is hard to do.  Your scripts will
almost certainly act one way on your machine, and different on others.
You may very well try an mmap version on your machine and scratch your head
because it's slower  there's more than just the userland processes
involved here.  ;)

Goodnight (again).


Re: Benchmark for Scala, Ruby and Perl

2022-01-15 Thread Paul Procacci
Sorry, it's 5:00am here and needless to say it's wy past my bedtime and
I'm making mistakes.
The comparison should have been between both ruby versions  ugh.

I'll let you play though.  Have a great night.

On Sat, Jan 15, 2022 at 4:57 AM Paul Procacci  wrote:

> Hey John,
>
> One more follow up and then it's bedtime for me.  I wanted to further this
> discussion just a little bit more by implementing the mmap solution that I
> applied to perl to ruby instead.  Now all of a sudden, ruby is much much
> faster.  My ruby source code follows:
>
> Goodnight!
>
> # ruby -W0 ./doit.rb | md5
> 786be54356a5832dcd1148c18de71fc8
> # perl ./doit2.pl | md5
> 786be54356a5832dcd1148c18de71fc8
>
>
> # truss -c ruby -W0 ./doit.rb
> 
>   - --- ---
> 0.0141115021855 260
>
> # truss -c perl ./doit2.pl
> 
>   - --- ---
> 0.049820267 777  52
>
>
>
> -
> require 'mmap';
>
> stopwords = {}
> mmap_s = Mmap.new('stopwords.txt')
> mmap_s.advise(Mmap::MADV_SEQUENTIAL)
> mmap_s.each_line do |s|
>   s.strip!
>   stopwords[s] =1
> end
>
> count = {}
> mmap_c = Mmap.new('words.txt')
> mmap_c.advise(Mmap::MADV_SEQUENTIAL)
> mmap_c.each_line do |s|
>   s.strip!
>   if ! stopwords.has_key?(s)
> if count.has_key?(s)
>count[s] += 1
> else
>count[s] = 1
> end
>   end
> end
>
> z = count.sort {|a1,a2| a2[1]<=>a1[1]}
> z.take(20).each do |s| puts "#{s[0]} -> #{s[1]}" end
>
> On Sat, Jan 15, 2022 at 3:48 AM Paul Procacci  wrote:
>
>> Hey John,
>>
>> On Sat, Jan 15, 2022 at 3:04 AM Jon Smart  wrote:
>>
>>>
>>> Hello Paul
>>>
>>> Do you mean by undef $/ and with <$fh> we can read the file into memory
>>> at one time?
>>>
>>
>> In most cases the short answer is yes.
>> I have problems with your wording however given the 'geek' that I am.
>> 'At one time'  not quite.  In your example there were over 4000 read(2)
>> syscalls by the operating system for instance.  This wouldn't have been 'at
>> one time'.  ;)
>>
>>
>> Yes that would be faster b/c we don't need to read file by each line,
>>> which increases the disk IO.
>>>
>>>
>> It actually doesn't make it faster.
>> Perl buffers it's reads as does all modern programming languages.  If you
>> ask perl to give you 10 bytes it certainly will, but what you don't know is
>> that perl has really read up to 8192 bytes.  It only gave you what you
>> asked for and the rest is sitting in perl buffers.
>> To put this another way, you can put 8192 newline characters in a file
>> and read this file line by line.  This doesn't equate to 8192 separate
>> read(2) syscalls ... it's just 1 read syscall.  It won't be faster nor
>> slower.
>>
>>
>>
>>> Another questions:
>>> 1. what's the "truss" command?
>>>
>>
>> truss is akin to strace.  If you're on linux, you can install strace and
>> get the samish type of utility.
>> It allows you to trace system calls and see how much of your time for a
>> given program is waiting on the kernel and/or how often it's asking the
>> kernel to do something.
>>
>> 2. what's the syntax "<:mmap"?
>>>
>>> mmap is a method of mapping a file (among other things) into memory on
>> an on-demand basis.
>> Given the example you provided, this is actually where the speed up comes
>> from.  This is because my version removes the 4000+ read(2) syscalls in
>> favor of just 2 mmap(2) syscalls.
>>
>> Thank you.
>>
>>
>> ~Paul
>>
>
>
> --
> __
>
> :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Benchmark for Scala, Ruby and Perl

2022-01-15 Thread Paul Procacci
Hey John,

One more follow up and then it's bedtime for me.  I wanted to further this
discussion just a little bit more by implementing the mmap solution that I
applied to perl to ruby instead.  Now all of a sudden, ruby is much much
faster.  My ruby source code follows:

Goodnight!

# ruby -W0 ./doit.rb | md5
786be54356a5832dcd1148c18de71fc8
# perl ./doit2.pl | md5
786be54356a5832dcd1148c18de71fc8


# truss -c ruby -W0 ./doit.rb

  - --- ---
0.0141115021855 260

# truss -c perl ./doit2.pl

  - --- ---
0.049820267 777  52



-
require 'mmap';

stopwords = {}
mmap_s = Mmap.new('stopwords.txt')
mmap_s.advise(Mmap::MADV_SEQUENTIAL)
mmap_s.each_line do |s|
  s.strip!
  stopwords[s] =1
end

count = {}
mmap_c = Mmap.new('words.txt')
mmap_c.advise(Mmap::MADV_SEQUENTIAL)
mmap_c.each_line do |s|
  s.strip!
  if ! stopwords.has_key?(s)
if count.has_key?(s)
   count[s] += 1
else
   count[s] = 1
end
  end
end

z = count.sort {|a1,a2| a2[1]<=>a1[1]}
z.take(20).each do |s| puts "#{s[0]} -> #{s[1]}" end

On Sat, Jan 15, 2022 at 3:48 AM Paul Procacci  wrote:

> Hey John,
>
> On Sat, Jan 15, 2022 at 3:04 AM Jon Smart  wrote:
>
>>
>> Hello Paul
>>
>> Do you mean by undef $/ and with <$fh> we can read the file into memory
>> at one time?
>>
>
> In most cases the short answer is yes.
> I have problems with your wording however given the 'geek' that I am.  'At
> one time'  not quite.  In your example there were over 4000 read(2)
> syscalls by the operating system for instance.  This wouldn't have been 'at
> one time'.  ;)
>
>
> Yes that would be faster b/c we don't need to read file by each line,
>> which increases the disk IO.
>>
>>
> It actually doesn't make it faster.
> Perl buffers it's reads as does all modern programming languages.  If you
> ask perl to give you 10 bytes it certainly will, but what you don't know is
> that perl has really read up to 8192 bytes.  It only gave you what you
> asked for and the rest is sitting in perl buffers.
> To put this another way, you can put 8192 newline characters in a file and
> read this file line by line.  This doesn't equate to 8192 separate read(2)
> syscalls ... it's just 1 read syscall.  It won't be faster nor slower.
>
>
>
>> Another questions:
>> 1. what's the "truss" command?
>>
>
> truss is akin to strace.  If you're on linux, you can install strace and
> get the samish type of utility.
> It allows you to trace system calls and see how much of your time for a
> given program is waiting on the kernel and/or how often it's asking the
> kernel to do something.
>
> 2. what's the syntax "<:mmap"?
>>
>> mmap is a method of mapping a file (among other things) into memory on an
> on-demand basis.
> Given the example you provided, this is actually where the speed up comes
> from.  This is because my version removes the 4000+ read(2) syscalls in
> favor of just 2 mmap(2) syscalls.
>
> Thank you.
>
>
> ~Paul
>


-- 
__

:(){ :|:& };:


Re: Benchmark for Scala, Ruby and Perl

2022-01-15 Thread Paul Procacci
Hey John,

On Sat, Jan 15, 2022 at 3:04 AM Jon Smart  wrote:

>
> Hello Paul
>
> Do you mean by undef $/ and with <$fh> we can read the file into memory
> at one time?
>

In most cases the short answer is yes.
I have problems with your wording however given the 'geek' that I am.  'At
one time'  not quite.  In your example there were over 4000 read(2)
syscalls by the operating system for instance.  This wouldn't have been 'at
one time'.  ;)


Yes that would be faster b/c we don't need to read file by each line,
> which increases the disk IO.
>
>
It actually doesn't make it faster.
Perl buffers it's reads as does all modern programming languages.  If you
ask perl to give you 10 bytes it certainly will, but what you don't know is
that perl has really read up to 8192 bytes.  It only gave you what you
asked for and the rest is sitting in perl buffers.
To put this another way, you can put 8192 newline characters in a file and
read this file line by line.  This doesn't equate to 8192 separate read(2)
syscalls ... it's just 1 read syscall.  It won't be faster nor slower.



> Another questions:
> 1. what's the "truss" command?
>

truss is akin to strace.  If you're on linux, you can install strace and
get the samish type of utility.
It allows you to trace system calls and see how much of your time for a
given program is waiting on the kernel and/or how often it's asking the
kernel to do something.

2. what's the syntax "<:mmap"?
>
> mmap is a method of mapping a file (among other things) into memory on an
on-demand basis.
Given the example you provided, this is actually where the speed up comes
from.  This is because my version removes the 4000+ read(2) syscalls in
favor of just 2 mmap(2) syscalls.

Thank you.


~Paul


Re: Benchmark for Scala, Ruby and Perl

2022-01-14 Thread Paul Procacci
Hey Jon,

The most glaringly obvious thing I could recommend is that at least in your
perl routine (and probably the other languages) most of your time is
context switching reading from the disk.
Now, my perl version is indeed faster, but one has to ask themselves, was
.015193256 seconds really worth the effort?  /shrug   -- If this is for a
financial industry perhaps, but then they'd just have written it in C.
Otherwise, probably not.
Also note, there's other ways to speed this up even further, but at that
point it isn't really worth the time.  We're talking a couple of
microseconds at best.  I've included my version for your reference.

Before closing, I happen to like micro benchmarks whether or not you think
'I know this benchmark is maybe meaningless' as your site says.
If anything, it can absolutely be useful.  I personally think sometimes
they are and others not so much.  Just depends on the context.

Your perl source (doit) .. my perl source (doit2):
# ./doit2.pl | md5
786be54356a5832dcd1148c18de71fc8
root@nas:~ # ./doit.pl | md5
786be54356a5832dcd1148c18de71fc8

# truss -c ./doit.pl

syscall seconds   calls  errors
read0.0368288134140   0

  - --- ---
0.0378218215227 284



# truss -c ./doit2.pl

syscall seconds   calls  errors
read0.000245121  19   0

  - --- ---
0.022628565 804  59


-
use strict;

$/ = undef;
my %stopwords = do {
open my $fh, '<:mmap', 'stopwords.txt' or die $!;
map { $_ => 1; } split /\n/, <$fh>;
};

my %count = do {
my %res;
open my $fh, '<:mmap', 'words.txt' or die $!;
map { $res{$_}++ unless $stopwords{$_}; } split /\n/, <$fh>;
%res;
};

my $i=0;
for (sort {$count{$b} <=> $count{$a}} keys %count) {
if ($i < 20) {
print "$_ -> $count{$_}\n"
} else {
   last;
}
$i ++;
}

On Sat, Jan 15, 2022 at 12:37 AM Jon Smart  wrote:

> Hello,
>
> May I show the result of my benchmark for perl5, ruby, and scala?
> https://blog.cloudcache.net/benchmark-for-scala-ruby-and-perl/
>
> Welcome you to give any suggestion to me for improving this.
>
> Thanks.
>


-- 
__

:(){ :|:& };:


Re: Grammar Help

2021-12-29 Thread Paul Procacci
 Ralph,

So Ijust tried comma w/ grammar live view because as I expand the logic to
encompass other things it became further and further unwieldy ... and I
must say, I'm blown away.  This really should be in the docs (it may be,
but I didn't go looking for it).

It's really awesome to say the least.  Thanks for pointing it out.

Thanks,
Paul

On Sun, Dec 26, 2021 at 5:37 PM Ralph Mellor 
wrote:

> On Sun, Dec 26, 2021 at 6:01 AM Paul Procacci  wrote:
> >
> > Hope everyone had a Merry Christmas and takes likings to corny opening
> statements.  ;)
>
> I love me some corn but it's especially appropriate to share some
> in winter if you've got hungry mice. :)
>
> As others have noted, you need `%%` instead of `%`.
>
> Grammar::Tracer isn't always intuitive. Have you tried CommaIDE's
> Grammar Live View? https://commaide.com/docs/grammar-live-view
>
> I wonder if you would have immediately known what the problem was
> with the latter or if the problem was just that you were thinking `%` did
> what `%%` does?
>
> --
> love, raiph
>


-- 
__

:(){ :|:& };:


Re: Grammar Help

2021-12-26 Thread Paul Procacci
Hey all,

Firstly, I want to thank everyone for all their responses.  It helped
greatly.

I wanted to share what I ended up with that seems to be working. (below)
I'm open to suggestions on how to improve this, tidy things up a bit, etc.

Running on the below grammar yields the following what to me seems perfect:

% raku ./test2.raku < test.data
{objectKey => {a => 1234, anotherObjectKey => {b => "45934"}, b => 5345,
newobjectKey => {a => 1534, b => "asdf"}}}

Thanks all for taking the time to respond.
~Paul


 data 
objectKey:
{
a = 1234;
b = 5345;

newobjectKey:
{
a = 1534;
b = "asdf";
}

anotherObjectKey:
{
b = "45934";
}
}
 end data 


- logic -
grammar Test {

rule TOP{  }
rule object {   }
rule objectKey  {  ':'   }
rule objectBody { '{' ~ '}' +}
rule pair   {  '='  ';'   }
token cstr  { + }
token number{ <[0..9]>+}
token string{ '"' ~ '"' <-["]>+}

proto token item{ * };
  token item:sym{  }
  token item:sym  {  }

proto token value   { * };
  token value:sym   {  }
  token value:sym   {  }
}

class TestActions {

method TOP($/) { make $.made; }
method object($/) { make $.made => $.made; }
method objectBody($/) { make $>>.made.hash.item; }
method pair($/) { make $.made => $.made; }
method objectKey($/) { make $.made; }
method cstr($/)  { make ~$/; }
method string($/){ make ~$/; }
method number($/){ make ~$/; }

method item:sym($/){ make $.made; }
method item:sym($/)  { make $.made; }
method value:sym($/) { make ~$/; }
method value:sym($/) { make ~$/; }
}
- end logic -

On Sun, Dec 26, 2021 at 1:01 AM Paul Procacci  wrote:

> Hey all,
>
> Twas the night of Christmas, when all through the house, not a creature
> was stirring except Paul w/ his mouse.
>
> Hope everyone had a Merry Christmas and takes likings to corny opening
> statements.  ;)
>
> I was writing a little something tonight using Grammars and ran into
> something that I can't seem to wrap my head around.  I'm hoping someone
> could explain in detail.
>
> Given the following data:
>  data -
> objectKey:
> {
> a = "bi";
> b = "hi";
> }
>  end data -
>
>
>  and the following logic partially taken from JSON::Tiny:
>
>  code 
> grammar myTest {
> token TOP{ \s*  \s* }
> rule  object {  '{'  '}' }
> # rule  object {  '{' ~ '}'  }
> rule  objectKey  {  ':' }
> rule  pairlist   {  * % \; }
> rule  pair   {  '='  }
> token cstr   { + }
> token value  { '"' ~ '"' * }
> }
>
> class myTestActions {
> method TOP($/) {
> make $.made.hash.item;
> }
>
> method object($/) {
> say 'hello';
> }
>
> method objectKey($/) {
> make $.made;
> }l
> method pairlist($/) {
> make $>>.made.flat;
> }
>
> method pair($/) {
> make $.made => $.made;
> }
>
> method cstr($/)  { make ~$/ }
> method value($/) { make ~$/ }
> }
>  code 
>
>
> ... it'd be my hopes that this would match.  However, It's not matching on
> 'object' and I can't seem to figure out why.
>
> Adding Grammar::Tracer yields the following:
>
> TOP
> |  object
> |  |  objectKey
> |  |  |  cstr
> |  |  |  * MATCH "objectKey"
> |  |  * MATCH "objectKey:\n"
> |  |  pairlist
> |  |  |  pair
> |  |  |  |  cstr
> |  |  |  |  * MATCH "a"
> |  |  |  |  value
> |  |  |  |  * MATCH "\"bi\""
> |  |  |  * MATCH "a = \"bi\""
> |  |  |  pair
> |  |  |  |  cstr
> |  |  |  |  * MATCH "b"
> |  |  |  |  value
> |  |  |  |  * MATCH "\"hi\""
> |  |  |  * MATCH "b = \"hi\""
> |  |  |  pair
> |  |  |  |  cstr
> |  |  |  |  * FAIL
> |  |  |  * FAIL
> |  |  * MATCH "a = \"bi\";\n\tb = \"hi\""
> |  * FAIL
> * FAIL
>
> What exactly am I doing wrong?  Does '{' ~ '}' not work as I expect here?
> Appreciate any insight.
>
> Thanks,
> Paul
> --
> __
>
> :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Grammar Help

2021-12-26 Thread Paul Procacci
Hey Ralph,

I don't use Comma.  I'm not a fan of IDE's and generally stick to vi for
all my needs.  Old habits are hard to break especially once you've been
using the same tool for ~25 years like I have.  ;(
I've been switching back and forth between:

use Grammar::Tracer;

and

use Grammar::Tracer::Compact;

Both I've found helpful, though obviously not fool proof.  It'd be nice if
it show'd why it failed (i.e. what it encountered vs what it expected )
rather than just a 'failed'.
The difference was partly my fault for not even considering the differences
between '%' and '%%'.  It was a fork lift from JSON::Tiny and it's one
change I overlooked entirely.

As an aside, I'm coming back to raku after a year hiatus in hopes that the
support for FreeBSD has improved and to some degree, have to reteach myself
the little tidbits I've forgotten.
Ultimately, I'm looking to parse the following using Grammars:

 data -
objectKey:
{
a = "bi";
b = "hi";
newObjectKey:
{
 a = "go";
}
   anotherObjectKey:
   {
a = 'ho';
   }
  c = "no";
}


... and naturally with a Match object that represents this as a
hierarchical hash structure containing all the elements.
The differences between this and JSON are close enough that I thought a
mere copy/paste from JSON::Tiny w/ just enough changes to account for the
differences would be enough, but they are in fact different enough that
this was an oversight.

At least I learned a bit in the process.

Thanks,
Paul

On Sun, Dec 26, 2021 at 5:37 PM Ralph Mellor 
wrote:

> On Sun, Dec 26, 2021 at 6:01 AM Paul Procacci  wrote:
> >
> > Hope everyone had a Merry Christmas and takes likings to corny opening
> statements.  ;)
>
> I love me some corn but it's especially appropriate to share some
> in winter if you've got hungry mice. :)
>
> As others have noted, you need `%%` instead of `%`.
>
> Grammar::Tracer isn't always intuitive. Have you tried CommaIDE's
> Grammar Live View? https://commaide.com/docs/grammar-live-view
>
> I wonder if you would have immediately known what the problem was
> with the latter or if the problem was just that you were thinking `%` did
> what `%%` does?
>
> --
> love, raiph
>


-- 
__

:(){ :|:& };:


Grammar Help

2021-12-25 Thread Paul Procacci
Hey all,

Twas the night of Christmas, when all through the house, not a creature was
stirring except Paul w/ his mouse.

Hope everyone had a Merry Christmas and takes likings to corny opening
statements.  ;)

I was writing a little something tonight using Grammars and ran into
something that I can't seem to wrap my head around.  I'm hoping someone
could explain in detail.

Given the following data:
 data -
objectKey:
{
a = "bi";
b = "hi";
}
 end data -


 and the following logic partially taken from JSON::Tiny:

 code 
grammar myTest {
token TOP{ \s*  \s* }
rule  object {  '{'  '}' }
# rule  object {  '{' ~ '}'  }
rule  objectKey  {  ':' }
rule  pairlist   {  * % \; }
rule  pair   {  '='  }
token cstr   { + }
token value  { '"' ~ '"' * }
}

class myTestActions {
method TOP($/) {
make $.made.hash.item;
}

method object($/) {
say 'hello';
}

method objectKey($/) {
make $.made;
}l
method pairlist($/) {
make $>>.made.flat;
}

method pair($/) {
make $.made => $.made;
}

method cstr($/)  { make ~$/ }
method value($/) { make ~$/ }
}
 code 


... it'd be my hopes that this would match.  However, It's not matching on
'object' and I can't seem to figure out why.

Adding Grammar::Tracer yields the following:

TOP
|  object
|  |  objectKey
|  |  |  cstr
|  |  |  * MATCH "objectKey"
|  |  * MATCH "objectKey:\n"
|  |  pairlist
|  |  |  pair
|  |  |  |  cstr
|  |  |  |  * MATCH "a"
|  |  |  |  value
|  |  |  |  * MATCH "\"bi\""
|  |  |  * MATCH "a = \"bi\""
|  |  |  pair
|  |  |  |  cstr
|  |  |  |  * MATCH "b"
|  |  |  |  value
|  |  |  |  * MATCH "\"hi\""
|  |  |  * MATCH "b = \"hi\""
|  |  |  pair
|  |  |  |  cstr
|  |  |  |  * FAIL
|  |  |  * FAIL
|  |  * MATCH "a = \"bi\";\n\tb = \"hi\""
|  * FAIL
* FAIL

What exactly am I doing wrong?  Does '{' ~ '}' not work as I expect here?
Appreciate any insight.

Thanks,
Paul
-- 
__

:(){ :|:& };:


Re: why not raku ?

2021-11-19 Thread Paul Procacci
Raku is pretty amazing.  I too would use it pretty regularly except it
doesn't run on Freebsd properly.  Many a times I started a project that
would have been a great contribution yet always ran into problems and had
to change back to Perl.

It's definitely a good language. It's just not suited for production.

On Fri, Nov 19, 2021, 4:44 AM Marc Chantreux  wrote:

> hello,
>
> > I like ruby and perl
>
> so do I but raku is by far my prefered interpreted langage now.
> I don't raku that much and most of the time, i read the doc more than i
> actually write code but when it's writen, it's always elegant and
> concise the way i never seen before.
>
> > Maybe perl6 is still not production-ready?
>
> Perl6 is now raku.
>
> it depends: what do you mean by "production" and "ready"? start with
> some few non-critical usecases and you'll see raku is production ready
> enough for lot of things.
>
> > but why so few open source projects which were developed by perl6?
>
> wow ... interesting question. my cents on it:
>
> * raku shines on interpreted langages when people are moving to compiled
> langages
> * raku is that rich it's hard to get it in a first view
> * raku is still way too slow to be taken seriously by a large audience
> * js or python developpers are legions on the market now so everyone
>   choose this as an argument
> * we need more packages on raku.land
> * i really think technologies are massively adopted when they are
>   packaged in main linux distros because lot of people don't want to
>   bother compiling an interpreter or adding extra repos to do it.
>
> regards,
> marc
>


Re: Buf to Str

2021-06-09 Thread Paul Procacci
>> That C null is an int pointer, longer than a single byte.

Yep, no arguments there.  ;)

On Wed, Jun 9, 2021 at 11:06 AM yary  wrote:

> That C null is an int pointer, longer than a single byte.
>
> On Wed, Jun 9, 2021 at 11:04 AM Paul Procacci  wrote:
>
>> Not sure about the 80's, my programming endeavors started in the 90's.
>> NUL doesn't exist in the C standard so I have no comment on it.
>> The C standard defines that 0 cast to the type void * is both a null
>> pointer and a null pointer constant.
>> I always have and most likely will continue using 0 over NULL rightly or
>> otherwise; though tend to use either/or when describing something ... as
>> Elizabeth pointed out.
>>
>> Potato vs Potatoe I guess.
>> ~Paul
>>
>> On Wed, Jun 9, 2021 at 10:20 AM yary  wrote:
>>
>>> From my early 1980s days learning programming, ASCII CHR 0 is not null,
>>> it is NUL (can type it as ctrl-@) it's a control character much like
>>> the others.
>>>
>>> Ctrl-G BEL, it was fun putting you in file names...
>>>
>>> On Wed, Jun 9, 2021, 9:56 AM Paul Procacci  wrote:
>>>
>>>> >> But yeah, the Str class in Raku is much more than a C-string.
>>>>
>>>> Got it.  Thanks Elizabeth.
>>>>
>>>> On Wed, Jun 9, 2021 at 6:45 AM Elizabeth Mattijsen 
>>>> wrote:
>>>>
>>>>> > On 9 Jun 2021, at 06:34, Paul Procacci  wrote:
>>>>> >
>>>>> > Hopefully a pretty quick question
>>>>> >
>>>>> > GIven the following:
>>>>> >
>>>>> > my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]);
>>>>> > say $b.decode;
>>>>> >
>>>>> > I would expect this to print 'Hi'.
>>>>> > Instead it prints 'Hi again'.
>>>>> >
>>>>> > https://docs.raku.org/type/Buf#(Blob)_method_decode
>>>>> >
>>>>> > The decode documentation for Buf only states that 'Applies an
>>>>> encoding to turn the blob into a Str; the encoding will be UTF-8 by
>>>>> default.'
>>>>>
>>>>> >
>>>>> > The zero (0) in that Buf should imply an end of string yet decode
>>>>> seems to want to decode the number of elements instead.
>>>>>
>>>>> That is an incorrect assumption carried over from C.  In the Raku
>>>>> Programming Language, a null byte is a valid grapheme, as it is in
>>>>> unicode.  A small change to your program:
>>>>>
>>>>> my Buf $b .= new(72, 105, 0, 32, 97, 103, 97, 105, 110, 0);
>>>>> .say for $b.decode.uninames;
>>>>> #
>>>>> LATIN CAPITAL LETTER H
>>>>> LATIN SMALL LETTER I
>>>>> 
>>>>> SPACE
>>>>> LATIN SMALL LETTER A
>>>>> LATIN SMALL LETTER G
>>>>> LATIN SMALL LETTER A
>>>>> LATIN SMALL LETTER I
>>>>> LATIN SMALL LETTER N
>>>>> 
>>>>>
>>>>>
>>>>> > Furthermore, If I 'say $b.decode.chars;' it counts the initial null
>>>>> as part of Str.
>>>>> > In my mind, that means Str doesn't really mean string.
>>>>>
>>>>> I don't see an initial null in your example.
>>>>>
>>>>> But yeah, the Str class in Raku is much more than a C-string.
>>>>>
>>>>>
>>>>> > So the question, how does one ACTUALLY decode what's in a buffer to
>>>>> a string where it adheres to the semantics of NULL termination for strings
>>>>> cleanly.
>>>>>
>>>>> If you want to include the null byte in your final strings:
>>>>>
>>>>> my @strings = $b.decode.comb(/ .*? "\0" /)
>>>>>
>>>>> would be a way.
>>>>>
>>>>>
>>>>>
>>>>> > Another question might be, should decode() follow null terminating
>>>>> semantics instead of number of elements in a given Buf.
>>>>>
>>>>> No.  The C-string semantics are what they are.  They are not the
>>>>> semantics used in the Raku Programming Language.
>>>>>
>>>>>
>>>>>
>>>>> Liz
>>>>
>>>>
>>>>
>>>> --
>>>> __
>>>>
>>>> :(){ :|:& };:
>>>>
>>>
>>
>> --
>> __
>>
>> :(){ :|:& };:
>>
> --
> -y
>


-- 
__

:(){ :|:& };:


Re: Buf to Str

2021-06-09 Thread Paul Procacci
That does help some Daniel.
I do in fact need to work with C style null terminated strings because when
passing a structure to an underlying OS via nativecall, you have no control
over what the underlying libraries want.
An Example:

class myStruct is repr('CStruct')
{
  HAS int8 @.Path[MAX_PATH] is CArray;

  method path
  {
blob-from-pointer(Pointer.new(nativecast(Pointer, self)+4),
:elems(MAX_PATH));
  }
}

sub Fill(myStruct) is native('Kernel32') { * };

my myStruct $struct .= new;

Fill($struct);

say $struct.path;

As you can imagine, @.PATH has a static buffer that is MAX_PATH
length, and the strings stored there are <= MAX_PATH in length.

The only way to get them is to look for that NULL.


On Wed, Jun 9, 2021 at 10:46 AM Daniel Sockwell 
wrote:

> Hi Paul,
>
> If you _do_ want/need to work with C-style null-terminated strings, you
> can use the (core)
> NativeCall library.  So, given your example:
>
> > my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]);
> > say $b.decode;
> > I would expect this to print 'Hi'.
> >
> > Instead it prints 'Hi again'.
>
> You can write:
>
>use NativeCall;
>say nativecast(str, $b) # prints 'Hi'
>
> Hope that helps!
>
> – codesections
>


-- 
__

:(){ :|:& };:


Re: Buf to Str

2021-06-09 Thread Paul Procacci
Not sure about the 80's, my programming endeavors started in the 90's.
NUL doesn't exist in the C standard so I have no comment on it.
The C standard defines that 0 cast to the type void * is both a null
pointer and a null pointer constant.
I always have and most likely will continue using 0 over NULL rightly or
otherwise; though tend to use either/or when describing something ... as
Elizabeth pointed out.

Potato vs Potatoe I guess.
~Paul

On Wed, Jun 9, 2021 at 10:20 AM yary  wrote:

> From my early 1980s days learning programming, ASCII CHR 0 is not null, it
> is NUL (can type it as ctrl-@) it's a control character much like the
> others.
>
> Ctrl-G BEL, it was fun putting you in file names...
>
> On Wed, Jun 9, 2021, 9:56 AM Paul Procacci  wrote:
>
>> >> But yeah, the Str class in Raku is much more than a C-string.
>>
>> Got it.  Thanks Elizabeth.
>>
>> On Wed, Jun 9, 2021 at 6:45 AM Elizabeth Mattijsen 
>> wrote:
>>
>>> > On 9 Jun 2021, at 06:34, Paul Procacci  wrote:
>>> >
>>> > Hopefully a pretty quick question
>>> >
>>> > GIven the following:
>>> >
>>> > my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]);
>>> > say $b.decode;
>>> >
>>> > I would expect this to print 'Hi'.
>>> > Instead it prints 'Hi again'.
>>> >
>>> > https://docs.raku.org/type/Buf#(Blob)_method_decode
>>> >
>>> > The decode documentation for Buf only states that 'Applies an encoding
>>> to turn the blob into a Str; the encoding will be UTF-8 by default.'
>>>
>>> >
>>> > The zero (0) in that Buf should imply an end of string yet decode
>>> seems to want to decode the number of elements instead.
>>>
>>> That is an incorrect assumption carried over from C.  In the Raku
>>> Programming Language, a null byte is a valid grapheme, as it is in
>>> unicode.  A small change to your program:
>>>
>>> my Buf $b .= new(72, 105, 0, 32, 97, 103, 97, 105, 110, 0);
>>> .say for $b.decode.uninames;
>>> #
>>> LATIN CAPITAL LETTER H
>>> LATIN SMALL LETTER I
>>> 
>>> SPACE
>>> LATIN SMALL LETTER A
>>> LATIN SMALL LETTER G
>>> LATIN SMALL LETTER A
>>> LATIN SMALL LETTER I
>>> LATIN SMALL LETTER N
>>> 
>>>
>>>
>>> > Furthermore, If I 'say $b.decode.chars;' it counts the initial null as
>>> part of Str.
>>> > In my mind, that means Str doesn't really mean string.
>>>
>>> I don't see an initial null in your example.
>>>
>>> But yeah, the Str class in Raku is much more than a C-string.
>>>
>>>
>>> > So the question, how does one ACTUALLY decode what's in a buffer to a
>>> string where it adheres to the semantics of NULL termination for strings
>>> cleanly.
>>>
>>> If you want to include the null byte in your final strings:
>>>
>>> my @strings = $b.decode.comb(/ .*? "\0" /)
>>>
>>> would be a way.
>>>
>>>
>>>
>>> > Another question might be, should decode() follow null terminating
>>> semantics instead of number of elements in a given Buf.
>>>
>>> No.  The C-string semantics are what they are.  They are not the
>>> semantics used in the Raku Programming Language.
>>>
>>>
>>>
>>> Liz
>>
>>
>>
>> --
>> __
>>
>> :(){ :|:& };:
>>
>

-- 
__

:(){ :|:& };:


Re: Buf to Str

2021-06-09 Thread Paul Procacci
>> But yeah, the Str class in Raku is much more than a C-string.

Got it.  Thanks Elizabeth.

On Wed, Jun 9, 2021 at 6:45 AM Elizabeth Mattijsen  wrote:

> > On 9 Jun 2021, at 06:34, Paul Procacci  wrote:
> >
> > Hopefully a pretty quick question
> >
> > GIven the following:
> >
> > my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]);
> > say $b.decode;
> >
> > I would expect this to print 'Hi'.
> > Instead it prints 'Hi again'.
> >
> > https://docs.raku.org/type/Buf#(Blob)_method_decode
> >
> > The decode documentation for Buf only states that 'Applies an encoding
> to turn the blob into a Str; the encoding will be UTF-8 by default.'
>
> >
> > The zero (0) in that Buf should imply an end of string yet decode seems
> to want to decode the number of elements instead.
>
> That is an incorrect assumption carried over from C.  In the Raku
> Programming Language, a null byte is a valid grapheme, as it is in
> unicode.  A small change to your program:
>
> my Buf $b .= new(72, 105, 0, 32, 97, 103, 97, 105, 110, 0);
> .say for $b.decode.uninames;
> #
> LATIN CAPITAL LETTER H
> LATIN SMALL LETTER I
> 
> SPACE
> LATIN SMALL LETTER A
> LATIN SMALL LETTER G
> LATIN SMALL LETTER A
> LATIN SMALL LETTER I
> LATIN SMALL LETTER N
> 
>
>
> > Furthermore, If I 'say $b.decode.chars;' it counts the initial null as
> part of Str.
> > In my mind, that means Str doesn't really mean string.
>
> I don't see an initial null in your example.
>
> But yeah, the Str class in Raku is much more than a C-string.
>
>
> > So the question, how does one ACTUALLY decode what's in a buffer to a
> string where it adheres to the semantics of NULL termination for strings
> cleanly.
>
> If you want to include the null byte in your final strings:
>
> my @strings = $b.decode.comb(/ .*? "\0" /)
>
> would be a way.
>
>
>
> > Another question might be, should decode() follow null terminating
> semantics instead of number of elements in a given Buf.
>
> No.  The C-string semantics are what they are.  They are not the semantics
> used in the Raku Programming Language.
>
>
>
> Liz



-- 
__

:(){ :|:& };:


Buf to Str

2021-06-08 Thread Paul Procacci
Hopefully a pretty quick question

GIven the following:

my Buf $b .= new([72, 105, 0, 32, 97, 103, 97, 105, 110, 0]);
say $b.decode;

I would expect this to print 'Hi'.
Instead it prints 'Hi again'.

https://docs.raku.org/type/Buf#(Blob)_method_decode

The decode documentation for Buf only states that 'Applies an encoding to
turn the blob into a Str ; the encoding
will be UTF-8 by default.'


The zero (0) in that Buf should imply an end of string yet decode seems to
want to decode the number of elements instead.

Furthermore, If I 'say $b.decode.chars;' it counts the initial null as part
of Str.
In my mind, that means Str doesn't really mean string.

So the question, how does one ACTUALLY decode what's in a buffer to a
string where it adheres to the semantics of NULL termination for strings
cleanly.

Another question might be, should decode() follow null terminating
semantics instead of number of elements in a given Buf.

Thanks,
Paul
-- 
__

:(){ :|:& };:


Re: Find Packages with prefix ...

2021-03-07 Thread Paul Procacci
Thanks Richard.

You have described it perfectly.  This is exactly what I mean.  I was
reading about $*REPO but like you, I am unable to truly grasp how to get at
the information required to make this a reality.

On Sun, Mar 7, 2021 at 7:07 AM Richard Hainsworth 
wrote:

> Interesting question.
>
> I would like to know a good way of doing this. But just to refine the
> question, so that we don't discuss which directory modules are in (doesn't
> matter whether its lib/ or bin/), or whether we use classes and
> inheritance, the interesting (for me) question is: how to find other
> packages that have been installed?
>
> Assume that we have three distributions, Parent, Brother, Sister. Parent
> 'provides' 'Top.rakumod', while brother and sister provide
> 'Top::Son.rakumod' and 'Top::daughter.rakumod', respectively. Brother and
> Sister have a 'depends': 'Top' in their META6.json.
>
> Each distribution is in its own git repo. And each are installed by zef.
>
> Suppose Top is set up as a class with an interface method 'multi method
> on-starting { ... }', which each sub-class has to implement, and which when
> run, provides the caller with information about the sub-class.
>
> So, now we run an instance of Top (as defined in Parent). It needs to look
> for installed modules that match 'Top::*'. The place to start (I think) is
> '$*REPO', which is a linked list of repositories containing the modules
> that are installed. $*REPO also does the CompUnit::Repository role, which
> in turn has a 'need' method.
>
> What I don't understand is how to manipulate $*REPO to get a list of all
> the candidate modules that match 'Top::*', along the whole of the linked
> list.
>
> Once I have the list of candidates, I can use '^can' to check it has a
> 'on-starting' method, and then call that method.
>
> If someone could provide a bit of boiler plate code, I would really
> appreciate it.
>
> Richard
> On 07/03/2021 02:36, Paul Procacci wrote:
>
> Hey Gents,
>
> I was toying with an idea of writing a program (shocker!) and in the
> design of said program I wanted to give the ability to other module writers
> to extend functionality of the base program.
>
> The main program would live in bin/ as per normal and claim a namespace of
> its own.  Call it: SuperDuperProgram
>
> Within this namespace, I would ideally designate
> SuperDuperProgram::Modules to be the namespace that modules would live
> under.  These modules would have their own git repos and depend on
> SuperDuperProgram being installed.
>
> Given this, I'd like the main program to perform a prefix search of
> installed modules with that namespace as a prefix, load said modules it
> finds, perform callbacks that I would designate as being required to
> successfully load the module, etc.
>
> I've read various resources which include the zef source code, the raku
> docs, etc., yet am still unable to wrap my head around this.
>
> Any pointers in the right direction as welcomed.
>
> A mock up of what I had in mind:
>
> ./bin/SuperDuperProgram
> ###
> #!/usr/bin/env raku
>
> my @modules_with_prefix =
> search_installed_modules_with_prefix('SuperDuperProgram::Modules');
>
> for @modules_with_prefix {
>   # require module
>   # perform init callbacks - new() for example
>   # whatever else
> }
>
> --
> __
>
> :(){ :|:& };:
>
>

-- 
__

:(){ :|:& };:


Find Packages with prefix ...

2021-03-06 Thread Paul Procacci
Hey Gents,

I was toying with an idea of writing a program (shocker!) and in the design
of said program I wanted to give the ability to other module writers to
extend functionality of the base program.

The main program would live in bin/ as per normal and claim a namespace of
its own.  Call it: SuperDuperProgram

Within this namespace, I would ideally designate SuperDuperProgram::Modules
to be the namespace that modules would live under.  These modules would
have their own git repos and depend on SuperDuperProgram being installed.

Given this, I'd like the main program to perform a prefix search of
installed modules with that namespace as a prefix, load said modules it
finds, perform callbacks that I would designate as being required to
successfully load the module, etc.

I've read various resources which include the zef source code, the raku
docs, etc., yet am still unable to wrap my head around this.

Any pointers in the right direction as welcomed.

A mock up of what I had in mind:

./bin/SuperDuperProgram
###
#!/usr/bin/env raku

my @modules_with_prefix =
search_installed_modules_with_prefix('SuperDuperProgram::Modules');

for @modules_with_prefix {
  # require module
  # perform init callbacks - new() for example
  # whatever else
}

-- 
__

:(){ :|:& };:


Re: Extracting 4 wchar_t's from int64

2021-01-14 Thread Paul Procacci
My C code has nothing to do with the question I'm asking.
I'll share it anyway to my dismay.

static int raidFillProcessStruct(raidProcess* process)
> {
> process->handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
> if (process->handle == INVALID_HANDLE_VALUE) return 0;
>
> process->entry.dwSize = sizeof(process->entry);
> if (Process32First(process->handle, &process->entry)) {
> do {
> if (!wcscmp(L"Raid.exe", process->entry.szExeFile)) {
> CloseHandle(process->handle);
> return 1;
> }
> } while (Process32Next(process->handle, &process->entry));
> }
>
> CloseHandle(process->handle);
> return 0;
> }
>
>
My C code also doesn't print anything as that's not it's intention.  It
returns with an appropriate exit code which
is all my proof of concept measures.

The above is besides the point however and functions as expected.

I have defined a CStruct that contains 65 non inlined int64's.
Each int64 contains four int16's that I need to extract at the following
bit positions:  [0,15], [16,31], [32, 47], [48, 63]

What method accomplishes this, if any?
I'm ok with no function existing in which case I'll need to roll my own,
but this is the premise for this email.


On Thu, Jan 14, 2021 at 11:00 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/14/21 7:38 PM, Paul Procacci wrote:
> > I may have.
> >
> > Regardless, I have written a proof of concept in 'C' which works.
> > My question isn't how to interface with the Windows Operating System
> > because I have already done so in 'C' quite easily.
> >
> > My question pertains to working around the bug as described here:
> > https://github.com/rakudo/rakudo/issues/3633
> > <https://github.com/rakudo/rakudo/issues/3633>
> > Do you have any comments concerning this bug and/or the approach I'm
> taking?
> > Passing a flattened array of 260 int16's doesn't work so my approach is
> > passing 65 non-flattened int64's.
> >
> > The 65 int64's are in fact being utilized by the callee, yet I'm looking
> > for a method to reconstruct this back into 260 wchar_t's (which has a
> > width of 16)  w/ a simple method that doesn't include me walking the
> > int64 and masking the chars out myself.
> > Does this exist.
> >
> > Thanks,
> > ~Paul
> >
> > On Thu, Jan 14, 2021 at 9:58 PM ToddAndMargo via perl6-users
> > mailto:perl6-users@perl.org>> wrote:
> >
> > On 1/14/21 4:32 PM, Paul Procacci wrote:
> >  >
> >
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
> > <
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
> >
> >
> >  >
> > <
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
> > <
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
> >>
> >  >
> >  > On Thu, Jan 14, 2021 at 7:30 PM ToddAndMargo via perl6-users
> >  > mailto:perl6-users@perl.org>
> > <mailto:perl6-users@perl.org <mailto:perl6-users@perl.org>>> wrote:
> >  >
> >  > On 1/14/21 3:42 PM, Paul Procacci wrote:
> >  >  > Let me preface this by saying if I were using a lower
> > level language
> >  >  > (like C) I wouldn't have this problem; as I know how to
> > shift and
> >  > mask
> >  >  > accordingly.
> >  >  >
> >  >  > On raku however, how to do so *eloquantly* eludes me.
> >  >  >
> >  >  > I've defined a CStruct as follows:
> >  >  >
> >  >  > class test is repr('CStruct') {
> >  >  >
> >  >  >has int64 $.a1;
> >  >  >
> >  >  >has int64 $.a2;
> >  >  >
> >  >  >...
> >  >  >
> >  >  >has int64 $.a65;
> >  >  >
> >  >  > }
> >  >  >
> >  >  > Under normal circumstances I would have defined the member
> as:
> >  >  > `HAS int16 @.a[260] is CArray` however that seems to be
> > broken. See:
> >  >  >
> >  >  > https://github.com/rakudo/rakudo/issu

Re: Extracting 4 wchar_t's from int64

2021-01-14 Thread Paul Procacci
I may have.

Regardless, I have written a proof of concept in 'C' which works.
My question isn't how to interface with the Windows Operating System
because I have already done so in 'C' quite easily.

My question pertains to working around the bug as described here:
https://github.com/rakudo/rakudo/issues/3633
Do you have any comments concerning this bug and/or the approach I'm taking?
Passing a flattened array of 260 int16's doesn't work so my approach is
passing 65 non-flattened int64's.

The 65 int64's are in fact being utilized by the callee, yet I'm looking
for a method to reconstruct this back into 260 wchar_t's (which has a width
of 16)  w/ a simple method that doesn't include me walking the int64 and
masking the chars out myself.
Does this exist.

Thanks,
~Paul

On Thu, Jan 14, 2021 at 9:58 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/14/21 4:32 PM, Paul Procacci wrote:
> >
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
> > <
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
> >
> >
> > On Thu, Jan 14, 2021 at 7:30 PM ToddAndMargo via perl6-users
> > mailto:perl6-users@perl.org>> wrote:
> >
> > On 1/14/21 3:42 PM, Paul Procacci wrote:
> >  > Let me preface this by saying if I were using a lower level
> language
> >  > (like C) I wouldn't have this problem; as I know how to shift and
> > mask
> >  > accordingly.
> >  >
> >  > On raku however, how to do so *eloquantly* eludes me.
> >  >
> >  > I've defined a CStruct as follows:
> >  >
> >  > class test is repr('CStruct') {
> >  >
> >  >has int64 $.a1;
> >  >
> >  >has int64 $.a2;
> >  >
> >  >...
> >  >
> >  >has int64 $.a65;
> >  >
> >  > }
> >  >
> >  > Under normal circumstances I would have defined the member as:
> >  > `HAS int16 @.a[260] is CArray` however that seems to be broken.
> See:
> >  >
> >  > https://github.com/rakudo/rakudo/issues/3633
> > <https://github.com/rakudo/rakudo/issues/3633>
> >  > <https://github.com/rakudo/rakudo/issues/3633
> > <https://github.com/rakudo/rakudo/issues/3633>>
> >  >
> >  > The function that gets called with these members $.a1 .. $.a65
> > should
> >  > get filled with a wchar_t character array.
> >  >
> >  > Does raku employ a simple method of decoding the 4 wchar_t's that
> > get
> >  > placed into a int64 structure?  The characters I'd like to
> > extract are
> >  > at bit positions [15:0], [31.16], [47:32], [63:48]
> >  >
> >  > I'm imagining something along the lines of:
> >  >
> >  > buf16.new( $.a1, ... $.a64);
> >  >
> >  > ... but that doesn't quite work like I would expect.
> >  >
> >  > Thanks,
> >  > ~Paul
> >
> > Hi Paul,
> >
> > Would you post the system call you are trying to
> > interface with?
> >
> > -T
>
>
> Did you see?
>
>
> https://docs.microsoft.com/en-us/windows/win32/toolhelp/taking-a-snapshot-and-viewing-processes
>
>

-- 
__

:(){ :|:& };:


Re: Extracting 4 wchar_t's from int64

2021-01-14 Thread Paul Procacci
https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot

On Thu, Jan 14, 2021 at 7:30 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/14/21 3:42 PM, Paul Procacci wrote:
> > Let me preface this by saying if I were using a lower level language
> > (like C) I wouldn't have this problem; as I know how to shift and mask
> > accordingly.
> >
> > On raku however, how to do so *eloquantly* eludes me.
> >
> > I've defined a CStruct as follows:
> >
> > class test is repr('CStruct') {
> >
> >has int64 $.a1;
> >
> >has int64 $.a2;
> >
> >...
> >
> >has int64 $.a65;
> >
> > }
> >
> > Under normal circumstances I would have defined the member as:
> > `HAS int16 @.a[260] is CArray` however that seems to be broken. See:
> >
> > https://github.com/rakudo/rakudo/issues/3633
> > <https://github.com/rakudo/rakudo/issues/3633>
> >
> > The function that gets called with these members $.a1 .. $.a65 should
> > get filled with a wchar_t character array.
> >
> > Does raku employ a simple method of decoding the 4 wchar_t's that get
> > placed into a int64 structure?  The characters I'd like to extract are
> > at bit positions [15:0], [31.16], [47:32], [63:48]
> >
> > I'm imagining something along the lines of:
> >
> > buf16.new( $.a1, ... $.a64);
> >
> > ... but that doesn't quite work like I would expect.
> >
> > Thanks,
> > ~Paul
>
> Hi Paul,
>
> Would you post the system call you are trying to
> interface with?
>
> -T
>
>

-- 
__

:(){ :|:& };:


Extracting 4 wchar_t's from int64

2021-01-14 Thread Paul Procacci
Let me preface this by saying if I were using a lower level language (like
C) I wouldn't have this problem; as I know how to shift and mask
accordingly.

On raku however, how to do so *eloquantly* eludes me.

I've defined a CStruct as follows:

class test is repr('CStruct') {

  has int64 $.a1;

  has int64 $.a2;

  ...

  has int64 $.a65;

}

Under normal circumstances I would have defined the member as:
`HAS int16 @.a[260] is CArray` however that seems to be broken. See:

https://github.com/rakudo/rakudo/issues/3633

The function that gets called with these members $.a1 .. $.a65 should get
filled with a wchar_t character array.

Does raku employ a simple method of decoding the 4 wchar_t's that get
placed into a int64 structure?  The characters I'd like to extract are at
bit positions [15:0], [31.16], [47:32], [63:48]

I'm imagining something along the lines of:

buf16.new( $.a1, ... $.a64);

... but that doesn't quite work like I would expect.

Thanks,
~Paul

-- 
__

:(){ :|:& };:


Re: Nativecall - Callee filled static character array - CArray [repost]

2021-01-03 Thread Paul Procacci
Thanks for that.
I did see that but neglected to add my 2 cents.
I just have though.

~Paul

On Sun, Jan 3, 2021 at 8:40 PM Paul Procacci  wrote:

> This is a repost from an improperly worded email.
> That previous email thread divulged into things it shouldn't have to which
> I'm partially to blame.
> This isn't Windows specific - the problem occurs across platforms.
>
> This is simply about the proper way to define an *inline* array of items
> in a Raku CStruct definition.  It's also about the retrieval of those
> stored values.
> The type of items aren't relevant. char[n], int[n], int16[n], etc ... it
> doesn't matter one bit.
>
> Given the following C structure:
>
> typedef struct T {
>> char a[260];
>> int32_t  b;
>> } T;
>>
>
> and given the following C function body:
>
> void setTest(T *t){
>> (void)memset(t->a, 'T', 260);
>> t->b = 1;
>> }
>>
>
>  I presumed this would be defined as follows in Raku[1]:
>
> class T is repr('CStruct') {
>> HAS int8 @.a[260] is CArray;
>> has int32 $.b;
>> };
>>
>> sub setTest(T) is native('./test.so') { * };
>>
>
> and invoked as such:
>
> my T $t .= new;
>> setTest($t);
>>
>
> While the value of the member 'b' gets set to 1 as expected, I cannot
> inspect the values that should be stored at the memory location referenced
> by member 'a[0]..a[n]'.
>
> Conversely, the following C program snippet that utilizes the same C
> function provides the output one would expect:
>
> extern void setTest(T *);
>>
>> T t;
>>
>> int main(void)
>> {
>> setTest(&t);
>> printf("%c\n%d\n", t.a[0], t.b);
>> _exit(0);
>> }
>>
>
> So the questions are:
>
> 1) How does one define an *inline* array of whatever size in Raku (size
> doesn't matter)
> 2) How does one retrieve the values stored in that defined array after the
> callee populates it.
>
> Thanks,
> ~Paul
>
> [1] - test.so is the shared object that I created for testing.
> --
> __
>
> :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Nativecall - Help with converting wchar_t CArray to something printable

2021-01-03 Thread Paul Procacci
Todd,

I know what 'is rw' is for.  This isn't at all related to the problem I'm
having.

On Sun, Jan 3, 2021 at 9:43 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/3/21 10:40 AM, Paul Procacci wrote:
> > Furthermore you can't pass a naked $entry to like so:
> > Process32First($handle, $entry)
> > It has to be a pointer;  had you tried what you suggested, raku would
> > have thrown errors and for good reason.
>
> Do you understand about "is rw" now?
>
>  Process32First($handle, $entry is rw)
>
>
>

-- 
__

:(){ :|:& };:


Re: Nativecall - Help with converting wchar_t CArray to something printable

2021-01-03 Thread Paul Procacci
No, it doesn't work.
The inline statically defined array in Raku never gets filled with any data
from the win32 function call.

On Sun, Jan 3, 2021 at 9:37 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/3/21 12:00 PM, Paul Procacci wrote:
> > Todd,
> >
> > I've made a mistake. Raku does ensure a pointer gets passed to the
> > function transparently when referencing a structure and doesn't require
> > one to be explicit.
> > I stand corrected.
> >
> > ~Paul
>
> Did you get it working?
>


-- 
__

:(){ :|:& };:


Nativecall - Callee filled static character array - CArray [repost]

2021-01-03 Thread Paul Procacci
This is a repost from an improperly worded email.
That previous email thread divulged into things it shouldn't have to which
I'm partially to blame.
This isn't Windows specific - the problem occurs across platforms.

This is simply about the proper way to define an *inline* array of items in
a Raku CStruct definition.  It's also about the retrieval of those stored
values.
The type of items aren't relevant. char[n], int[n], int16[n], etc ... it
doesn't matter one bit.

Given the following C structure:

typedef struct T {
> char a[260];
> int32_t  b;
> } T;
>

and given the following C function body:

void setTest(T *t){
> (void)memset(t->a, 'T', 260);
> t->b = 1;
> }
>

 I presumed this would be defined as follows in Raku[1]:

class T is repr('CStruct') {
> HAS int8 @.a[260] is CArray;
> has int32 $.b;
> };
>
> sub setTest(T) is native('./test.so') { * };
>

and invoked as such:

my T $t .= new;
> setTest($t);
>

While the value of the member 'b' gets set to 1 as expected, I cannot
inspect the values that should be stored at the memory location referenced
by member 'a[0]..a[n]'.

Conversely, the following C program snippet that utilizes the same C
function provides the output one would expect:

extern void setTest(T *);
>
> T t;
>
> int main(void)
> {
> setTest(&t);
> printf("%c\n%d\n", t.a[0], t.b);
> _exit(0);
> }
>

So the questions are:

1) How does one define an *inline* array of whatever size in Raku (size
doesn't matter)
2) How does one retrieve the values stored in that defined array after the
callee populates it.

Thanks,
~Paul

[1] - test.so is the shared object that I created for testing.
-- 
__

:(){ :|:& };:


Re: Nativecall - Help with converting wchar_t CArray to something printable

2021-01-03 Thread Paul Procacci
Todd,

I've made a mistake.  Raku does ensure a pointer gets passed to the
function transparently when referencing a structure and doesn't require one
to be explicit.
I stand corrected.

~Paul

On Sun, Jan 3, 2021 at 1:40 PM Paul Procacci  wrote:

> Todd,
>
> Nothing you stated addresses my initial question nor subsequent findings.
> My question is simply how to retrieve the value that gets stored in the
> CArray; nothing more.
> My subsequent findings included the following as I believe I'm running
> into it: https://github.com/rakudo/rakudo/issues/3633
>
>
> Irrelevant musings:
> --
> A windows handle is a pointer.
> A handle is an abstraction to a memory location that can be used in
> subsequent api calls.
> Declaring it as any incarnation of an int is a mistake and will not
> function properly across all machines.
>
> --
> The windows api requires the PROCESSENTRY32 structure member dwSize to be
> set to size of the structure.
> There's nothing wrong with my instantiation.
> Passing this structure to Process32First and Process32Next in the manner I
> have done so works.
>
> Furthermore you can't pass a naked $entry to like so:
> Process32First($handle, $entry)
> It has to be a pointer;  had you tried what you suggested, raku would have
> thrown errors and for good reason.
>
> --
> My suggestion is to try it yourself.  Utilize the windows api in a manner
> in which you'd expect it to run
> properly and see what happens.  Retrieve the value in szExeFile.  If you'd
> get it to work properly, show me
> what version of raku you are using and supply it as it could be a bug in
> the version I'm running.
>
> ~Paul
>
> On Sun, Jan 3, 2021 at 5:37 AM ToddAndMargo via perl6-users <
> perl6-users@perl.org> wrote:
>
>> On 1/2/21 11:38 PM, Paul Procacci wrote:
>> > I don't have a C string that's terminated by null.
>> > I have a CArray[int16] of length 260 that's passed to and filled in by
>> > the windows api.
>> >
>> >
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
>> > <
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
>> >
>> >
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32first
>> > <
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32first
>> >
>> >
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32next
>> > <
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32next
>> >
>> >
>> > Take the following for example which I quickly whipped up that is
>> > supposed to iterate over the processes running on the given windows
>> machine:
>> >
>> > -
>> >
>> > use NativeCall;
>> >
>> > constant \MAX_PATH = 260;
>> > constant \TH32CS_SNAPPROCESS = 0x0002;
>> >
>> > class PROCESSENTRY32 is repr('CStruct') {
>> >  has int32 $.dwSize;
>> >  has int32 $.cntUsage;
>> >  has int32 $.th32ProcessID;
>> >  has int32 $.th32DefaultHeapID;
>> >  has int32 $.th32ModuleID;
>> >  has int32 $.cntThreads;
>> >  has int32 $.th32ParentProcessID;
>> >  has int32 $.pcPriClassBase;
>> >  has int32 $.dwFlags is rw;
>> >  HAS uint16 @.szExeFile[MAX_PATH] is CArray;
>> > };
>> >
>> > sub CreateToolhelp32Snapshot(int32,int32 -->Pointer)is
>> native('Kernel32') { * };
>> > sub Process32First(Pointer,Pointer -->Bool)is native('Kernel32') { * };
>> > sub Process32Next(Pointer,Pointer -->Bool)is native('Kernel32') { * };
>> >
>> > my $ptr = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
>> > die 'Failed to retreive process list.' if $ptr == Bool::False;
>> > my PROCESSENTRY32 $entry .= new(:dwSize(nativesizeof(PROCESSENTRY32)));
>> > die 'Unable to iterate over process list' if Process32First($ptr,
>> nativecast(Pointer, $entry))== Bool::False;
>> >
>> > repeat {
>> >  say $entry.szExeFile[0].ord;
>> >  say $entry.szExeFile[1].ord;
>> > }while Process32Next($ptr, nativecast(Pointer, $entry));
>> >
>> > --
>> >
>> > The output of the above is:
>> > 48
>&g

Re: Nativecall - Help with converting wchar_t CArray to something printable

2021-01-03 Thread Paul Procacci
To expand further on the problem I'm running into, take the following C
function:


#include 

typedef struct T {
char a[260];
int32_t  b;
} T;

void setTest(T *t)
{
(void)memset(t->a, 'T', 260);
t->b = 1;
}
-

gcc -c -O3 -Wextra -Wall -Wno-unused-parameter -Wno-unused-function
-Wno-missing-braces -Werror=pointer-arith -O3 -DNDEBUG -D_REENTRANT
-D_FILE_OFFSET_BITS=64 -DMVM_HEAPSNAPSHOT_FORMAT=2 -o test.o test.c
gcc -shared -fPIC -O3 -DNDEBUG -Wl,-rpath,"//opt/rakudo-pkg/lib" -o test.so
test.o

... and the following raku program:

-
#!/usr/bin/env raku

use NativeCall;

class T is repr('CStruct') {
HAS int8 @.a[260] is CArray;
has int32 $.b;
};

sub setTest(T) is native('./test.so') { * };

my T $t .= new;
setTest($t);
say $t.a[0];
say $t.b;
-

Running this yields:

# ./test.raku
0
1

What's expected is:

# ./test.raku
T
1

On Sun, Jan 3, 2021 at 1:29 AM Paul Procacci  wrote:

> A follow-up to my initial message.
>
> I think the following is relevant:
>
> https://github.com/rakudo/rakudo/issues/3633
>
> I think my inlined array is actually being filled with zero's.
>
> say $a.a[0];
> say $a.a[1];
>
> Yields:
>
> 0
> 0
>
> I'm using the comma ide:
>
> >raku -v
> Welcome to Rakudo(tm) v2020.12.
> Implementing the Raku(tm) programming language v6.d.
> Built on MoarVM version 2020.12.
>
> ~Paul
>
> On Sat, Jan 2, 2021 at 11:13 PM Paul Procacci  wrote:
>
>> Hey gents (again),
>>
>> I'm having on awful time with decoding UTF16LE character sequences that
>> are placed into a Nativecall CArray that I've defined as an interface
>> between Raku and a windows library call.
>>
>> The structure used by the windows function includes a static wchar_t
>> field that's PATH_MAX in
>> length (260 wchar_t's).  It would look like the following in C:
>>
>> struct Something {
>>   int32_t dwSize;
>>   wchar_t a[260];
>> };
>>
>> I've written the following as the Raku counterpart to the above:
>>
>> class Something is repr('CStruct') {
>>has int32 $.dwsize;
>>HAS int16 @.a[260] is CArray;
>> };
>>
>> Given the following definition for the windows library call:
>>
>> bool Test(Something *x);
>>
>> .. I've defined the following on the raku side of things:
>>
>> sub Test(Pointer --> Bool) is native('Kernel32') { * };
>>
>> --
>>
>> The function is supposed to write characters into the space allotted,
>> namely a.
>> The only required set member prior to calling the subroutine is that the
>> size of the structure.
>> It gets set in dwSize like so:
>>
>> my Something $a .= new(:dwSize(nativesizeof(Something)));
>>
>> Then the actual function call from the raku side of things:
>>
>> Test(nativecast(Pointer, $a));
>>
>> --
>>
>> All the above is working.  What I'm trying to attempt to do now is
>> display the contents of that
>> CArray (raku member a).  In my mind, this is an "array of utf16LE byte
>> sequences terminated by 0".
>>
>> What's the cleanest way of doing this in raku?  I've tried variations of
>> "join" and "encode", simple printf's, say's, etc., and even tried
>> manipulating this data with Buf's, but I can't seem to get it quite right.
>>
>> Any help is appreciated.
>> ~Paul
>> --
>> __
>>
>> :(){ :|:& };:
>>
>
>
> --
> __
>
> :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Nativecall - Help with converting wchar_t CArray to something printable

2021-01-03 Thread Paul Procacci
Todd,

Nothing you stated addresses my initial question nor subsequent findings.
My question is simply how to retrieve the value that gets stored in the
CArray; nothing more.
My subsequent findings included the following as I believe I'm running into
it: https://github.com/rakudo/rakudo/issues/3633


Irrelevant musings:
--
A windows handle is a pointer.
A handle is an abstraction to a memory location that can be used in
subsequent api calls.
Declaring it as any incarnation of an int is a mistake and will not
function properly across all machines.

--
The windows api requires the PROCESSENTRY32 structure member dwSize to be
set to size of the structure.
There's nothing wrong with my instantiation.
Passing this structure to Process32First and Process32Next in the manner I
have done so works.

Furthermore you can't pass a naked $entry to like so:
Process32First($handle, $entry)
It has to be a pointer;  had you tried what you suggested, raku would have
thrown errors and for good reason.

--
My suggestion is to try it yourself.  Utilize the windows api in a manner
in which you'd expect it to run
properly and see what happens.  Retrieve the value in szExeFile.  If you'd
get it to work properly, show me
what version of raku you are using and supply it as it could be a bug in
the version I'm running.

~Paul

On Sun, Jan 3, 2021 at 5:37 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/2/21 11:38 PM, Paul Procacci wrote:
> > I don't have a C string that's terminated by null.
> > I have a CArray[int16] of length 260 that's passed to and filled in by
> > the windows api.
> >
> >
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
> > <
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
> >
> >
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32first
> > <
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32first
> >
> >
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32next
> > <
> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32next
> >
> >
> > Take the following for example which I quickly whipped up that is
> > supposed to iterate over the processes running on the given windows
> machine:
> >
> > -
> >
> > use NativeCall;
> >
> > constant \MAX_PATH = 260;
> > constant \TH32CS_SNAPPROCESS = 0x0002;
> >
> > class PROCESSENTRY32 is repr('CStruct') {
> >  has int32 $.dwSize;
> >  has int32 $.cntUsage;
> >  has int32 $.th32ProcessID;
> >  has int32 $.th32DefaultHeapID;
> >  has int32 $.th32ModuleID;
> >  has int32 $.cntThreads;
> >  has int32 $.th32ParentProcessID;
> >  has int32 $.pcPriClassBase;
> >  has int32 $.dwFlags is rw;
> >  HAS uint16 @.szExeFile[MAX_PATH] is CArray;
> > };
> >
> > sub CreateToolhelp32Snapshot(int32,int32 -->Pointer)is
> native('Kernel32') { * };
> > sub Process32First(Pointer,Pointer -->Bool)is native('Kernel32') { * };
> > sub Process32Next(Pointer,Pointer -->Bool)is native('Kernel32') { * };
> >
> > my $ptr = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
> > die 'Failed to retreive process list.' if $ptr == Bool::False;
> > my PROCESSENTRY32 $entry .= new(:dwSize(nativesizeof(PROCESSENTRY32)));
> > die 'Unable to iterate over process list' if Process32First($ptr,
> nativecast(Pointer, $entry))== Bool::False;
> >
> > repeat {
> >  say $entry.szExeFile[0].ord;
> >  say $entry.szExeFile[1].ord;
> > }while Process32Next($ptr, nativecast(Pointer, $entry));
> >
> > --
> >
> > The output of the above is:
> > 48
> > 48
> >
> > This is clearly wrong.  This is either a) my misunderstanding of these
> api calls b) raku bug or c) a usage error on my part.
> >
> > ~Paul
>
> Hi Paul,
>
> Please bottom post.  It removes a lot of confusion.
>
> ~~
> CreateToolhelp32Snapshot:
>
> C++
>
> HANDLE CreateToolhelp32Snapshot(
>   DWORD dwFlags,
>   DWORD th32ProcessID
> );
>
>
> The return is a "handle", not a pointer.
>
> You declare it as such:
>
>  sub CreateToolhelp32Snapshot(int32, int32 --> Pointer) is
> native('Kernel32') { * };
>
>  my $ptr = CreateToolh

Re: Nativecall - Help with converting wchar_t CArray to something printable

2021-01-03 Thread Paul Procacci
Hi Fernando,

I appreciate your response.
The windows api requires that the dwSize member of the structure gets set
to the size of the structure.
This is the reason why the object is being constructed as I've provided.

~Paul

On Sun, Jan 3, 2021 at 4:58 AM Fernando Santagata 
wrote:

> Hi Paul,
> I can't help, since I haven't a Windows PC, but I noticed that you
> initialize your $entry variable like this:
>
> my PROCESSENTRY32 $entry .= new(:dwSize(nativesizeof(PROCESSENTRY32)));
>
> I don't think the argument to the "new" call is necessary. Try this:
>
> my PROCESSENTRY32 $entry .= new;
> say nativesizeof($entry);  # output: 556
>
> Raku already knows the native size of the CStruct class.
> Other than that I can't say anything useful.
>
> On Sun, Jan 3, 2021 at 8:38 AM Paul Procacci  wrote:
>
>> I don't have a C string that's terminated by null.
>> I have a CArray[int16] of length 260 that's passed to and filled in by
>> the windows api.
>>
>>
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
>>
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32first
>>
>> https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32next
>>
>> Take the following for example which I quickly whipped up that is
>> supposed to iterate over the processes running on the given windows machine:
>>
>> -
>>
>> use NativeCall;
>>
>> constant \MAX_PATH = 260;
>> constant \TH32CS_SNAPPROCESS = 0x0002;
>>
>> class PROCESSENTRY32 is repr('CStruct') {
>> has int32 $.dwSize;
>> has int32 $.cntUsage;
>> has int32 $.th32ProcessID;
>> has int32 $.th32DefaultHeapID;
>> has int32 $.th32ModuleID;
>> has int32 $.cntThreads;
>> has int32 $.th32ParentProcessID;
>> has int32 $.pcPriClassBase;
>> has int32 $.dwFlags is rw;
>> HAS uint16 @.szExeFile[MAX_PATH] is CArray;
>> };
>>
>> sub CreateToolhelp32Snapshot(int32, int32 --> Pointer) is native('Kernel32') 
>> { * };
>> sub Process32First(Pointer, Pointer --> Bool) is native('Kernel32') { * };
>> sub Process32Next(Pointer, Pointer --> Bool) is native('Kernel32') { * };
>>
>> my $ptr = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
>> die 'Failed to retreive process list.' if $ptr == Bool::False;
>> my PROCESSENTRY32 $entry .= new(:dwSize(nativesizeof(PROCESSENTRY32)));
>> die 'Unable to iterate over process list' if Process32First($ptr, 
>> nativecast(Pointer, $entry)) == Bool::False;
>>
>> repeat {
>> say $entry.szExeFile[0].ord;
>> say $entry.szExeFile[1].ord;
>> } while Process32Next($ptr, nativecast(Pointer, $entry));
>>
>> --
>>
>> The output of the above is:
>> 48
>> 48
>>
>> This is clearly wrong.  This is either a) my misunderstanding of these api 
>> calls b) raku bug or c) a usage error on my part.
>>
>> ~Paul
>>
>>
>> On Sun, Jan 3, 2021 at 2:12 AM ToddAndMargo via perl6-users <
>> perl6-users@perl.org> wrote:
>>
>>> On 1/2/21 8:13 PM, Paul Procacci wrote:
>>> > What I'm trying to attempt to do now is display the contents of that
>>> > CArray (raku member a).  In my mind, this is an "array of utf16LE byte
>>> > sequences terminated by 0".
>>>
>>> Hi Paul,
>>>
>>> If I understand your problem, you are having trouble
>>> extracting something usable from Windows return UTF16
>>> string.
>>>
>>> I have a series of modules I wrote to read and write
>>> to the Windows registry.  The strings I got back were
>>> all "UTF16 little ending C Strings".
>>>
>>> This means that every character is a TWO byte word
>>> (16 bits) with the first byte containing the low
>>> value byte.  And they terminate with both bytes
>>> being a numerical zero.  It is a little weird to get
>>> use to.
>>>
>>> There was some interesting conversation in these
>>> parts a bit ago as to if a C string could ever
>>> not be terminated with a nul.  They are always
>>> terminated with a nul.
>>>
>>> The following is from one of the modules I wrote to
>>> support this.  I used brute force, rather than
>>> rely on UTF conversion uti

Re: Nativecall - Help with converting wchar_t CArray to something printable

2021-01-02 Thread Paul Procacci
 I don't have a C string that's terminated by null.
I have a CArray[int16] of length 260 that's passed to and filled in by the
windows api.

https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32first
https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32next

Take the following for example which I quickly whipped up that is supposed
to iterate over the processes running on the given windows machine:

-

use NativeCall;

constant \MAX_PATH = 260;
constant \TH32CS_SNAPPROCESS = 0x0002;

class PROCESSENTRY32 is repr('CStruct') {
has int32 $.dwSize;
has int32 $.cntUsage;
has int32 $.th32ProcessID;
has int32 $.th32DefaultHeapID;
has int32 $.th32ModuleID;
has int32 $.cntThreads;
has int32 $.th32ParentProcessID;
has int32 $.pcPriClassBase;
has int32 $.dwFlags is rw;
HAS uint16 @.szExeFile[MAX_PATH] is CArray;
};

sub CreateToolhelp32Snapshot(int32, int32 --> Pointer) is
native('Kernel32') { * };
sub Process32First(Pointer, Pointer --> Bool) is native('Kernel32') { * };
sub Process32Next(Pointer, Pointer --> Bool) is native('Kernel32') { * };

my $ptr = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
die 'Failed to retreive process list.' if $ptr == Bool::False;
my PROCESSENTRY32 $entry .= new(:dwSize(nativesizeof(PROCESSENTRY32)));
die 'Unable to iterate over process list' if Process32First($ptr,
nativecast(Pointer, $entry)) == Bool::False;

repeat {
say $entry.szExeFile[0].ord;
say $entry.szExeFile[1].ord;
} while Process32Next($ptr, nativecast(Pointer, $entry));

--

The output of the above is:
48
48

This is clearly wrong.  This is either a) my misunderstanding of these
api calls b) raku bug or c) a usage error on my part.

~Paul


On Sun, Jan 3, 2021 at 2:12 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/2/21 8:13 PM, Paul Procacci wrote:
> > What I'm trying to attempt to do now is display the contents of that
> > CArray (raku member a).  In my mind, this is an "array of utf16LE byte
> > sequences terminated by 0".
>
> Hi Paul,
>
> If I understand your problem, you are having trouble
> extracting something usable from Windows return UTF16
> string.
>
> I have a series of modules I wrote to read and write
> to the Windows registry.  The strings I got back were
> all "UTF16 little ending C Strings".
>
> This means that every character is a TWO byte word
> (16 bits) with the first byte containing the low
> value byte.  And they terminate with both bytes
> being a numerical zero.  It is a little weird to get
> use to.
>
> There was some interesting conversation in these
> parts a bit ago as to if a C string could ever
> not be terminated with a nul.  They are always
> terminated with a nul.
>
> The following is from one of the modules I wrote to
> support this.  I used brute force, rather than
> rely on UTF conversion utilities as I found them
> unreliable.
>
> If you un-comment
> # print $CStr[ $i ] ~ "," ~ $CStr[ $i + 1 ] ~ ",  ";
> you can watch the sub do its thing.
>
> I have a sub to go the other way too, if you want it.
>
> HTH,
> -T
>
>
>
> use NativeCall;
>
> sub c-to-raku-str( BYTES $CStr ) returns Str  is export( :c-to-raku-str ) {
> # Note: C Strings are always terminated with a nul.  This sub will
> malfunction without it.
> # Note: presumes a UTF16 little endian C String and converts to UTF8
>
> my Str$SubName   = &?ROUTINE.name;
> my Str$RakuStr   = "";
> my Str$Msg   = "";
> my uint32 $CStrElems = $CStr.elems;
> # say $CStrElems;
>
> loop (my $i = 0; $i < $CStrElems - 2 ; $i += 2) {
>if  $CStr[ $i ] == 0  &&  $CStr[ $i + 1 ] == 0  { last; }
>
>if $i == $CStrElems - 4  {
>   $Msg = "$SubName ERROR:" ~ " C Strings are required to be
> terminated with a nul\n\n" ~
>  " Returning an empty string\n" ~
>  " Cowardly existing\n";
>   exit;
>}
>
># print $CStr[ $i ] ~ "," ~ $CStr[ $i + 1 ] ~ ",  ";
>$RakuStr ~= chr( $CStr[ $i ] );
> }
> # say "";
>
> # say "$RakuStr";
> return $RakuStr;
> }
>
>
>

-- 
__

:(){ :|:& };:


Re: Nativecall - Help with converting wchar_t CArray to something printable

2021-01-02 Thread Paul Procacci
A follow-up to my initial message.

I think the following is relevant:

https://github.com/rakudo/rakudo/issues/3633

I think my inlined array is actually being filled with zero's.

say $a.a[0];
say $a.a[1];

Yields:

0
0

I'm using the comma ide:

>raku -v
Welcome to Rakudo(tm) v2020.12.
Implementing the Raku(tm) programming language v6.d.
Built on MoarVM version 2020.12.

~Paul

On Sat, Jan 2, 2021 at 11:13 PM Paul Procacci  wrote:

> Hey gents (again),
>
> I'm having on awful time with decoding UTF16LE character sequences that
> are placed into a Nativecall CArray that I've defined as an interface
> between Raku and a windows library call.
>
> The structure used by the windows function includes a static wchar_t field
> that's PATH_MAX in
> length (260 wchar_t's).  It would look like the following in C:
>
> struct Something {
>   int32_t dwSize;
>   wchar_t a[260];
> };
>
> I've written the following as the Raku counterpart to the above:
>
> class Something is repr('CStruct') {
>has int32 $.dwsize;
>HAS int16 @.a[260] is CArray;
> };
>
> Given the following definition for the windows library call:
>
> bool Test(Something *x);
>
> .. I've defined the following on the raku side of things:
>
> sub Test(Pointer --> Bool) is native('Kernel32') { * };
>
> --
>
> The function is supposed to write characters into the space allotted,
> namely a.
> The only required set member prior to calling the subroutine is that the
> size of the structure.
> It gets set in dwSize like so:
>
> my Something $a .= new(:dwSize(nativesizeof(Something)));
>
> Then the actual function call from the raku side of things:
>
> Test(nativecast(Pointer, $a));
>
> --
>
> All the above is working.  What I'm trying to attempt to do now is display
> the contents of that
> CArray (raku member a).  In my mind, this is an "array of utf16LE byte
> sequences terminated by 0".
>
> What's the cleanest way of doing this in raku?  I've tried variations of
> "join" and "encode", simple printf's, say's, etc., and even tried
> manipulating this data with Buf's, but I can't seem to get it quite right.
>
> Any help is appreciated.
> ~Paul
> --
> __
>
> :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Nativecall - Help with converting wchar_t CArray to something printable

2021-01-02 Thread Paul Procacci
Hey gents (again),

I'm having on awful time with decoding UTF16LE character sequences that are
placed into a Nativecall CArray that I've defined as an interface between
Raku and a windows library call.

The structure used by the windows function includes a static wchar_t field
that's PATH_MAX in
length (260 wchar_t's).  It would look like the following in C:

struct Something {
  int32_t dwSize;
  wchar_t a[260];
};

I've written the following as the Raku counterpart to the above:

class Something is repr('CStruct') {
   has int32 $.dwsize;
   HAS int16 @.a[260] is CArray;
};

Given the following definition for the windows library call:

bool Test(Something *x);

.. I've defined the following on the raku side of things:

sub Test(Pointer --> Bool) is native('Kernel32') { * };

--

The function is supposed to write characters into the space allotted,
namely a.
The only required set member prior to calling the subroutine is that the
size of the structure.
It gets set in dwSize like so:

my Something $a .= new(:dwSize(nativesizeof(Something)));

Then the actual function call from the raku side of things:

Test(nativecast(Pointer, $a));

--

All the above is working.  What I'm trying to attempt to do now is display
the contents of that
CArray (raku member a).  In my mind, this is an "array of utf16LE byte
sequences terminated by 0".

What's the cleanest way of doing this in raku?  I've tried variations of
"join" and "encode", simple printf's, say's, etc., and even tried
manipulating this data with Buf's, but I can't seem to get it quite right.

Any help is appreciated.
~Paul
-- 
__

:(){ :|:& };:


Re: NativeCall -- size of data structure

2021-01-02 Thread Paul Procacci
nativesizeof is what I was looking for.

Thanks,

~Paul


On Sat, Jan 2, 2021 at 8:39 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/2/21 4:58 PM, Paul Procacci wrote:
> > Hey Gents,
> >
> > Hopefully a simple question that I could not find the answer to.
> >
> > Given the following:
> >
> > class PROCESSENTRY32 is repr('CStruct') {
> >  has int32 $.dwSize;
> >  has int32 $.cntUsage;
> >  has int32 $.th32ProcessID;
> >  has Pointer $.th32DefaultHeapID;
> >  has int32 $.th32ModuleID;
> >  has int32 $.cntThreads;
> >  has int32 $.th32ParentProcessID;
> >  has long $.pcPriClassBase;
> >  has int32 $.dwFlags;
> >  HAS Str @.szExeFile[260] is CArray;# MAX_PATH on windows is 260
>
> Did you mean to capitalize the HAS?
>
> > };
> >
> >
> > ... is there is a raku method that gives me the size of this data
> structure?
> >
> > In C for example one would use sizeof() to provide the size.  In raku I
> > could not find the documentation that would provide this information.
> >
> > Thanks,
> > Paul Procacci
>
> Hi Paul,
>
> Not that I know of.  Raku has a lot of things in
> the background of its structures that C does not.
>
> C does not have OOP (object orientated programming)
> but does have "struct", which is close enough.
>
> What you are trying to do is match the C structure
> of the system call with Raku's OOP "class".  In Raku,
> "class" is the definition of a structure and not the
> structure itself -- very much like C's "struct".
>
> You don't get an actual structure until you
> declare one from the class.  For example:
>
>  my $x = PROCESSENTRY32.new;
>
> And it is going to be a lot bigger behind the scenes
> that just adding up the bits.  You really do not need
> to know the behind the scene details anyway.  Raku
> strives to remove the need for the programmer to
> have to know these kind of details, unlike C.
>
> What system call are you trying to run?
>
> I have an example of how to read Windows Time
> that also uses OOP (class, object, and method).
> Let me know if you want it
>
> -T
>
>
>
>

-- 
__

:(){ :|:& };:


Re: NativeCall -- size of data structure

2021-01-02 Thread Paul Procacci
Apologies.  I just came across it.

nativesizeof is the subroutine you're looking for.

Again, sorry for the noise.

~Paul


On Sat, Jan 2, 2021 at 7:58 PM Paul Procacci  wrote:

> Hey Gents,
>
> Hopefully a simple question that I could not find the answer to.
>
> Given the following:
>
> class PROCESSENTRY32 is repr('CStruct') {
> has int32 $.dwSize;
> has int32 $.cntUsage;
> has int32 $.th32ProcessID;
> has Pointer $.th32DefaultHeapID;
> has int32 $.th32ModuleID;
> has int32 $.cntThreads;
> has int32 $.th32ParentProcessID;
> has long $.pcPriClassBase;
> has int32 $.dwFlags;
> HAS Str @.szExeFile[260] is CArray;  # MAX_PATH on windows is 260
> };
>
>
> ... is there is a raku method that gives me the size of this data
> structure?
>
> In C for example one would use sizeof() to provide the size.  In raku I
> could not find the documentation that would provide this information.
>
> Thanks,
> Paul Procacci
> --
> __
>
> :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


NativeCall -- size of data structure

2021-01-02 Thread Paul Procacci
Hey Gents,

Hopefully a simple question that I could not find the answer to.

Given the following:

class PROCESSENTRY32 is repr('CStruct') {
has int32 $.dwSize;
has int32 $.cntUsage;
has int32 $.th32ProcessID;
has Pointer $.th32DefaultHeapID;
has int32 $.th32ModuleID;
has int32 $.cntThreads;
has int32 $.th32ParentProcessID;
has long $.pcPriClassBase;
has int32 $.dwFlags;
HAS Str @.szExeFile[260] is CArray;  # MAX_PATH on windows is 260
};


... is there is a raku method that gives me the size of this data structure?

In C for example one would use sizeof() to provide the size.  In raku I
could not find the documentation that would provide this information.

Thanks,
Paul Procacci
-- 
__

:(){ :|:& };:


Re: Sending a string to a subprocess

2020-11-11 Thread Paul Procacci
So sorry... hit enter way too early.

https://docs.raku.org/type/Proc

my $p = run "ssh", "myhost", "cat - > outfile", :in, :out;
$p.in.say: "Hello,\nworld!";

I haven't tested it, but you can obtain a proc object and write to the
processes' stdin directly.

~Paul


On Wed, Nov 11, 2020 at 7:41 PM Paul Procacci  wrote:

> https://docs.raku.org/type/Proc
>
> my $p = run "cat", "cat - > outfile", :in, :out;
> $p.in.say: "Hello,\nworld!";
>
>
> On Wed, Nov 11, 2020 at 7:15 PM Sean McAfee  wrote:
>
>> I posted this question on Stack Overflow a few days ago:
>> https://stackoverflow.com/questions/64757138/raku-sending-a-string-to-a-subprocesss-standard-input
>>
>> ...but there haven't been any answers, so I'm asking here.
>>
>> The tl;dr is that I don't see an obvious way to send data to a
>> subprocess's standard input stream, other than by creating a second
>> subprocess.
>>
>> For a simple example, suppose I wanted to store a file on a remote host
>> with ssh:
>>
>> my $content = 'file contents';
>> run 'ssh', 'myhost', 'cat > outfile', :in($what-goes-here);
>>
>> The only solution I've found after a lot of searching is not very
>> appealing.
>>
>> my $echo = run 'echo', $content, :out;
>> run 'ssh', 'myhost', 'cat > outfile', :in($echo.out);
>>
>> Is there any better way?
>>
>>
>
> --
> __
>
> :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Sending a string to a subprocess

2020-11-11 Thread Paul Procacci
https://docs.raku.org/type/Proc

my $p = run "cat", "cat - > outfile", :in, :out;
$p.in.say: "Hello,\nworld!";


On Wed, Nov 11, 2020 at 7:15 PM Sean McAfee  wrote:

> I posted this question on Stack Overflow a few days ago:
> https://stackoverflow.com/questions/64757138/raku-sending-a-string-to-a-subprocesss-standard-input
>
> ...but there haven't been any answers, so I'm asking here.
>
> The tl;dr is that I don't see an obvious way to send data to a
> subprocess's standard input stream, other than by creating a second
> subprocess.
>
> For a simple example, suppose I wanted to store a file on a remote host
> with ssh:
>
> my $content = 'file contents';
> run 'ssh', 'myhost', 'cat > outfile', :in($what-goes-here);
>
> The only solution I've found after a lot of searching is not very
> appealing.
>
> my $echo = run 'echo', $content, :out;
> run 'ssh', 'myhost', 'cat > outfile', :in($echo.out);
>
> Is there any better way?
>
>

-- 
__

:(){ :|:& };:


Re: Subset w/ Inline::Perl5 RE as constraint

2020-11-08 Thread Paul Procacci
 Thank you Bruce,

This does indeed help.  Like I mentioned to Joseph I have yet to test it
but because it's coming out of the SF Study group I imagine it works.  ;)
I'll certainly make noise if it doesn't.

Appreciate the time given to a follow-up.

~Paul

On Sun, Nov 8, 2020 at 7:42 PM Bruce Gray 
wrote:

> > On Fri, Nov 6, 2020 at 8:23 AM Paul Procacci 
> wrote:
> > >
> > > So two example patterns are:
> > >
> > >
> [\\u0009\\u000A\\u000D\\u0020-\\u007E\\u0085\\u00A0-\\uD7FF\\uE000-\\uFFFD\\u1-\\u10]*
> > > [\\p{L}\\p{Z}\\p{N}_.:\\/=+\\-@]*
> > >
> > > To note, the RE's themselves cannot be changed as they are fed
> externally.
> > > Given that I'm stuck with these RE's which I believe are PCRE, It was
> my hopes to lean on perl to do the evaluation.
> > > Raku's perl regex engine is too old to interpret it properly, hence
> the shenanigans with Inline::Perl5.
>
> —snip—
>
> This came out of the San Francisco Raku study group, just now.
>
> # This part is the set-up to allow interpolation of the P5 regex into a
> callable sub.
> use Inline::Perl5;
> use MONKEY-SEE-NO-EVAL;
> my $regex_string = '\w'; # In reality, this comes from a file!
>
> # Returns 1e0 when True and Nil when False.
> my $is_valid_sub = Inline::Perl5.new.run("
> sub \{
> return \$_[0] =~ /$regex_string/;
> \}
> ");
> # Wrapper to allow naming the sub (instead of anonymous sub in a variable),
> # and forces the return values to be the clearer Bool::True and
> Bool::False.
> sub is-valid ( $candidate --> Bool ) {
> return ? $is_valid_sub.($candidate);
> }
>
> # Test what we have so far.
> say is-valid('abc').raku;
> say is-valid('^').raku;
>
> # You don't really need a subtype
> multi sub real-use-of-where ( $var1 where { is-valid($^var1) } ) {
> say "This is definitely valid: $var1";
> }
> multi sub real-use-of-where ( $var1 ) {
> say "Not valid: $var1";
> }
> # Test it.
> real-use-of-where('abc');
> real-use-of-where('^');
>
> # Now make the subset, and use it as a type. The `where` is no longer
> needed.
> subset test-set of Str where *.&is-valid;
> sub real-use-of-subsets ( test-set $var1 ) {
> say "This is definitely a `test-set`: $var1";
> }
> sub real-use-of-subsets ( test-set $var1 ) {
> say "Not a `test-set`: $var1";
> }
> # Test it.
> real-use-of-subsets('abc');
> real-use-of-subsets('^’);
>
> —
> Hope this helps,
> Bruce Gray (Util of PerlMonks)
>
>

-- 
__

:(){ :|:& };:


Re: Subset w/ Inline::Perl5 RE as constraint

2020-11-08 Thread Paul Procacci
Thank you Joseph,

Your response along with Bruce's response (which I'll respond to
separately) I presume works.
My hopes of fitting this into a one liner are crushed!   lol  Nah, just
playing.

Thank you for taking the time to respond.
This certainly helps with my project.

~Paul

On Sun, Nov 8, 2020 at 6:32 PM Joseph Brenner  wrote:

> I think this kind of thing does what you're after:
>
> use Inline::Perl5;
> my $p5 = Inline::Perl5.new;
>
> my $p5pat = '\w+';
> $p5.run( 'sub chk { $_[0] =~ m/' ~ $p5pat ~ '/ }' );
> subset p5_words of Str where { $p5.call( "chk", $^a ) };
>
> my p5_words $a = "alpha";
> say $a; # alpha,  perl5 word chars, so acceptable to subset
> my p5_words $b = "^";  # Type check failed in assignment to $b;
> expected str_p5 but got Str ("^")
> # Errors out because carets don't pass \w check
>
>
> It could probably be cleaned up a little more, but it works.
>
>
> On 11/5/20, Paul Procacci  wrote:
> > https://github.com/niner/Inline-Perl5
> >
> > use Inline::Perl5;
> >
> > subset test of Str where EVAL "sub {@_[0] ~= m/\w+/}", :lang;
> >
> >
> > Question:  Can you pass whatever {*} into eval for use in Inline::Perl5 a
> > la subset?
> > The above example is incomplete, I understand, however I'm looking to
> find
> > a method of constraining Str's w/ >perl5.8 RE's in subset's without it
> > getting too crazy.
> >
> > The RE's that come w/ raku proper are are much older version of RE and
> > cannot accomplish what I need.
> >
> > Thanks in Advance,
> > ~Paul
> >
> > --
> > __
> >
> > :(){ :|:& };:
> >
>


-- 
__

:(){ :|:& };:


Re: Subset w/ Inline::Perl5 RE as constraint

2020-11-06 Thread Paul Procacci
Hi Bill,

Yes, that is exactly what I'm saying.

https://github.com/rakudo/rakudo/issues/2624

" FWIW, the :P5 supports a Perl 5 like syntax from X versions ago (probably
about 5.8, I would say)."

The features I need are in perl 5.10 which the :P5 adverb doesn't provide.

Thanks,
Paul

On Fri, Nov 6, 2020 at 11:47 AM William Michels 
wrote:

> Hi Paul,
>
> I'm sorry, I don't understand.
>
> Are you saying you're using Inline::Perl5 because you can't use the
> "Perl compatibility adverb" :Perl5 or :P5 ?
>
> https://docs.raku.org/language/regexes#Perl_compatibility_adverb
>
> Is that what you mean when you say the "perl regex engine [in Raku] is
> too old" ?
>
> Thanks, Bill.
>
>
>
>
>
> On Fri, Nov 6, 2020 at 8:23 AM Paul Procacci  wrote:
> >
> > So two example patterns are:
> >
> >
> [\\u0009\\u000A\\u000D\\u0020-\\u007E\\u0085\\u00A0-\\uD7FF\\uE000-\\uFFFD\\u1-\\u10]*
> > [\\p{L}\\p{Z}\\p{N}_.:\\/=+\\-@]*
> >
> > To note, the RE's themselves cannot be changed as they are fed
> externally.
> > Given that I'm stuck with these RE's which I believe are PCRE, It was my
> hopes to lean on perl to do the evaluation.
> > Raku's perl regex engine is too old to interpret it properly, hence the
> shenanigans with Inline::Perl5.
> >
> > Thanks,
> > Paul
> >
> >
> > On Fri, Nov 6, 2020 at 9:51 AM Parrot Raiser <1parr...@gmail.com> wrote:
> >>
> >> Can you provide some samples of what you are trying to match and
> >> exclude? There might be alternative solutions.
> >
> >
> >
> > --
> > __
> >
> > :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Subset w/ Inline::Perl5 RE as constraint

2020-11-06 Thread Paul Procacci
So two example patterns are:

[\\u0009\\u000A\\u000D\\u0020-\\u007E\\u0085\\u00A0-\\uD7FF\\uE000-\\uFFFD\\u1-\\u10]*
[\\p{L}\\p{Z}\\p{N}_.:\\/=+\\-@]*

To note, the RE's themselves cannot be changed as they are fed externally.
Given that I'm stuck with these RE's which I believe are PCRE, It was my
hopes to lean on perl to do the evaluation.
Raku's perl regex engine is too old to interpret it properly, hence the
shenanigans with Inline::Perl5.

Thanks,
Paul


On Fri, Nov 6, 2020 at 9:51 AM Parrot Raiser <1parr...@gmail.com> wrote:

> Can you provide some samples of what you are trying to match and
> exclude? There might be alternative solutions.
>


-- 
__

:(){ :|:& };:


Subset w/ Inline::Perl5 RE as constraint

2020-11-05 Thread Paul Procacci
https://github.com/niner/Inline-Perl5

use Inline::Perl5;

subset test of Str where EVAL "sub {@_[0] ~= m/\w+/}", :lang;


Question:  Can you pass whatever {*} into eval for use in Inline::Perl5 a
la subset?
The above example is incomplete, I understand, however I'm looking to find
a method of constraining Str's w/ >perl5.8 RE's in subset's without it
getting too crazy.

The RE's that come w/ raku proper are are much older version of RE and
cannot accomplish what I need.

Thanks in Advance,
~Paul

-- 
__

:(){ :|:& };:


Re: Metamodel parent and return values ...

2020-10-30 Thread Paul Procacci
Well I feel dumb.  I haven't tried it yet, but that makes sense.  It's what
I get for working on this in the w hours of the morning.

Thanks,
Paul

On Fri, Oct 30, 2020 at 7:48 AM Timo Paulssen  wrote:

> On 30/10/2020 07:58, Paul Procacci wrote:
> > Here is what I have ... trimmed.
> >
> > #
> > use JSON::Pretty;
> > use Staticish;
> >
> > unit class MyClass is Static;
> >
> > method client(::?CLASS:D: Str:D $service! --> MyClass)
> > {
> > my %data := from-json %?RESOURCES{$service}.slurp;
> > my $type = Metamodel::ClassHOW.new_type(
> > name => $service,
> > ver => v0.0.1,
> > auth => 'github:me'
> > );
> >
> > $type.HOW.add_parent($type, 'MyClass');
> > $type.HOW.compose($type);
> > $type.new;
> > }
> > #
>
>
> Hi Paul,
>
> I think when you're passing a string to add_parent, you're actually
> setting the class Str as the parent. raku will not go looking for a
> class with that name. Instead, it expects you to pass the actual type
> object.
>
> Hope that helps!
>- Timo
>


-- 
__

:(){ :|:& };:


Metamodel parent and return values ...

2020-10-30 Thread Paul Procacci
Defining classes outside of raku 

I'm designing a program that would be easiest to have classes created at
runtime.
I've been reading the following page:

https://docs.raku.org/type/Metamodel::ClassHOW

While I believe creating the class itself is working, I'm unsure where it's
documented about setting the parent.

Here is what I have ... trimmed.

#
use JSON::Pretty;
use Staticish;

unit class MyClass is Static;

method client(::?CLASS:D: Str:D $service! --> MyClass)
{
my %data := from-json %?RESOURCES{$service}.slurp;
my $type = Metamodel::ClassHOW.new_type(
name => $service,
ver => v0.0.1,
auth => 'github:me'
);

$type.HOW.add_parent($type, 'MyClass');
$type.HOW.compose($type);
$type.new;
}
#

Now when I try running the above, I get:

Type check failed for return value; expected MyClass but got SomeService
("")

Is add_parent in this context not doing what I expect it to do?
I should rephrase that question.  I know it's not doing what I expect it to
do, but is this the right method?
... if it is, returning a class which is a child of another class should
satisfy the return value requirements and I would expect this to work.

Any pointers in the right direction are much appreciated.

Thanks in advance,
Paul Procacci
-- 
__

:(){ :|:& };:


Re: lines :$nl-in question

2020-08-28 Thread Paul Procacci
:nl-in is a named parameter that defines what the method lines would
consider as line endings.
It defines  "\x0A", "\r\n" as the default.

Example:
 % echo "Hi, Frank." > test.txt ; echo "What's up?" >> test.txt ; echo
'"test.txt".IO.lines(:nl-in).say' > test.pl6 ; perl6 ./test.pl6
(Hi, Fr nk.
Wh t's up?
)

|c slurps the remaining arguments into c and passese those arguments to the
lines method of IO::Handle.

On Fri, Aug 28, 2020 at 9:18 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> In the following:
>
> https://docs.raku.org/type/IO::Path#method_lines
>
> (IO::Path) method lines
>
> Defined as:
>
> method lines(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in =
> ["\x0A", "\r\n"], |c --> Seq:D)
>
> Opens the invocant and returns its lines.
>
> The behavior is equivalent to opening the file specified
> by the invocant, forwarding the :$chomp, :$enc,
> and :$nl-in arguments to IO::Handle.open, then calling
> IO::Handle.lines on that handle, forwarding any of the
> remaining arguments to that method, and returning the
> resultant Seq.
>
> What exactly is and how exactly do you use `:$nl-in`
>
> And if I am not pushing it, what is `|c`?
>
> I am confused,
> -T
>


-- 
__

:(){ :|:& };:


Re: I need help with IO.e

2020-05-17 Thread Paul Procacci
Don't 'say' anything.  Just let the optimizer spit out the QAST that you
are interested in looking at.
The following spits out a diff after optimization:

# diff -u <(perl6 --target=optimize -e '"test".IO.e') <(perl6
--target=optimize -e '"test".IO.e.Bool')

>> Huh.  Not sure what I am looking at

You aren't specific.  Is it the error message you received because you used
'say' improperly or is it the QAST?
- How to use 'say' is in the documentation.
- A QAST is pretty close to an AST and I'd start there on wikipedia or
something.

On Mon, May 18, 2020 at 1:03 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-05-17 21:48, Paul Procacci wrote:
> > You can check this yourself by looking at the QAST nodes after the
> > static analyzer has had its fill:
> >
> > # diff -u <(perl6 --target=optimize -e '"test".IO.e') <(perl6
> > --target=optimize -e '"test".IO.e.Bool')
>
> Huh.  Not sure what I am looking at
>
>
> $ diff -u <(perl6 --target=optimize -e 'say if "test".IO.d') <(perl6
> --target=optimize -e 'say "test".IO.d.Bool')
> ===SORRY!=== Error while compiling -e
> Unsupported use of bare "say"; in Raku please use .say if you meant to
> call it as a method on $_, or use an explicit invocant or argument, or
> use &say to refer to the function as a noun
> at -e:1
> --> say⏏ if "test".IO.d
> --- /dev/fd/63  2020-05-17 22:01:37.947790677 -0700
> +++ /dev/fd/62  2020-05-17 22:01:37.948790659 -0700
> @@ -0,0 +1,98 @@
> +- QAST::CompUnit  :W :UNIT :CAN_LOWER_TOPIC
> +  [pre_deserialize]
> +- QAST::Stmt
> +  - QAST::Stmt
> +- QAST::Op(loadbytecode)
> +  - QAST::VM
> +[moar]
> +  - QAST::SVal(ModuleLoader.moarvm)
> +[jvm]
> +  - QAST::SVal(ModuleLoader.class)
> +[js]
> +  - QAST::SVal(ModuleLoader)
> +- QAST::Op(callmethod load_module)
> +  - QAST::Op(gethllsym)
> +- QAST::SVal(nqp)
> +- QAST::SVal(ModuleLoader)
> +  - QAST::SVal(Perl6::ModuleLoader)
> +  - QAST::Op(forceouterctx)
> +- QAST::BVal(2)
> +- QAST::Op(callmethod load_setting)
> +  - QAST::Op(getcurhllsym)
> +- QAST::SVal(ModuleLoader)
> +  - QAST::SVal(CORE.d)
> +  [post_deserialize]
> +- QAST::Stmts
> +  - QAST::Op(bind)
> +- QAST::Var(attribute $!do)
> +  - QAST::WVal(Block)
> +  - QAST::WVal(Code)
> +- QAST::BVal(1)
> +- QAST::Op(bindcurhllsym)
> +  - QAST::SVal(GLOBAL)
> +  - QAST::WVal(GLOBAL)
> +  [load]
> +- QAST::Op(call)
> +  - QAST::BVal(2)
> +  [children]
> +- QAST::Block(:cuid(2))  :in_stmt_mod say \"test\".IO.d.Bool
> +│ - QAST::Var(local __args__ :decl(param))
> +│ - QAST::Stmts  say \"test\".IO.d.Bool
> +│ - QAST::Op(call)
> +│   - QAST::Block(:cuid(1) :blocktype(declaration_static))
> :outer :in_stmt_mod :code_object :IN_DECL
> +│   │ - QAST::Stmts  say \"test\".IO.d.Bool
> +│   │   - QAST::Var(lexical $¢ :decl(contvar))
> +│   │   - QAST::Var(lexical $! :decl(contvar))
> +│   │   - QAST::Var(lexical $/ :decl(contvar))
> +│   │   - QAST::Op(null)
> +│   │   - QAST::Var(lexical GLOBALish :decl(static))
> +│   │   - QAST::Var(lexical EXPORT :decl(static))
> +│   │   - QAST::Var(lexical $?PACKAGE :decl(static))
> +│   │   - QAST::Var(lexical ::?PACKAGE :decl(static))
> +│   │   - QAST::Var(lexical $=finish :decl(static))
> +│   │   - QAST::Var(lexical $=pod :decl(static))
> +│   │ [value]
> +│   │   -
> +│   │   - QAST::Var(lexical !UNIT_MARKER :decl(static))
> +│   │ - QAST::Stmts
> +│   │   - QAST::Op(bind)
> +│   │ - QAST::Var(local ctxsave :decl(var))
> +│   │ - QAST::Var(contextual $*CTXSAVE)
> +│   │   - QAST::Op(unless)
> +│   │ - QAST::Op(isnull)
> +│   │   - QAST::Var(local ctxsave)
> +│   │ - QAST::Op(if)
> +│   │   - QAST::Op(can)
> +│   │ - QAST::Var(local ctxsave)
> +│   │ - QAST::SVal(ctxsave)
> +│   │   - QAST::Op(callmethod ctxsave)
> +│   │ - QAST::Var(local ctxsave)
> +│   │ - QAST::Stmts
> +│   │   - QAST::WVal(Array)
> +│   │   - QAST::Stmt  say \"test\".IO.d.Bool
> +│   │ - QAST::Want 
> +│   │   - QAST::Op(callstatic &say)  :statement_id<1> say
> \"

Re: I need help with IO.e

2020-05-17 Thread Paul Procacci
You can check this yourself by looking at the QAST nodes after the static
analyzer has had its fill:

# diff -u <(perl6 --target=optimize -e '"test".IO.e') <(perl6
--target=optimize -e '"test".IO.e.Bool')

On Mon, May 18, 2020 at 12:25 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-05-17 21:19, ToddAndMargo via perl6-users wrote:
> > On 2020-05-17 20:28, Paul Procacci wrote:
> >> So tack a .Bool at the end.
> >> You are coercing a bool to a bool by doing so and hopefully the
> >> optimizer is smart enough for people who like to be redundant.
> >>
> >> ;)
> >
> > Hi Paul,
> >
> > Especially when I can never remember when IO.someletter
> > will return a full True or False Boolean and when it
> > will return a True or a useless text message.
> >
> > The optimizer should have a lot of fun with me!  Works
> > darned well too!
> >
> > -T
>
> Speaking of optimizer, it would be interesting to see
> if my coercing a half a Bool and a useless text message
> to a True of False when analyzed takes  more of less
> optimizing than giving it a straight Boolean to start with.
>
> But now I are getting at some really deep trivia
>


-- 
__

:(){ :|:& };:


Re: I need help with IO.e

2020-05-17 Thread Paul Procacci
So tack a .Bool at the end.
You are coercing a bool to a bool by doing so and hopefully the optimizer
is smart enough for people who like to be redundant.

;)

On Sun, May 17, 2020 at 6:10 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-05-17 14:43, ToddAndMargo via perl6-users wrote:
> > Although
> >
> > "say 'yes' if 'h:/'.IO.d"
> >
> > is obscure to me and
> >
> > "say 'h:/'.IO.d.Bool"
> >
> > is very easy for me to understand.
>
>
> Hi Peter,
>
> I think it would help if you knew how my
> mind worked.
>
> `if` to me is a Boolean function.  If this
> expressions is true, do this, if not, do
> something else.
>
> This is what happens to me when I see expressions
> like
>
>  "say 'yes' if 'h:/'.IO.d"
>
> if the expression (h:/.IO.d) is true, then
> say the result of the expression test, not the
> result from the expression.  I have to figure
> out what the results from the expression are
> and why it is passing or failing the test.
>
> Two steps in my mind.  Not that I can't figure
> things like this out, I just don't like the effort,
> when it can be avoided.  The way I look at it, the
> time it takes me to write five extra letters (.Bool)
> will save me ten times that amount of time trying to
> figure out what I did 5 days or 5 years from now.
> Dr. Demming (Quality circles, Kaisen, etc.) makes
> a very strong point of this
>
> And `if` can actually be very helpful at times:
> if  $x ~~ s/ "abc" /def/ {...;}
> will tell you if the regex actually found "abc".
> This I adore because I know is a test.  Not
> in the least bit obscure.
>
> .d is suppose to return a Boolen, but does not.
> Some functions will convert the junk for you on
> the fly.  Raku is very friendly this way.  Five extra
> letters and I do not have to wonder who does what
> to whom.
>
> Yes, I am a bit weird.
>
> Thank you for all the wonderful explanations!  Very
> much appreciated.
>
> -T
>


-- 
__

:(){ :|:& };:


Re: I need help with the copy command

2020-05-01 Thread Paul Procacci
$createonly fails to do a copy when the destination file already exists.
An example of shell commands to emulate this is:

# echo a > test1
# echo b > test2
# cp -n test2 test1
# cat test1
a

As for recursion...you probably need to write a recursive function
somewhere (didn't look that hard).

On Fri, May 1, 2020 at 11:40 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> https://docs.perl6.org/routine/copy
>
>  (IO::Path) routine copy
>
>  Defined as:
>
>  method copy(IO::Path:D: IO() $to, :$createonly --> Bool:D)
>  subcopy(IO() $from, IO() $to, :$createonly --> Bool:D)
>
>  Copies a file. Returns True on success; fails with
>  X::IO::Copy if :$createonly is True and the $to path
>  already exists or if the operation failed for some
>  other reason, such as when $to and $from are the same
>  file.
>
> Huh?
>
> 1) what exactly is `:$createonly`?
>
> 2) is there some other command for recurse and create?
>
> cp -Rip source destination
>
>
> Many thanks,
> -T
>


-- 
__

:(){ :|:& };:


Re: sleep 3600 vs task scheduler

2020-04-07 Thread Paul Procacci
What happens when you try it?
What impact do you observe?

My guess is the impact is exactly the time it takes for your cpu to perform
the initial context switch for the syscall, and then another when waking up.

On Tue, Apr 7, 2020 at 10:28 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> >> On Tue, Apr 7, 2020 at 6:02 AM ToddAndMargo via perl6-users
> >> mailto:perl6-users@perl.org>> wrote:
> >>
> >> Hi All,
> >>
> >> Windows 7/10
> >>
> >> Another piece of the puzzle.
> >>
> >> I want to loop Raku program once an hour.
> >>
> >> Is it better use `sleep 3600` or let the program
> >> die and restart every hour from the Task Scheduler.
> >> By better, I mean less of a CPU footprint.
> >>
> >> `sleep` would allow the user to cancel the program
> >> and not have it come back.
> >>
> >> Many thanks,
> >> -T
> >>
>
> On 2020-04-07 19:22, Brad Gilbert wrote:
> > Run code once an hour:
> >
> >  react whenever Supply.interval(60 * 60) {
> >  say "it's been an hour"
> >  }
> >
> > Right now that gets about 0.01 seconds slower every time the interval
> runs.
> > (At least on my computer.)
> > So it will get 1 second later every 4 days.
> >
> >
> > Or if you want more precise control, you could do something like:
> >
> > Here is an example that is more accurate, and happens at the top of the
> > hour:
> >
> >  sub every-hour ( --> Supply:D ) {
> >  supply {
> >  sub add-next ( $hour --> Nil ) {
> >  whenever Promise.at( $hour.Instant ) {
> >  emit $hour;
> >  add-next( $hour.later(:1hour) );
> >  }
> >  }
> >
> >  add-next( DateTime.now.truncated-to('hour').later( :1hour )
> );
> >  }
> >  }
> >
> >
> >  react whenever every-hour() {
> >  say "it's been an hour";
> >  say "the time is $_"
> >  }
> >
> >
> >
> > Honestly it would probably be better to use something native to the
> > system to run it once an hour, in case the program dies.
> >
> >
>
> Thank you!
>
> How much impact CPU usage wise does sleep and/or react
> have on the system?
>
> My fear is I will tie up a core with my code
>


-- 
__

:(){ :|:& };:


Re: Can a sub be released?

2020-04-07 Thread Paul Procacci
https://docs.perl6.org/language/5to6-perlfunc#fork
https://docs.perl6.org/type/Thread

I haven't tried myself but it's conceivable that you can start a new thread
that exec's some external program.

On Tue, Apr 7, 2020 at 7:21 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> Can a subroutine be released from the main program
> to go off on its own?  (Is this called "forked"?)
>
> If not how do I do a `qqx` or a `run` that releases
> the program to go off on its own?
>
> Many thanks,
> -T
>


-- 
__

:(){ :|:& };:


Re: qqx with quotes

2020-02-25 Thread Paul Procacci
Perl6 version 2019.03

On Tue, Feb 25, 2020 at 12:12 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> >> On Mon, Feb 24, 2020 at 4:01 PM ToddAndMargo via perl6-users
> >> mailto:perl6-users@perl.org>> wrote:
> >>
> >> Hi All,
> >>
> >> Windows 7
> >>
> >> In the following
> >>
> >> @Result = qqx { C:/Windows/System32/fsutil.exe usn readdata
> >> \"$FileName\" }.lines;
> >>
> >>
> >> $FileName needs to be in quotes as it can have
> >> spaces in it.
> >>
> >>
> >> The following did not work:
> >>
> >> \"$FileName\"
> >> "$FileName\
> >> $FileName
> >>
> >> What am I doing wrong, this time?
> >>
> >> Many thanks,
> >> -T
>
> On 2020-02-25 08:29, Paul Procacci wrote:
> > The following works on FreeBSD:
> >
> > my $fn = "file name";
> > say qqx { ls -l "$fn" };
> >
> > Not sure why this would be any different on Windows.
>
> What version are you running?
>


-- 
__

:(){ :|:& };:


Re: qqx with quotes

2020-02-25 Thread Paul Procacci
The following works on FreeBSD:

my $fn = "file name";
say qqx { ls -l "$fn" };

Not sure why this would be any different on Windows.

On Mon, Feb 24, 2020 at 4:01 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> Windows 7
>
> In the following
>
> @Result = qqx { C:/Windows/System32/fsutil.exe usn readdata
> \"$FileName\" }.lines;
>
>
> $FileName needs to be in quotes as it can have
> spaces in it.
>
>
> The following did not work:
>
> \"$FileName\"
> "$FileName\
> $FileName
>
> What am I doing wrong, this time?
>
> Many thanks,
> -T
>
>
>
> --
> ~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~
>


-- 
__

:(){ :|:& };:


Re: irrational nubmer?

2020-02-20 Thread Paul Procacci
Every system that uses a fixed finite number of bits to represent numbers
has to represent them as implicit rationals...that is
unless it goes through the trouble of having a finite list of irrational
constants that it represented specially.

sqrt is not equivalent to the mathematical definition of √.  It's an
approximation of the latter.
Most computer languages except for CAS work this way.

Truth by told, a lot of what I'm saying is general knowledge and shouldn't
be news to anyone.
Somewhat kiddingly, Raku should introduce the LazilyComputableReal datatype
which returns True to maybe_irrational ... until it isn't.  ;)


On Thu, Feb 20, 2020 at 2:21 AM Shlomi Fish  wrote:

> Hi Paul,
>
> On Thu, 20 Feb 2020 00:22:34 -0500
> Paul Procacci  wrote:
>
> > If you wouldn't mind, please stop referring things as being "magical".
> > There's nothing magical about Raku/Perl6 other than the devs that put in
> > their time to give you that perception.
> > They are to be commended for their time and effort.
> >
> > Also, being condescending as in "he gave up" is uncalled for.
> >
> > It's an IMPOSSIBILITY using today's technology to tell you whether a
> number
> > is irrationalperiod.
>
> Well, it is not unthinkable that a
> https://en.wikipedia.org/wiki/Computer_algebra_system (CAS)-like system
> will be
> able to tell that the abstract number sqrt(2) is irrational, as well as
> some
> derivative numbers such as 3 + sqrt(2). E.g:
>
> ```
> > my $num = sqrt(2);
> > say $num.is_irrational()
> True
> ```
>
> It won't be able to give its exact value, but may still be able to reason
> about
> it.
>
> --
>
> Shlomi Fish   https://www.shlomifish.org/
> Perl Elems to Avoid - https://perl-begin.org/tutorials/bad-elements/
>
> The cool thing about Vim is — you find something interesting with every
> typo.
> — Su‐Shee on Freenode’s #perl .
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>


-- 
__

:(){ :|:& };:


Re: irrational nubmer?

2020-02-19 Thread Paul Procacci
If you wouldn't mind, please stop referring things as being "magical".
There's nothing magical about Raku/Perl6 other than the devs that put in
their time to give you that perception.
They are to be commended for their time and effort.

Also, being condescending as in "he gave up" is uncalled for.

It's an IMPOSSIBILITY using today's technology to tell you whether a number
is irrationalperiod.

The reason why a Raku/Perl6 Int can perform operations on numbers greater
than the cpu architecture allows
is because that single addition operator you use is actually more than just
a single addition.  You said you were
familiar with assembly language so let me give it to you that way.
Perhaps that will allow you to understand it better.

###
SECTION .data
mem: DQ 0
 DQ 0x7FFF

SECTION .text
add:
mov rax, qword [mem + 8]
add rax, 1
jno .ok
mov qword [mem + 8], 0
inc qword [mem]
.ok:
###

There's obviously more involved than this small snippet, but it shows how
storing values greater than 64-bits (in my case) is possible.
No magic needed, just a bit of comp-sci knowledge.  Obviously I'm leaving
out a display routine but that'd be for you to finish.  ;)
Also, the above is limited to a 128 bit value but with a proper allocator
the sky is the limit (unless you run out of memory).

Here's an experiment 

Write a program to tell you whether PI is irrational.
Respond once it returns with your answer.
A forewarning however, your response will fall on deaf ears as I'll be long
gone as will all others receiving this message.

On Wed, Feb 19, 2020 at 11:15 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> >> On Wed, Feb 19, 2020 at 9:58 PM ToddAndMargo via perl6-users
> >> mailto:perl6-users@perl.org>> wrote:
> >>
> >> Hi All,
> >>
> >> This is a complete trivia question.
> >>
> >> Is there a test to see if a number is irrational,
> >> such as the square root of two?
> >>
> >> And how does Int handle a irrational number?  Is
> >> there a limit to magic Larry powder?
> >>
> >> Many thanks,
> >> -T
> >
>
> On 2020-02-19 19:20, Paul Procacci wrote:
> >  >> Is there a test to see if a number is irrational
> > There is no such thing as an irrational number in computing.
> >
> > Surely there are "close approximations", but that's the best any
> > computer language can currently do.
> >
>
> Hi Paul,
>
> $ perl6 -e 'my num $x=sqrt(2); say $x;'
> 1.4142135623730951
>
> No Magic Larry "Num" that has no set length?
> And I thought Larry was unstoppable!  Guess
> he gave up at UInt and Int.
>
> :-)
>
> -T
>


-- 
__

:(){ :|:& };:


Re: irrational nubmer?

2020-02-19 Thread Paul Procacci
 >> Is there a test to see if a number is irrational
There is no such thing as an irrational number in computing.

Surely there are "close approximations", but that's the best any computer
language can currently do.

On Wed, Feb 19, 2020 at 9:58 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> This is a complete trivia question.
>
> Is there a test to see if a number is irrational,
> such as the square root of two?
>
> And how does Int handle a irrational number?  Is
> there a limit to magic Larry powder?
>
> Many thanks,
> -T
>


-- 
__

:(){ :|:& };:


Re: PCRE -> Perl 6 RE

2020-02-18 Thread Paul Procacci
Right,

I'm going with bug.

https://github.com/rakudo/rakudo/issues/2624

It looks like there's some discussion about dropping Perl5 regex support
from the engine.
I didn't want to translate this by hand as there's too many regexes within
this source file to do so by hand (I lean on the Perl5 adverb) ... and was
hoping someone get frustrated enough w/ Raku's perl5 regex implementation
that rolled their own.  ;(

Thanks Brad,
Paul

On Tue, Feb 18, 2020 at 11:56 PM Brad Gilbert  wrote:

> The \p{L} syntax is done by using :L inside of <> instead
>
> /\p{L}/
> /<:L>/
>
> You can combine them
>
> /[\p{L}\p{Z}\p{N}]/
> /<:L + :Z + :N>/
>
> Character classes are also done inside of <>
>
> /[_.:/=+\-@]/
> /<[_.:/=+\-@]>/
>
> They of course can also be combined with the previous discussed feature.
>
> /[\p{L}\p{Z}\p{N}_.:/=+\-@]+/
> /<:L + :Z + :N + [_.:/=+\-@] >+/
>
> So that is the translation of your regex.
>
> ---
>
> It might be considered a bug that you can't just use :P5, as your regex
> works just fine in Perl5.
>
> On Tue, Feb 18, 2020 at 10:03 PM Paul Procacci 
> wrote:
>
>> Hey Guys,
>>
>> I've got a bunch of source files with PCRE's like the following:
>>
>> [\p{L}\p{Z}\p{N}_.:/=+\-@]+
>>
>> I have a program that generates other perl6 source files, by way of
>> reading the initial source files.
>> It was my intention to simply pass along this regex to the resulting
>> output file for use in subset's like the following:
>>
>> subset ThisType of Str where * ~~ m:P5/[\p{L}\p{Z}\p{N}_.:/=+\-@]+/;
>>
>> I was going to at first hope add a PCRE adverb of sorts but that doesn't
>> exist.
>> I then was going to stick the Perl5 adverb on it ... but again no luck as
>> it pukes on the escaped p's.
>>
>> Is there anything smart enough to translate these tokens into either:
>>
>> a) perl5 syntax for use with the Perl5 adverb or
>> b) perl6 syntax for use with built-in perl6 re's.
>> c) A module that anyone's familiar with that isn't publish or
>> d) I'm simply SOL?
>>
>> Thanks,
>> Paul
>>
>> --
>> __
>>
>> :(){ :|:& };:
>>
>

-- 
__

:(){ :|:& };:


PCRE -> Perl 6 RE

2020-02-18 Thread Paul Procacci
Hey Guys,

I've got a bunch of source files with PCRE's like the following:

[\p{L}\p{Z}\p{N}_.:/=+\-@]+

I have a program that generates other perl6 source files, by way of reading
the initial source files.
It was my intention to simply pass along this regex to the resulting output
file for use in subset's like the following:

subset ThisType of Str where * ~~ m:P5/[\p{L}\p{Z}\p{N}_.:/=+\-@]+/;

I was going to at first hope add a PCRE adverb of sorts but that doesn't
exist.
I then was going to stick the Perl5 adverb on it ... but again no luck as
it pukes on the escaped p's.

Is there anything smart enough to translate these tokens into either:

a) perl5 syntax for use with the Perl5 adverb or
b) perl6 syntax for use with built-in perl6 re's.
c) A module that anyone's familiar with that isn't publish or
d) I'm simply SOL?

Thanks,
Paul

-- 
__

:(){ :|:& };:


Re: Substr behaviour with CRLF

2020-02-10 Thread Paul Procacci
Unicode conformance requires "\r\n" to be interpreted as \n alone.
With that said; no, I don't not know how to turn this off.

I personally think I'd consider this a bug.  If not a bug, greater
documentation efforts that explain this.
The display routines (say / print) don't modify the string on output, yet
the other string handling routines do.

We'd need further clarification from the devs as I don't have a full grasp
of the design decision for this problem.

On Mon, Feb 10, 2020 at 11:28 AM David Santiago  wrote:

> A 10 de fevereiro de 2020 16:57:55 CET, David Santiago 
> escreveu:
> >
> >
> >Hi!
> >
> >Is there a way to change the the following behaviour, so it considers
> \r\n as two characters when using substr, instead of one?
> >
> >On raku version 2019.11
> >
> >> "1234\r\n". substr(*-4)
> >4
> >78
> >> "1234\r\n". substr(*-4).ords()
> >(52 13 10 55 56)
> >
> >
> >Best regards,
> >David Santiago
> >
>
> Copied wrong the example:
>
> It should be:
>
> On raku version 2019.11
>
> > "1234\r\n78". substr(*-4)
> 4
> 78
> > "1234\r\n78". substr(*-4).ords()
> (52 13 10 55 56)
>
>
>
> --
> Sent from my Android device with K-9 Mail. Please excuse my brevity.
>


-- 
__

:(){ :|:& };:


Re: printf question

2020-02-10 Thread Paul Procacci
Thanks Timo,

I was, in part, aware of this, but didn't have the full knowledge/details
as you've explained it.

Thanks!

On Mon, Feb 10, 2020 at 6:18 AM Timo Paulssen  wrote:

> Hi Paul and Todd,
>
> just a little extra info: the limitation for nameds to come after
> positionals is only for declarations of signatures.
>
> Usage of subs/methods as well as capture literals (which you don't use
> often, i imagine, so feel free to disregard) allow you to mix nameds and
> positionals freely; it will handle named parameters that are put between
> positionals as if they were after the positional parameters.
>
> > sub abcdefg($b, $f, $g, :$a, :$c, :$e) { say $a, $b, $c, $e }
> &abcdefg
> > abcdefg(1, a => 5, 2, c => 99, 100, e => 1024)
> 51991024
>
> Most cases where I wanted named parameters early in the call was when
> there was something big in the call, for example if a sub takes a block and
> a few options, i prefer to put the options before the block, so they are
> visible at a glance rather than after scrolling. I suppose this mirrors how
> regex modifiers (like :ignorecase / :i, :global, etc) have been moved to
> the front of regexes.
>
> Hope that's interesting
>   - Timo
> On 10/02/2020 07:48, Paul Procacci wrote:
>
> Named parameters must come after all positional parameters.
> Your example subroutine is invalid for this reason, while the following
> would be fine:
>
> sub abcdefg( $b, $f, $g, :$a, :$c, :$e)
>
> abcdefg("position1", "position2", "position3", :e("named_e"),
> :a("named_a"), :c("named_c"));
>
>
>
> On Sun, Feb 9, 2020 at 6:24 PM ToddAndMargo via perl6-users <
> perl6-users@perl.org> wrote:
>
>> On 2020-02-09 14:53, Paul Procacci wrote:
>> > subchdir(IO() $path, :$d=True, :$r, :$w, :$x-->IO::Path:D)
>>
>> Hi Paul,
>>
>> What I wanted to see is how something liek
>>
>> sub abcdefg( :$a, $b, :$c, :$e, $f, $g )
>>
>> would be called
>>
>> -T
>>
>
>
> --
> __
>
> :(){ :|:& };:
>
>

-- 
__

:(){ :|:& };:


Re: printf question

2020-02-09 Thread Paul Procacci
Named parameters must come after all positional parameters.
Your example subroutine is invalid for this reason, while the following
would be fine:

sub abcdefg( $b, $f, $g, :$a, :$c, :$e)

abcdefg("position1", "position2", "position3", :e("named_e"),
:a("named_a"), :c("named_c"));



On Sun, Feb 9, 2020 at 6:24 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-02-09 14:53, Paul Procacci wrote:
> > subchdir(IO() $path, :$d=True, :$r, :$w, :$x-->IO::Path:D)
>
> Hi Paul,
>
> What I wanted to see is how something liek
>
> sub abcdefg( :$a, $b, :$c, :$e, $f, $g )
>
> would be called
>
> -T
>


-- 
__

:(){ :|:& };:


Re: printf question

2020-02-09 Thread Paul Procacci
I think it's best that I show you examples from the official documentation.
Let's use chdir as our example.

https://docs.raku.org/routine/chdir

chdir has the following signature:

sub chdir(IO() $path, :$d = True, :$r, :$w, :$x --> IO::Path:D)


So let's break this down.

$path :: This is a positional parameter.  You know it's positional because
of a lack of preceding colon (:)
:$d :: This is a named parameter w/ a default value of True
:$r :: This is a named parameter
:$w :: This is a named parameter
:$x :: This is a named parameter

So let''s try it!

# Just the positional parameter
chdir('/tmp');

# Both positional and named parameter ':d' set to False
chdir('/tmp', :d(False));

# Again, both positional and named, but this time with named parameters
':d' set to False and ':x' set to True.  ':x' is enough to set to True ..
you can alternatively do a ':x(True)'.
chdir('/tmp', :d(False), :x;

# Same as last one, though named parameter order doesn't matter.
chdir('/tmp', :x, :d(False));

Now quickly imagine you had a variable set to False like the following:

my $d = False;

I could instead have written the above examples like the following (in the
order the above was written):

chdir('/tmp');
chdir('/tmp', :$d);
chdir('/tmp', :$d, :x);
chdir('/tmp', :x, :$d);


If you desire another example, please let me know.  I believe given your
latest comment and this latest example, that would be enough.
If not, I'll happily provide another one.

~Paul

On Sun, Feb 9, 2020 at 5:20 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-02-08 15:39, Paul Procacci wrote:
> > sub a(:$a, :$b, :$c) {}
> >
> > a(:c(1), :a(0), :b(3));
>
> Hi Paul,
>
> I think I got it, but would yo give me one more exampale
> to make sure I fully understand?
>
> sub a(:$a, :$b, :$c) {}
>
> a(:c(1), :a(0), :b(3));
>
> But with two that are not named and two that are named.
> And what is the order?
>
>
> Many thanks,
> -T
>


-- 
__

:(){ :|:& };:


Re: printf question

2020-02-08 Thread Paul Procacci
Named parameters are an important part of the raku language.
In fact, named parameters are used in several other languages as well.
It's in your best interest to learn how they work in order to use the
language properly.

sub a (:$a) {}
    This is a named parameter.

You know it's a named parameter because of the preceding colon (:).
What this means is in order for caller to pass this parameter to this
subroutine, it's necessary for
the caller to call it using a named parameter as an argument.

Here are two examples that utilize the named parameter of the subroutine I
just created above:

##

my $var = 'test';
a(:a($var));
    The named parameter

##

my $a = 'test';
a(:$a));
    The named parameter

##

In the first example, the value I wanted to pass to the named parameter was
stored in '$var'.
The syntax for passing this value to the named parameter as defined in the
subroutine is :a($var) ...
This is saying, pass the value stored in $var to the named parameter that
the subroutine expects as $a.

In the second example, the value I wanted to pass to the named parameter
was stored in '$a'.
The syntax for passing this value to the named parameter as defined in the
subroutine is :a($a)  or if you like shorthand  :$a
This is saying, pass the value stored in $a to the named parameter that the
subroutine expects as $a.

What you are accustomed to are positional parameters like the following:

sub a($a, $b, $c) {}

Positional parameters have to be passed in order ...

a(1,2,3);

Named parameters have a feature that order doesn't matter:

sub a(:$a, :$b, :$c) {}

a(:c(1), :a(0), :b(3));

This is advantageous in certain cases.  One such example is passing a bunch
of booleans though it's not limited to booleans as I've demonstrated in my
original post.
Anyways, learn named parameters.  It's essential to the raku programming
language.


On Sat, Feb 8, 2020 at 12:52 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> >> On Thu, Feb 6, 2020 at 11:43 AM ToddAndMargo via perl6-users
> >> mailto:perl6-users@perl.org>> wrote:
> >>
> >> On 2020-02-05 20:12, Paul Procacci wrote:
> >>  > I wasn't going to follow up but decided to do so since there is a
> >> small
> >>  > but subtle bug in my original post.
> >>  > I wouldn't want to mislead you Todd.
> >>  >
> >>  > The \d has been changed to [0..9] as the expected input would
> >> only ever
> >>  > be in that range.  (\d includes Unicode Characters)
> >>  > I've also included an alignment parameter (shadow'ing the sub
> >> written by
> >>  > you Todd).
> >>  >
> >>  > sub sbprint( Int $n, Int :$alignment = 8) {
> >>  >  my Int $padding = $alignment + $n.msb - ( $n.msb +& (
> >>  > $alignment  - 1 ) );
> >>  >  '0b' ~ "\%0{$padding}b".sprintf($n).comb(/<[0..9]> ** {
> >>  > $alignment }/).join('_')
> >>  > }
> >>  >
> >>  > say sbprint 0x04F842;
> >>  > say sbprint 0x04F842, :alignment(4);
> >>  >
> >>  > # ./test.pl6
> >>  > 0b0100_1000_0110
> >>  > 0b0100__1000_0100_0010
> >>
> >>
> >> `Int :$alignment = 8` was inspired!
> >>
> >> What does the ":" do before `alignment`?
> >>
>
> On 2020-02-06 08:52, Paul Procacci wrote:
> > The subroutine I wrote defines a named parameter that goes by the name
> > alignment 'Int :$alignment'.
> > If the caller wants to call the callee using the given named parameter
> > there are several ways to so do   hence the ':alignment(4)'.
> >
> > If instead you have a variable already defined you can instead pass that
> > variable as a named parameter via something like the following:
> >
> > $alignment = 4;
> > sbprint 0x04F842, :$alignment;
> >
> >
> > The documentation goes into much more detail about named arguments.
> >
> > https://docs.raku.org/language/functions#Arguments
> >
>
> Sorry, went right over my head.  Thank you for trying anyway.
>


-- 
__

:(){ :|:& };:


Re: printf question

2020-02-06 Thread Paul Procacci
The subroutine I wrote defines a named parameter that goes by the name
alignment 'Int :$alignment'.
If the caller wants to call the callee using the given named parameter
there are several ways to so do   hence the ':alignment(4)'.

If instead you have a variable already defined you can instead pass that
variable as a named parameter via something like the following:

$alignment = 4;
sbprint 0x04F842, :$alignment;


The documentation goes into much more detail about named arguments.

https://docs.raku.org/language/functions#Arguments


On Thu, Feb 6, 2020 at 11:43 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-02-05 20:12, Paul Procacci wrote:
> > I wasn't going to follow up but decided to do so since there is a small
> > but subtle bug in my original post.
> > I wouldn't want to mislead you Todd.
> >
> > The \d has been changed to [0..9] as the expected input would only ever
> > be in that range.  (\d includes Unicode Characters)
> > I've also included an alignment parameter (shadow'ing the sub written by
> > you Todd).
> >
> > sub sbprint( Int $n, Int :$alignment = 8) {
> >  my Int $padding = $alignment + $n.msb - ( $n.msb +& (
> > $alignment  - 1 ) );
> >  '0b' ~ "\%0{$padding}b".sprintf($n).comb(/<[0..9]> ** {
> > $alignment }/).join('_')
> > }
> >
> > say sbprint 0x04F842;
> > say sbprint 0x04F842, :alignment(4);
> >
> > # ./test.pl6
> > 0b0100_1000_0110
> > 0b0100__1000_0100_0010
>
>
> `Int :$alignment = 8` was inspired!
>
> What does the ":" do before `alignment`?
>


-- 
__

:(){ :|:& };:


Re: printf question

2020-02-05 Thread Paul Procacci
I wasn't going to follow up but decided to do so since there is a small but
subtle bug in my original post.
I wouldn't want to mislead you Todd.

The \d has been changed to [0..9] as the expected input would only ever be
in that range.  (\d includes Unicode Characters)
I've also included an alignment parameter (shadow'ing the sub written by
you Todd).

sub sbprint( Int $n, Int :$alignment = 8) {
my Int $padding = $alignment + $n.msb - ( $n.msb +& ( $alignment  -
1 ) );
'0b' ~ "\%0{$padding}b".sprintf($n).comb(/<[0..9]> ** { $alignment
}/).join('_')
}

say sbprint 0x04F842;
say sbprint 0x04F842, :alignment(4);

# ./test.pl6
0b0100_1000_0110
0b0100__1000_0100_0010



I'm not suggesting you use my routine as I have no idea about what you're
requirements are but wanted to be sure my contribution to your problem was
sound.

Take Care.

On Wed, Feb 5, 2020 at 10:29 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-02-03 13:51, ToddAndMargo via perl6-users wrote:
> > Hi All,
> >
> > Is ther a way to get
> >
> > $ p6 'my uint8 $u = 84; printf "\$u = <%08s>\n", $u;'
> > $u = <0084>
> >
> > to print
> >
> > $u = <_0084>
> >
> > ?
> >
> >
> > Many thanks,
> > -T
>
> Hi All,
>
> Just to torment myself, I wrote a sub to do this:
>
> ...
> sub sbprint( UInt $b )  {
> my $bits  = $b.UInt.msb + 1;
> my $bytes = $bits div 8;
> if ($bits % 8 ) > 0  { $bytes += 1 };
> # say "bits = $bits   bytes = $bytes";
> my Str $bitstr = "";
>
> loop ( my $i = 0; $i < $bytes; $i += 1 )  {
>my $j = ( $b +> ( $i * 8 ) ) +& 0xFF;
>my $k = sprintf "_%08s", $j.base(2);
># say "b = $b.base(2)   i = $i   j = $j.base(2)   k = $k";
>$bitstr = $k ~ $bitstr;
># say $bitstr;
> }
> $bitstr = "0b" ~ $bitstr;
> $bitstr ~~ s/ 0b_ /0b/;
> return $bitstr;
> }
>
> say sbprint 0x04F842;
> say 0x04F842.base(2);
> ...
>
> $ intTest.pl6
> 0b0100_1000_0110
> 100 1000 0110# spaces added by me
>


-- 
__

:(){ :|:& };:


Re: printf question

2020-02-04 Thread Paul Procacci
The only thing that's wrong is that you didn't account for leading zero's.
Your initial question has a type who's size is always 1 byte.
However your second question, the one where 'something is wrong' requires
more bits of information to hold the given value.
You need to modify the sprintf to pad the binary number with the leading
zeros  (Or you can look at Tom's module which I haven't done).

my $alignment = 8;
my $u = 0x4D4F;
my $padding = $alignment + $u.msb - ( $u.msb +& ( $alignment  - 1 ) );
say '$u = <0b' ~ "\%0{$padding}b".sprintf($u).comb(/\d ** 4/).join('_') ~
'>;';


On Tue, Feb 4, 2020 at 2:04 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> >> On Mon, Feb 3, 2020 at 8:17 PM ToddAndMargo via perl6-users
> >> mailto:perl6-users@perl.org>> wrote:
> >>
> >> On 2020-02-03 13:51, ToddAndMargo via perl6-users wrote:
> >>  > p6 'my uint8 $u = 84; printf "\$u = <%08s>\n", $u;'
> >>
> >> Oops, that should have been
> >>
> >> $ p6 'my uint8 $u = 0x4F; printf "\$u = <%#b>\n", $u;'
> >> $u = <0b100>
> >>
> >> Who do I get it to print
> >>
> >>   0b0100_
> >>
> >> ?
> >>
> >> -T
> >>
>
> On 2020-02-03 20:28, Paul Procacci wrote:
> > Here's one way
> >
> > my uint8 $u = 0x4F;
> > say '$u = <0b' ~ '%08b'.sprintf($u).comb(/\d ** 4/).join('_') ~ '>;';
> >
> >
> > There's probably others as well.
>
> Hi Paul,
>
> Something is wrong.  Dang!
>
> -T
>
>
>  > my $u = 0x4D4F; say '$u = <0b' ~ '%08b'.sprintf($u).comb(/\d **
> 4/).join('_') ~ '>;';
> $u = <0b1001_1010_1001>;
>
>  > say 0b1001_1010_1001.base(16)
> 9A9
>
>
>  > my $u = 0x3D4F; say '$u = <0b' ~ '%08b'.sprintf($u).comb(/\d **
> 4/).join('_') ~ '>;';
> $u = <0b_0101_0011>;
>
>  > say 0b_0101_0011.base(16)
> F53
>


-- 
__

:(){ :|:& };:


Re: printf question

2020-02-03 Thread Paul Procacci
Here's one way

my uint8 $u = 0x4F;
say '$u = <0b' ~ '%08b'.sprintf($u).comb(/\d ** 4/).join('_') ~ '>;';


There's probably others as well.

On Mon, Feb 3, 2020 at 8:17 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-02-03 13:51, ToddAndMargo via perl6-users wrote:
> > p6 'my uint8 $u = 84; printf "\$u = <%08s>\n", $u;'
>
> Oops, that should have been
>
> $ p6 'my uint8 $u = 0x4F; printf "\$u = <%#b>\n", $u;'
> $u = <0b100>
>
> Who do I get it to print
>
>  0b0100_
>
> ?
>
> -T
>


-- 
__

:(){ :|:& };:


Re: Hash Constraints w/in subset's

2020-02-01 Thread Paul Procacci
Thanks for all the answers.  This will get me going.
I was unaware of the ' Hash[Int, Str]' type of declarative syntax.
I knew Hash[Str] was a thing but was unaware of the '[Int,Str]' type of
syntax.

Thanks for the insight!

On Sat, Feb 1, 2020 at 11:16 AM Elizabeth Mattijsen  wrote:

> subset PhoneNumber of Int where *.chars == 9;  # or any other
> constraint
> constant PhoneBook = Hash[PhoneNumber, Str];   # no need to make a
> class, a type suffices
>
> my %pb is PhoneBook = foo => 123456789;
> dd %pb;  # (my PhoneNumber %{Str} = :foo(123456789))



-- 
__

:(){ :|:& };:


Hash Constraints w/in subset's

2020-02-01 Thread Paul Procacci
Hey ladies/gents,

How would one go about defining a subset of a Hash who's key's and values
are both constrained by something 

I've read https://docs.perl6.org/type/Hash and it does make mention of
constraining keys and values, but not within the context of a subset.
Before I go ripping my hair out, I want to make sure I'm doing things right.

For instance, what if I want to define a subset that uses a Hash as it's
base class, in which I want to add the constraint that it's keys must be
strings yet it's values are constrained to only accept Int's?

Thanks in Advance!

-- 
__

:(){ :|:& };:


Re: endian

2020-01-23 Thread Paul Procacci
endianess is dictate by the cpu.
If I store the value 4 into some memory address, the storage of and
retrieval thereof is controlled by the cpu.

On Thu, Jan 23, 2020 at 11:51 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> This is just a trivia question.
>
> Does anyone know if the actual data stored in
> Raku variables is little endian or big endian?
>
> -T
>


-- 
__

:(){ :|:& };:


Re: Ping JJ: string literals

2020-01-18 Thread Paul Procacci
Sorry.  Not a WINAPI expert, nor do I want to be.  ;)

The $nSize variable looks fishy.  Can it ever contain a value that's <= 2?
If so, you're in for a surprise one day.  ;)

Bedtime.

~Paul

On Sun, Jan 19, 2020 at 12:28 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-01-18 21:20, Paul Procacci wrote:
> > Perfect.  Obviously didn't know that.  My assumption that only the first
> > byte gets checked was obviously wrong.
> >
> > Thanks gents.
>
>
> This is the way I dig out the ascii characters
> from the word array. $nSize comes back from
> function call.
>
> loop (my $Index=0; $Index < $nSize  - 2 ; $Index += 2) {
>my $i = chr( $lpBuffer[ $Index ] );
>if $i eq chr(0)  { last; }
>$ErrorString ~= $i;
> }
>
> The `$Index += 2` skips over the second byte in the
> WORD array.
>
> So far, WinAPI call have always sent me back
> ascii characters in the first byte and a zero
> in the second.
>
> 84 0 104 0 101 0 32 0 103 0 114 0 111 0 117 0 112 0 32 0 101 0 108 0 101
> 0 109 0 101 0 110 0 116 0 32 0 99 0 111 0 117 0 108 0 100 0 32 0 110 0
> 111 0 116 0 32 0 98 0 101 0 32 0 114 0 101 0 109 0 111 0 118 0 101 0 100
> 0 46 0 13 0 10 0 0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
> -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
> -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
>


-- 
__

:(){ :|:& };:


Re: Ping JJ: string literals

2020-01-18 Thread Paul Procacci
Perfect.  Obviously didn't know that.  My assumption that only the first
byte gets checked was obviously wrong.

Thanks gents.

On Sun, Jan 19, 2020 at 12:12 AM yary  wrote:

> In UTF-16 every character is 16 bits, so all 8 bits of zeros tells you is
> that it's possibly a big-endian ascii character or a little-endian
> non-ascii character at a position divisible by 256. All zeros U+ is
> unicode NULL, which the windows UTF-16 C convention uses to terminate the
> string.
> -y
>
>
> On Sat, Jan 18, 2020 at 9:04 PM ToddAndMargo via perl6-users <
> perl6-users@perl.org> wrote:
>
>> On 2020-01-18 20:05, Paul Procacci wrote:
>> >  >> I also found out the
>> >  >> hard wasy the UTF16 strings need to be terminated with
>> >  >> a double nul (0x).
>> >
>> > Not to doubt you (I don't do anything in UTF-16), but can you show an
>> > example of this?
>> > I would have thought a single NULL character is enough.
>> >
>> > The 1st byte of a Unicode character determines whether or not it's
>> ascii
>> > or not and I wouldn't think when encountering the first null, any
>> > reasonable utf-16 interpretation would consume more than just that 1st
>> byte.
>>
>> Hi Paul,
>>
>> My dealings with UTF16 are dealing with Win API
>> calls to the registry.
>>
>> This is from my work in progress doc on NativeCall
>> and WinAPI:
>>
>>  Note: a UTF16 C string is “little-endian”
>>meaning “ABC” is represented as
>>0x4200 (A), 0X4300 (B), 0X4400 (C), 0x (nul)
>>
>> The following is a call to:
>>
>>
>> https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagew
>>
>>   DWORD FormatMessageW(
>>   DWORD   dwFlags,  # bitwise OR
>> FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
>> FORMAT_MESSAGE_IGNORE_INSERTS
>>   LPCVOID lpSource, # NULL.  The location of the message
>> definition. The type of this parameter depends upon the settings in the
>> dwFlags parameter.
>>   DWORD   dwMessageId,  # the error message number ($ErrorNumber)
>>   DWORD   dwLanguageId, # 0 for system's language
>>   LPTSTR  lpBuffer, # the return string, give it 1024
>>   DWORD   nSize,# 0  nubmer of bytes in the return
>>   va_list *Arguments# NULL
>>   );
>>
>>
>> I have removed the comment from the call that prints out
>> the raw returned data.  It looks like this:
>>
>> 
>> K:\Windows\NtUtil>perl6 -I. -e "use lib '.'; use WinErr
>> :WinFormatMessage; say WinFormatMessage( 0x789, True );"
>>
>> 84 0 104 0 101 0 32 0 103 0 114 0 111 0 117 0 112 0 32 0 101 0 108 0 101
>> 0 109 0 101 0 110 0 116 0 32 0 99 0 111 0 117 0 108 0 100 0 32 0 110 0
>> 111 0 116 0 32 0 98 0 101 0 32 0 114 0 101 0 109 0 111 0 118 0 101 0 100
>> 0 46 0 13 0 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0

Re: Ping JJ: string literals

2020-01-18 Thread Paul Procacci
>> I also found out the
>> hard wasy the UTF16 strings need to be terminated with
>> a double nul (0x).

Not to doubt you (I don't do anything in UTF-16), but can you show an
example of this?
I would have thought a single NULL character is enough.

The 1st byte of a Unicode character determines whether or not it's ascii or
not and I wouldn't think when encountering the first null, any reasonable
utf-16 interpretation would consume more than just that 1st byte.





On Sat, Jan 18, 2020 at 10:14 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-01-18 04:55, Tobias Boege wrote:
> > BUT the terminating NUL character is not inserted by NativeCall and it
> > isn't inserted by &encode.
>
>
> Hi Tobias,
>
> I found this out the hard way.  I also found out the
> hard wasy the UTF16 strings need to be terminated with
> a double nul (0x).
>
> -T
>


-- 
__

:(){ :|:& };:


Re: Ping JJ: string literals

2020-01-18 Thread Paul Procacci
Thank you Tobias.

This is what I was trying to get at, but wasn't sure _how_ to reach that
conclusion.
You've done so elegantly.

~Paul

On Sat, Jan 18, 2020 at 7:55 AM Tobias Boege  wrote:

> On Sat, 18 Jan 2020, JJ Merelo wrote:
> > The example works perfectly, and it does because it's a string literal
> > which is already 0 terminated. Let's use this code instead of the one
> that
> > I used in my other mail about this (which you probably didn't read
> anyway):
> >
> > 8< 8< 8<
> >
> > What does this mean? It means that NativeCall does the right call
> > (badum-t) and converts a Raku string literal into a C string literal,
> > inserting the null termination even if we didn't. I actually don't care
> if
> > it was the NativeCall API or the encode method. It just works. It gets
> > allocated the right amount of memory, it gets passed correctly into the C
> > realm. Just works. Since @array.elems has 3 elements, well, it might be
> > rather the C part the one that does that. But I really don't care, and it
> > does not really matter, and thus the example is correct, no need to add
> > anything else to the documentation. Except maybe "get your C right"
> >
>
> What you say seems to be correct: if you have a string literal in your
> Raku code, this works for me, too. (Sometimes, see below.)
>
> BUT the terminating NUL character is not inserted by NativeCall and it
> isn't inserted by &encode. If you run this program which uses a much
> longer string that is not a literal on the face of it:
>
> use NativeCall;
> sub c-printf(CArray[uint8]) is native is symbol { * };
>
> my $string = "X" x 1994;
> my $array = CArray[uint8].new($string.encode.list);
> c-printf $array;
>
> through valgrind, it will warn you about a one-character invalid read,
> that is a byte accessed by printf() which is at offset 0 after a properly
> allocated block of size 1994:
>
> $ perl6-valgrind-m -MNativeCall -e 'sub c-printf(CArray[uint8]) is
> native is symbol { * }; my $string = "X" x 1994; my $array =
> CArray[uint8].new($string.encode.list); c-printf $array' >/dev/null
>
> ==325957== Invalid read of size 1
> ==325957==at 0x48401FC: strchrnul (vg_replace_strmem.c:1395)
> ==325957==by 0x50CD334: __vfprintf_internal (in /usr/lib/
> libc-2.30.so)
> ==325957==by 0x50BA26E: printf (in /usr/lib/libc-2.30.so)
> ==325957==by 0x4B58048: ??? (in $rakudo/install/lib/libmoar.so)
> ==325957==by 0x1FFEFFFB5F: ???
> ==325957==by 0x4B57F81: dc_callvm_call_x64 (in
> $rakudo/install/lib/libmoar.so)
> ==325957==by 0x50BA1BF: ??? (in /usr/lib/libc-2.30.so)
> ==325957==by 0xA275E3F: ???
> ==325957==by 0x990153F: ???
> ==325957==by 0xA275E3F: ???
> ==325957==by 0x1FFEFFFB7F: ???
> ==325957==by 0x4B578D1: dcCallVoid (in
> $rakudo/install/lib/libmoar.so)
> ==325957==  Address 0xb5ebf1a is 0 bytes after a block of size 1,994
> alloc'd
> ==325957==at 0x483AD7B: realloc (vg_replace_malloc.c:836)
> ==325957==by 0x4A9DFDF: expand.isra.3 (in
> $rakudo/install/lib/libmoar.so)
> ==325957==by 0x4A9E6F4: bind_pos (in
> $rakudo/install/lib/libmoar.so)
> ==325957==by 0x4A2C9AF: MVM_interp_run (in
> $rakudo/install/lib/libmoar.so)
> ==325957==by 0x4B2CC24: MVM_vm_run_file (in
> $rakudo/install/lib/libmoar.so)
> ==325957==by 0x109500: main (in $rakudo/install/bin/perl6-m)
>
> This is the NUL byte that happens to be there and terminate our string
> correctly, but nothing in the moarvm process has allocated it, because
> knowing what is allocated and what isn't is valgrind's job. And if it's
> not allocated, then either moarvm routinely writes NULs to memory it
> doesn't own or it simply does not automatically insert a NUL after the
> storage of every CArray[uint8]. And why would it? I for one would not
> expect CArray[uint8] to have special precautions built in for when it's
> used to hold a C string.
>
> Why does it work with a string literal in the Raku code? I don't know,
> but consider the following variations of the code, with my oldish Rakudo:
>
>   - with $string = "X" x 1994: valgrind sad
>   - with $string = "X" x 4:valgrind sad
>   - with $string = "X" x 3:valgrind happy
>   - with $string = "X" x 2:valgrind happy
>   - with $string a short literal
> like "FOO":valgrind happy
>   - with $string a literal of
> sufficient length like "FOOO": valgrind sad
>   - with $string = "FOO" x 2:  valgrind happy
>   - with $string = "FOO" x 200:valgrind sad
>
> My guess is that if it's sufficiently small and easy, then it is computed
> at compile-time and stored somewhere in the bytecode, for which some
> serialization routine ensures a trailing NUL byte inside an allocated
> region of memory for the process.
>
> That is only a barely informed guesses, but independently of what causes
> it to work on short string literals, I strongly 

Re: problems with xor

2020-01-17 Thread Paul Procacci
Apocalypse 3:

"*operators are just funny* looking *function* calls"

On Sat, Jan 18, 2020 at 12:31 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-01-17 20:49, Kevin Pye wrote:
> > In Raku, all operators are just functions with a funny syntax
>
> I like the way you wrote that.  Your technical
> writing just got some fan mail.
>


-- 
__

:(){ :|:& };:


Re: problems with xor

2020-01-17 Thread Paul Procacci
multi sub infix:<+^>($a, $b --> Int:D)


_infix_ here is the keyword for you.

my uint8 $z = $x +^ $y;

On Fri, Jan 17, 2020 at 11:03 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> https://docs.raku.org/routine/+$CIRCUMFLEX_ACCENT
>
> (Operators) infix +^§
>
> multi sub infix:<+^>($a, $b --> Int:D)
>
> Integer bitwise XOR operator: Coerces both arguments to Int and does a
> bitwise XOR (exclusive OR) operation.
>
>
> $ p6 'my uint8 $x=0b0101_0111; my uint8 $y = 0b_; my uint8 $z =
> +^($x, $y ); say "0", $x.base(2); say "", $y.base(2); say $z.base(2);'
>
> 01010111
> 
> 1101
>
>
> XOR
> A  B   A xor B
> 0  0  0
> 1  0  1
> 0  1  1
> 1  1  0
>
> That s not what I am seeing above.
>
> What am I doing wrong this time?
>
> And who set the high bit making $z negative?
>
> Many thanks,
> -T
>


-- 
__

:(){ :|:& };:


Re: Ping JJ: string literals

2020-01-17 Thread Paul Procacci
>> For anyone following along that is not familiar with
>> C, a BYTE in C is called a Char in C.  (Causes
>> some interesting conversations when UTF16 gets involved.)

I don't want to make this about C, but to clarify there's no type called
"BYTE" in the C language.
If BYTE is defined it's system dependent and usually points to an unsigned
char.

>> The paragraph with the mistake in it is specifically talking about
strings.

That paragraph you quoted shows a message_box(Str) as the example.
It's argument is undoubtedly expecting a NULL terminated string.

However, your contention is about a set_foo(CArray[uint8])..

These aren't the same thing as I've already explained to you twice now.

++

With the above said, I personally _agree_ the documentation should be
altered.
This text preceding the example is what leads me to believe it should be:

" If the C function requires the lifetime of a string to exceed the
function call"

This hints to me that the function call is in fact supposed to take a
string.
$array.elems currently shows 3 items w/ the following values: 0x46 0x4F 0x4F
when I would expect, the CArray[uint8] to contain the following instead:
0x46 0x4F 0x4F 0x00

++

In closing.  if you have an example where you can get a garbage characters
because of a bad
example, then the example _should_ be updated.  Without knowing Raku
internals it's hard for me
to say (I'm not a Raku dev) but I feel I'm on the right track.

I've tried causing a segfault myself, but a trace of the small app always
made new allocations (mmap - which zero's pages),
so it's hard for me to duplicate such an error.

On Fri, Jan 17, 2020 at 9:16 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> For anyone following along that is not familiar with
> C, a BYTE in C is called a Char in C.  (Causes
> some interesting conversations when UTF16 gets involved.)
>
>
> On 2020-01-17 17:03, Paul Procacci wrote:
> >  >> Take a peak at N1570, 5.2.1p2, 7.1.1 and 6.7.9,p14.
> >  >> leaving off the nul is not a valid way of presenting a
> >  >> string to a C function.
> >
> > Here's the rub though Todd.  Who is saying it's a string that is being
> > passed?  (Hint: You are)
>
> True. And one other place too:
>
> https://docs.raku.org/language/nativecall#Passing_and_returning_values
>
> The paragraph with the mistake in it is specifically
> talking about strings.
>
>For strings, there is an additional encoded trait
>to give some extra hints on how to do the marshaling.
>
>To specify how to marshal string return types, just
>apply this trait to the routine itself.
>
>Note that a NULL string pointer can be passed by
>passing the Str type object; a NULL return will
>also be represented by the type object.
>
> At what point to you consider that they are actually speaking
> about C Strings?  Then they proceed to construct an
> "undefined" array, not a C String.
>
>
> > If the intention is to pass 3 bytes "FOO" starting at a given memory
> > address, then it's exactly as it should be.
>
> That is a string literal, not a string.
>
> > If the intention is to pass 4 bytes "FOO\0" then yes, it's a
> > documentation problem.
>
> Now we are talking.
>
> >
> > The reason for this ambiguity is because the prototype accepts either-or
> > of those interpretations.
>
> The documentation should so state.  It should make
> the distinction between strings and arrays (string
> literals).  Instead it blabs on and on about strings
> and the tells you how to convert an "undefined" array.
> It is clearly a mistake.
>
> > You can't make any definitive declaration without looking at the
> > function body.
> > A 'char *' doesn't automatically mean a NULL terminated string.  YOU are
> > making that assumption.
> > It could simply accept a 3 byte character array and we're done.
>
> You missed my point.  The structure required a
> companion variable to give the array's length.
>
> An actual C String stands on its own.
>
> >
> > I'm familiar with C, in fact it's my bread and butter.  Quite frankly, I
> > don't need the copy/pastes
> > from outside sources as I know for sure that everything I've stated here
> > is 100% accurate.
>
> That would make you the PERFECT person to clean up the documentation and
> make is usable to the rest of us!
>
> If you are one of Raku's developers, would you please
> consider taking over NativeCall's documentation page?
> 

Re: Ping JJ: string literals

2020-01-17 Thread Paul Procacci
>> Take a peak at N1570, 5.2.1p2, 7.1.1 and 6.7.9,p14.
>> leaving off the nul is not a valid way of presenting a
>> string to a C function.

Here's the rub though Todd.  Who is saying it's a string that is being
passed?  (Hint: You are)
If the intention is to pass 3 bytes "FOO" starting at a given memory
address, then it's exactly as it should be.
If the intention is to pass 4 bytes "FOO\0" then yes, it's a documentation
problem.

The reason for this ambiguity is because the prototype accepts either-or of
those interpretations.
You can't make any definitive declaration without looking at the function
body.
A 'char *' doesn't automatically mean a NULL terminated string.  YOU are
making that assumption.
It could simply accept a 3 byte character array and we're done.

I'm familiar with C, in fact it's my bread and butter.  Quite frankly, I
don't need the copy/pastes
from outside sources as I know for sure that everything I've stated here is
100% accurate.

>> C can not figure out where the end of the string

I don't see a string.  I only see a Character Array.



On Fri, Jan 17, 2020 at 7:06 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-01-17 15:34, Paul Procacci wrote:
> > Todd (and others),
> >
> > So a few things.
> >
> > 1)
> > JJ's example worked by chance.  (sorry JJ).
> > It worked because it just so happens that either his OS zero filled the
> > block he was allocated by the allocator
> > or because he didn't reuse the block that was given to him initially by
> > the allocator.
> >
> > 2)
> > If you assume that the function set_foo($array) expects a NULL
> > terminated string, only then is the documentation a problem.
> > However, there's not enough information in the C prototype to make this
> > assumption.  There's also no set_foo($array) function body to explore.
> >
> > # C prototype is void set_foo(const char *)
> >
> >
> > All this says is that the subroutine is receiving a pointer to memory
> > containing bytes and nothing more.
> > The body of the function _may_ simply check the first 3 bytes.  This is
> > up to the reader to ascertain.
> >
> > 3) As for a documentation update, I'm unsure if it's required.  The
> > example is not only perfectly valid raku, it's also perfectly valid C.
>
> Actually, it is not perfectly okay with C.  Let me state
> my case:
>
> Take a peak at N1570, 5.2.1p2, 7.1.1 and 6.7.9,p14.
> leaving off the nul is not a valid way of presenting a
> string to a C function.
>
> C can not figure out where the end of the string is
> on its own without the nul.  It will careen until it
> find a nul.  Even further if the string is UTF16 and
> is looking for a double nul (0x);
>
> Now if you want to do a "String Literal", you can
> leave the nul off.  But it required that you inform
> to other end as to the String Literal's length.  And
> a "String Literal" IS NOT a C string.
>
> "String Literal's" are covered by N1570 6.7.9,p14
>
> An example of a string literal can be found at:
>
>
> https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regsetvalueexw
>
> "const BYTE *lpData" points to the String Leteral, and
>
> "DWORD cbData" contains the length of the string literal
>
> C Strings and String Literals are two vastly different things.
>
> In that same example:
>
>  "LPCWSTR lpValueName" points to a C String that must
>  be nul terminated.  Note that there is no companion
>  variable stating its length
>
>
> > Perhaps something could be added to remove whatever ambiguity you
> > perceive, but you argument stems from something that isn't necessarily
> > true in this example
> > and that's the function set_foo($array) expects a NULL terminated string.
> >
> > ~Paul
>
> Hi Paul,
>
> This is the example I previously posted of careening
> that drove me crazy trying to troubleshoot.  The C guys
> told me it was not a convincing example.
>
> https://ibb.co/tHy7ZQM
>
> The C Spec N1570, 5.2.1, p2 is very clear:
>
> A byte with
>   all bits set to 0, called the
> null character, shall exist in the basic
> execution character set; it
>   is used to
> terminate a character string.
>
>
> And not including it causes all kinds of consternation,
> as I found out the hard way.
>
> -T
>


-- 
__

:(){ :|:& };:


Re: Ping JJ: string literals

2020-01-17 Thread Paul Procacci
Todd (and others),

So a few things.

1)
JJ's example worked by chance.  (sorry JJ).
It worked because it just so happens that either his OS zero filled the
block he was allocated by the allocator
or because he didn't reuse the block that was given to him initially by the
allocator.

2)
If you assume that the function set_foo($array) expects a NULL terminated
string, only then is the documentation a problem.
However, there's not enough information in the C prototype to make this
assumption.  There's also no set_foo($array) function body to explore.

# C prototype is void set_foo(const char *)


All this says is that the subroutine is receiving a pointer to memory
containing bytes and nothing more.
The body of the function _may_ simply check the first 3 bytes.  This is up
to the reader to ascertain.

3) As for a documentation update, I'm unsure if it's required.  The example
is not only perfectly valid raku, it's also perfectly valid C.
Perhaps something could be added to remove whatever ambiguity you perceive,
but you argument stems from something that isn't necessarily true in this
example
and that's the function set_foo($array) expects a NULL terminated string.

~Paul

On Fri, Jan 17, 2020 at 5:29 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi JJ,
>
> Please be my hero.
>
> I won't call you any goofy names out of
> affection and friendship, as others will get
> their nickers in a twist.
>
> This is from a previous conversation we had concerning
> the mistake in
>
> https://docs.raku.org/language/nativecall#index-entry-nativecall
>
>  my $string = "FOO";
>  ...
>  my $array = CArray[uint8].new($string.encode.list);
>
>
> Todd:
>  By the way, "C String" REQUIRES a nul at the end:
>  an error in the NativeCall documentation.
>
> JJ:
>  No, it does not. And even if it did, it should better
>  go to the C, not Raku, documentation
>
>
> And that would be a "String Literal", which is NOT
> a C String.  And C's documentation is precise and
> clear (n1570).  It is not their problem.  It
> is a mistake in NativeCall's documentation.
>
> Without the nul at the end, the string is considered
> "undefined".
>
> The C guys have been helping me with definitions.  Chapter and
> verse would be :
>
> INTERNATIONAL STANDARD ©ISO/IEC ISO/IEC 9899:201x
> Programming languages — C
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
>
>  5.2.1 Character sets
>  7.1.1 Definitions of terms
>  6.7.9,p14 Semantics
>
> Here is your String Literal, which you are confusing
> with a c string:
>
> 6.7.9,p14 states:
>
>  An array of character type may be initialized by a
>  character string literal or UTF−8 string literal,
>  optionally enclosed in braces. Successive bytes
>  of the string literal (including the terminating
>  null character if there is room or if the array
>  is of unknown size) initialize the elements of
>  the array.
>
> So there is the unnecessary nul you speak of, except,
> again, it is not a C String.  So you do have a
> somewhat of a point, although not a good one as
> it will throw those trying to use the docs into
> a state of confusion wondering what is going wrong,
> as it did me.
>
> It about killed me to figure it our myself.  I
> don't want others to go through the same pain
> over a simple mistake in the documentation.
>
> Now the C guys told me that the reason why I am not
> getting anywhere with you is that I provided a bad
> example.  They proceeded to give me an example
> that precisely shows the careening make by
> the mistake in the documentation:
>
>
> An example of an unterminated C string:
>
> On 2020-01-17 13:21, Bart wrote:
> >
> > 
> > #include 
> >
> > void foo(const char *s)
> > {
> > for (int i=0; i<10; ++i)
> > printf("%d ",*s++);
> > puts("");
> >
> > }
> >
> > int main(void)
> > {
> >  char str[3] = "FOO";
> >  foo(str);
> > }
> > 
>
>
> To compile:
>   gcc -o t2 t2.c
>
> To Run
>   t2
>
>
> > This prints the 3 characters codes of F,O,O in the string, plus 7
> > bytes that follow. Results on various compilers are as follows:
> >
> > bcc: 70 79 79 0 0 0 0 0 0 0
> > tcc: 70 79 79 80 -1 18 0 0 0 0
> > gcc -O0: 70 79 79 112 -14 48 0 0 0 0
> > gcc -O3: 70 79 79 2 0 0 0 0 0 0
> > lcc: 70 79 79 0 0 0 0 0 0 0
> > dmc: 70 79 79 0 -120 -1 24 0 25 33
> > clang -O0:   70 79 79 16 6 64 0 0 0 0
> > clang -O2:   70 79 79 0 48 -120 73 6 -19 127
> > msvc:70 79 79 -29 -9 127 0 0 0 0
> >
> > The 70, 79, 79 are the F, O and O codes. When those 3 happen to
> > be followed by 0, then it will appear to work.
> >
> > That is 4 out of 9, but the other 5 won't work. What follows
> > after FOO is undefined and could be anything, although a random 0 is
> common.
> >
>
> JJ!  He ran it through NINE C compilers!  The careening
> is OBVIOUS!
>
> And this mistake is very easy to fix:
>
> Change
> my $array = CArray[uint8].new($string.encode.

Re: bitwise NOT

2020-01-14 Thread Paul Procacci
 >> What is the syntax for a twos complement anyway?

I'm not sure I understand the question.
Two's compliment is +^ ... the routine you've been using.

On Tue, Jan 14, 2020 at 12:33 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> >> On Mon, Jan 13, 2020 at 11:30 PM ToddAndMargo via perl6-users
> >> mailto:perl6-users@perl.org>> wrote:
> >>
> >> Hi All,
> >>
> >> This works,
> >>
> >>  $ p6 'my uint8 $c = 0xA5; my uint8 $d =  +^$c; say
> $d.base(16);'
> >>  5A
> >>
> >> But this does not:
> >>
> >>  $ p6 'my uint8 $c = 0xA5; say (+^$c).base(16);'
> >>  -A6
> >>
> >> 1) who turned it into an negative integer?
> >>
> >> 2) how do I turn it back?
> >>
> >> Many thanks,
> >> -T
>
> On 2020-01-13 21:18, Paul Procacci wrote:
> > If you read the signature for +^, you'll notice it returns an Int.
> >
> > In your first working example, you're taking a uint8 with binary value
> > 10100101, zero extending it to 64 bits via +^, applying a two's
> > compliment, and then assigning bits [0:7] to another uint8 which at that
> > point contains the binary value of 01011010 (or hex value 0x5A).
> > In your second example that isn't working, you're taking uint8 with
> > binary value 10100101, zero extending it to 64 bits via +^, applying a
> > two's compliment, and then displaying this *Int* (64 bits) as hex.[1]
> > To turn it back you need to mask off bits [8:63] with: say ((+^$e) +&
> > 0x0FF).base(16);" [2]
> >
> > [1] I'd show you the 64 bit value but it's a bunch of 1's followed by
> > the value -0xA6.
> > [2] Note, since the type has been promoted to an Int there' no going
> > back to uint8 without an explicit assignment (afaik)
> >
>
> That explains it.  Thank you.
>
> I used uint8 to keep the ones to a mild torrent!
>
> If I am remembering correctly, 0xA5 going to 0x5A is
> a ones compliment.
>
> What is the syntax for a twos complement anyway?
>


-- 
__

:(){ :|:& };:


Re: bitwise NOT

2020-01-13 Thread Paul Procacci
 If you read the signature for +^, you'll notice it returns an Int.

In your first working example, you're taking a uint8 with binary value
10100101, zero extending it to 64 bits via +^, applying a two's compliment,
and then assigning bits [0:7] to another uint8 which at that point contains
the binary value of 01011010 (or hex value 0x5A).
In your second example that isn't working, you're taking uint8 with binary
value 10100101, zero extending it to 64 bits via +^, applying a two's
compliment, and then displaying this *Int* (64 bits) as hex.[1]
To turn it back you need to mask off bits [8:63] with: say ((+^$e) +&
0x0FF).base(16);" [2]

[1] I'd show you the 64 bit value but it's a bunch of 1's followed by the
value -0xA6.
[2] Note, since the type has been promoted to an Int there' no going back
to uint8 without an explicit assignment (afaik)

On Mon, Jan 13, 2020 at 11:30 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> This works,
>
> $ p6 'my uint8 $c = 0xA5; my uint8 $d =  +^$c; say $d.base(16);'
> 5A
>
> But this does not:
>
> $ p6 'my uint8 $c = 0xA5; say (+^$c).base(16);'
> -A6
>
> 1) who turned it into an negative integer?
>
> 2) how do I turn it back?
>
> Many thanks,
> -T
>


-- 
__

:(){ :|:& };:


Re: Bug to report: cardinal called an integer

2020-01-13 Thread Paul Procacci
>> what is the best strategy?

My general rule of thumb:

If you control the value then you can pick whatever data type that has the
largest number of bits to hold your largest value.[1]
If you don't control the value, stick with an Int (or Uint).

If you stick with Int you can later modify it to use more tailor made type
as necessary, but Int is always going to be the safest bet.

Hope this helps you.

[1] I never use unsigned variants of integers where arithmetic may be
involved do to overflow and use variants of signed integers instead
whenever possible.

On Mon, Jan 13, 2020 at 11:09 PM Aureliano Guedes <
guedes.aureli...@gmail.com> wrote:

> About the Raku typing, suppose I'll write a library to deal with data
> frames/tables (as PDL - Perl(5) Data Language), something like
> Pandas-Python or R.
> After reading the file (csv; tsv ) I'd like that some routine
> identifies the best type to fix each column (especially in cases like Unit,
> unit, int, ) what is the best strategy?
>
> On Mon, Jan 13, 2020 at 10:51 PM ToddAndMargo via perl6-users <
> perl6-users@perl.org> wrote:
>
>> >> On Mon, Jan 13, 2020 at 9:51 PM ToddAndMargo via perl6-users
>> >> mailto:perl6-users@perl.org>> wrote:
>> >>
>> >> On 2020-01-13 18:46, ToddAndMargo via perl6-users wrote:
>> >>  > On 2020-01-13 17:13, ToddAndMargo via perl6-users wrote:
>> >>  >> And, no one is telling me percisely what the difference
>> >>  >> between UInt and uint is other than one is a subset of
>> >>  >> Int and the other is a native type.  They act exactly
>> >>  >> the same.
>> >>  >
>> >>  > Hi All,
>> >>  >
>> >>  > Off line, Paul told me what the difference is
>> >>  > between a UInt and a uint.
>> >>  >
>> >>  > uint is constrained:
>> >>  >
>> >>  >  p6 'my uint $c = 0x0; $c = $c +| 0x1;'
>> >>  >  Cannot unbox 65 bit wide bigint into native integer
>> >>  >   in block  at -e line 1
>> >>  >
>> >>  > UInt uses magic Larry Wall powder to remove the constraints
>> >>  > from the variable.  (Same powder he uses to create nils.)
>> >>  >
>> >>  >  p6 'my UInt $c = 0x0; $c = $c +|
>> >>  > 0x1F;
>> say
>> >> $c'
>> >>  >
>> 862718293348820473429344482784628181556388621521298319395315527974911
>> >>  >
>> >>  > Other than the constrains, they act exactly the same.
>> >>  > Oh, and they are both (generic programming term) unsigned
>> integers
>> >>  > (cardinals)
>> >>  >
>> >>  > :-)
>> >>  >
>> >>  > -T
>> >>
>> >>
>> >> And good luck, all you who told me to read the
>> >> documentation, trying to find the constraints
>> >> explanation in the documentation:
>> >>
>> >> https://docs.raku.org/type/UInt
>> >>
>> >> Have fun!
>> >>
>> >> But it does say "The UInt is defined as a subset of Int:"
>> >> and Int is definitely constrained.
>> >>
>>
>> On 2020-01-13 19:13, Paul Procacci wrote:
>> >  >> trying to find the constraints explanation in the documentation:
>> >
>> > https://docs.raku.org/language/nativetypes
>> >
>> > "Raku offers a set of /native/ types with a fixed, and known,
>> > representation in memory"
>> > and
>> > "However, these types do not necessarily have the size that is required
>> > by the NativeCall <https://docs.raku.org/language/nativecall>
>> interface
>> > (e.g., Raku's |int| can be 8 bytes but C's |int| is only 4 bytes)"
>> >
>> > Took me all of 30 seconds to find.  Obviously that page has much more
>> > information to offer than just those couple of sentences, but ALL
>> native
>> > types are constrained by a certain number of bits.
>>
>>
>> Hi Paul,
>>
>> For those of you who were no privy to Paul and my
>> offline discussion, he must have wrote me 10
>> times trying to explain things to me before
>> I got it.  He is a real mensch.
>>
>> 30 seconds!  Yikes!  I never found it.
>>
>> https://docs.raku.org/type/UInt
>> should have stated that directly, but did not.
>>
>> And to add injury,
>>
>> https://docs.raku.org/language/nativetypes
>>  "Raku offers a set of /native/ types with a fixed,
>>  and known, representation in memory"
>>
>> Did not state who they were.  Sort of like when I
>> want the  salt, I always reach for the pepper!
>>
>> The state of the documentation drives me  ...
>>
>>  AAHH! 
>>
>> -T
>>
>
>
> --
> Aureliano Guedes
> skype: aureliano.guedes
> contato:  (11) 94292-6110
> whatsapp +5511942926110
>


-- 
__

:(){ :|:& };:


Re: Bug to report: cardinal called an integer

2020-01-13 Thread Paul Procacci
 >> trying to find the constraints explanation in the documentation:

https://docs.raku.org/language/nativetypes

"Raku offers a set of *native* types with a fixed, and known,
representation in memory"
and
"However, these types do not necessarily have the size that is required by
the NativeCall  interface (e.g.,
Raku's int can be 8 bytes but C's int is only 4 bytes)"

Took me all of 30 seconds to find.  Obviously that page has much more
information to offer than just those couple of sentences, but ALL native
types are constrained by a certain number of bits.

On Mon, Jan 13, 2020 at 9:51 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-01-13 18:46, ToddAndMargo via perl6-users wrote:
> > On 2020-01-13 17:13, ToddAndMargo via perl6-users wrote:
> >> And, no one is telling me percisely what the difference
> >> between UInt and uint is other than one is a subset of
> >> Int and the other is a native type.  They act exactly
> >> the same.
> >
> > Hi All,
> >
> > Off line, Paul told me what the difference is
> > between a UInt and a uint.
> >
> > uint is constrained:
> >
> >  p6 'my uint $c = 0x0; $c = $c +| 0x1;'
> >  Cannot unbox 65 bit wide bigint into native integer
> >   in block  at -e line 1
> >
> > UInt uses magic Larry Wall powder to remove the constraints
> > from the variable.  (Same powder he uses to create nils.)
> >
> >  p6 'my UInt $c = 0x0; $c = $c +|
> > 0x1F; say $c'
> > 862718293348820473429344482784628181556388621521298319395315527974911
> >
> > Other than the constrains, they act exactly the same.
> > Oh, and they are both (generic programming term) unsigned integers
> > (cardinals)
> >
> > :-)
> >
> > -T
>
>
> And good luck, all you who told me to read the
> documentation, trying to find the constraints
> explanation in the documentation:
>
> https://docs.raku.org/type/UInt
>
> Have fun!
>
> But it does say "The UInt is defined as a subset of Int:"
> and Int is definitely constrained.
>
>
>
> -T
>


-- 
__

:(){ :|:& };:


Cannot invoke this object (REPR: Null; VMNull)

2019-12-14 Thread Paul Procacci
Ladies/Gents,

I'm perplexed by the error message as stated in the subject.
It's quite possible I'm doing it wrong.
What I'm attempting to do is create an attribute of a class that is a Map
... who's value is known at compile time; hence the CHECK phaser.

To produce the error it requires two source files, as merging the class and
the code that invokes the class doesn't occur in an error:

# File lib/A.pm6

class A {

has Map $!pre-compiled = CHECK {
Map.new: dir("data", test => { $_ eq none('.', '..') and
"data/$_".IO.d })>>.map({
.basename => .dir>>.map({
.basename => .dir>>.basename.Set
})
})
};

method test {
$!pre-compiled.perl.say;
}
}


# File: test.pl6
---
use lib ;
use A;

my A $x .= new;
$x.test;


% ./test.pl6
Cannot invoke this object (REPR: Null; VMNull)
  in block  at /git-repos/test/lib/Loader.pm6 (Loader) line 31
  in method test at /git-repos/test/lib/Loader.pm6 (Loader) line 115
  in method load at /git-repos/lib/Loader.pm6 (Loader) line 132
  in block  at ./test.pl6 line 7


As stated earlier, when I merge the class and the initialization logic into
the same source file and run from there, no error occurs.

Am I doing this wrong?

Thanks,
Paul Procacci

-- 
__

:(){ :|:& };:


Re: Quoting issue in Windows

2019-12-03 Thread Paul Procacci
echo isn't a great example at all.  echo is both OS and SHELL specific.
Not only that, echo has argv to work with; each with it's own terminating
'\0'.
It absolutely can be quite literal, though that doesn't stop the
implementors from doing whatever they want.

Here's a snippet from my own OS's `man echo`:

 "Most shells provide a builtin echo command which tends to differ from
 this utility in the treatment of options and backslashes.  Consult the
 builtin(1) manual page."


Here's the cruft of it ..

With a single quoted string . containing a single quote itself  ...
in any given interpreted language . with no EOL or EOF in sight 
How would you let the interpreter know your done?
The answer:  You can't without escaping the single quote.

Some more examples I wrote up.
You'll note, they all use single quotes, yet they all interpret as they
absolutely should.

Example Perl 5:
-
# perl -e "print '\''" ;
'
# perl -e "print '\\'" ;
\
# perl -e "print '\a'" ;
\a


Example PHP 7.3:
-
# php -r "print '\'';"
'
# php -r "print '\\';"
\
# php -r "print '\a';"
\a

Example python3.6  **unique**:
---
# python3.6 -c "print('\'')"
'
# python3.6 -c "print('\\')"
\
# python3.6 -c "print('\a')" | hexdump -oc | head -1
000  005007


On Tue, Dec 3, 2019 at 9:04 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2019-12-03 17:31, Paul Procacci wrote:
> > When a string is specified in single quotes, perl6 (or any other
> > language that I'm aware of) will not evaluate or interpret an escape
> > character EXCEPT when the escape is follow'd by a single quote (') or
> > backslash(\).
> > These HAVE to be escaped and the interpreter HAS to account for it.
>
> Hi Paul,
>
> I am not finding that to be the case.  Well maybe not Perl 5.
>
> $ bash -c "echo '\'"
> \
>
> $ echo '\\\'
> \\\
>
> $ echo '\\'
> \\
>
> $ echo '\'
> \
>
>
>If it is on purpose and not a "feature" in Perl 6, then
> the documentation should state that single quotes will
> have interpretations inside of them in certain cases.
>
>
> Ahh Perl 5 is a pain in the butt backslash wise!
>
> $ perl -e "CORE::say '';"
> \
>
> And I am seeing in
> https://docs.perl6.org/language/quoting
>   Literal strings: Q
>   Q[A literal string]
>
> Is the way I have been using single quotes.
>
> So the rule is single quotes are literal, unless a backslash
> in included, then use Q
>
> Thank you for the help!
>
> -T
>


-- 
__

:(){ :|:& };:


Re: rmdir question

2019-12-02 Thread Paul Procacci
It seems to me all your questions are answered by the very documentation
you referenced.

>> Does *@dirs mean I can give it multiple directories as an array?

Remove the invocant   in sub form  of the provided directories in
the given list .

Example:
-
# mkdir {a,b} ; ls -ld a b ; `which perl6` -e "rmdir('a','b')" ; ls -ld a b
drwxr-xr-x  2 root  wheel  2 Dec  2 22:22 a
drwxr-xr-x  2 root  wheel  2 Dec  2 22:22 b
ls: a: No such file or directory
ls: b: No such file or directory


>> What does  "--> List:D" give me back?

It gives you back a list of directories that it successfully removed.
Example:

# mkdir a ; ls -ld a b ; `which perl6` -e "rmdir('a','b').perl.say"
ls: b: No such file or directory
drwxr-xr-x  2 root  wheel  2 Dec  2 22:23 a
["a"]
# mkdir {a,b} ; ls -ld a b ; `which perl6` -e "rmdir('a','b').perl.say"
drwxr-xr-x  2 root  wheel  2 Dec  2 22:23 a
drwxr-xr-x  2 root  wheel  2 Dec  2 22:23 b
["a", "b"]

>> And what is a "List:D?" anyway?

https://docs.perl6.org/type/Signature#Constraining_argument_definiteness

The above link explains it fully.  Though it uses "Int:D" as an example it
still applies.

>>  ...throws an exception of type X::IO::Rmdir if
>> the directory cannot be removed
>>
>> Does this crash the program or just return a "false"?

On success it return a Bool::True.
On failure it throws an exception.
It doesn't crash the program.
If you are interested in handling the exception, add logic to handle it.

Examples:
---
# mkdir a ; `which perl6` -e "'a'.IO.rmdir.perl.say"
Bool::True
# mkdir a ; `which perl6` -e "'b'.IO.rmdir.perl.say"
Failure.new(exception => X::IO::Rmdir.new(path => "/root/test/b", os-error
=> "Failed to rmdir: no such file or directory"), backtrace =>
Backtrace.new)
# mkdir a ; `which perl6` -e "try { 'b'.IO.rmdir.perl.say; CATCH { default
{ 'tsk tsk'.say }}}"
tsk tsk


>> Does it tell me what went wrong, as in "so and so" has "such and such"
file(s) open and locked?
See previous example.  It throws an exception with a detailed error message.

>> To delete non-empty directory, see rmtree in File::Directory::Tree
module.
>>
>> Seems to me I should not have to import a module for
>> this.  Most rmdir command have a "parents" flag.  Am
>> I missing something?

It's true that most binaries with the name rmdir have a p switch for
removing "empty directories" which I won't argue could be added to perl6
proper.
That documentation you are referencing though is concerning "non-empty
directories" and there no such flag for any rmdir binary I've seen.

~Paul

On Mon, Dec 2, 2019 at 9:39 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
>  From the manual page of rmdir:
> https://docs.perl6.org/routine/rmdir
>
>subrmdir(*@dirs --> List:D)
>method rmdir(IO::Path:D: --> True)
>
> Does *@dirs mean I can give it multiple directories
> as an array?
>
> What does  "--> List:D" give me back?  An array
> of Booleans as to which directories successfully removed?
> And what is a "List:D?" anyway?
>
>
>   ...throws an exception of type X::IO::Rmdir if
>   the directory cannot be removed
>
> Does this crash the program or just return a "false"?
> Does it tell me what went wrong, as in "so and so" has
> "such and such" file(s) open and locked?
>
> To delete non-empty directory, see rmtree in File::Directory::Tree module.
>
> Seems to me I should not have to import a module for
> this.  Most rmdir command have a "parents" flag.  Am
> I missing something?
>
> Many thanks,
> -T
>
>
>
> --
> 
> A computer without Microsoft is like
> a chocolate cake without the mustard
> 
>


-- 
__

:(){ :|:& };:


Re: Dynamic export/import of symbols into global namespace

2019-09-20 Thread Paul Procacci
I happen to agree with both of you and have no problems with this.
I think me trying to be `too slick` in the world of subsets and `classes`
has possibly made this more complicated than it has to be.

For the record, I knew about `our` `my` `is export`, etc. but had the
damnest time trying to get that to apply to subsets.  I was obviously doing
something wrong.

Have a great one folks and thanks for the eyes.

~Paul

On Fri, Sep 20, 2019 at 4:01 PM William Michels 
wrote:

> "Declaring a list of variables with lexical (my) or package (our) scope"
>
> https://docs.perl6.org/language/variables#index-entry-declaring_a_list_of_variables
>
> my:
> https://docs.perl6.org/syntax/my
>
> our:
> https://docs.perl6.org/syntax/our
>
> Paul, hoping the above points you in the right direction. FWIW, I
> agree with Vadim re: maintaining lexical scope at the highest possible
> level.
>
> Best, Bill.
>
>
>
>
> On Fri, Sep 20, 2019 at 12:38 PM Vadim Belman  wrote:
> >
> > Why would you need this? Mangling with things outside of your lexical
> scope isn't one of the best ideas. Perhaps, you could achieve same result
> with simply exporting the subset from the module using selective export
> with `is export(:tag)`?
> >
> > Best regards,
> > Vadim Belman
> >
> > On Sep 20, 2019, at 3:32 PM, Paul Procacci  wrote:
> >
> > As the subject suggests, I'd like to dynamically export/import symbols
> from a source file into the global scope of the program.  How would one
> accomplish this?
> >
> > Given the below, it yields an error _which I expect_.  How do I
> dynamically pull in the subset 'What'?
> >
> >
> > File: Testing.pm6
> > --
> >
> > subset What of Str;
> > class Testing {}
> >
> >
> >
> > File: main.pl
> > --
> > {
> > my $c = 'Testing';
> > require ::($c);
> > my $t = ::($c).new;
> > }
> >
> > my What $x = 'Blah'
> >
> >
> > --
> > __
> >
> > :(){ :|:& };:
> >
> >
>


-- 
__

:(){ :|:& };:


Dynamic export/import of symbols into global namespace

2019-09-20 Thread Paul Procacci
As the subject suggests, I'd like to dynamically export/import symbols from
a source file into the global scope of the program.  How would one
accomplish this?

Given the below, it yields an error _which I expect_.  How do I dynamically
pull in the subset 'What'?


File: Testing.pm6
--

subset What of Str;
class Testing {}



File: main.pl
--
{
my $c = 'Testing';
require ::($c);
my $t = ::($c).new;
}

my What $x = 'Blah'


-- 
__

:(){ :|:& };:


Re: Variable character class

2019-09-02 Thread Paul Procacci
I don't know then.

I've created the following ticket:

https://github.com/perl6/doc/issues/2999

Feel free to place your own input there if you feel the need.

On Mon, Sep 2, 2019 at 12:37 PM William Michels 
wrote:

> Sorry Paul, I don't get the correct answer in any of the three cases I
> tried. Here's what 6Pad returns:
>
> https://perl6.github.io/6pad/
>
> sub matching_chars(Str $chars_to_match, Str $str) {
> # warnings, treats as string not variable
> $str ~~ /<$_>/ given "<[$chars_to_match]>";
> }
>
> say matching_chars("24680", "19584203"); # expect 「8420」
> say matching_chars("Lorem ipsum dolor sit amet, consectetuer
> adipiscing elit.", "abcdef"); # expect 「a」 「cde」
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # expect 「][+//」
>
> 「19584203」
> 「abcdef」
> ===SORRY!=== Error while compiling ?/EVAL_7 Malformed regex at
> ?/EVAL_7:1 --> anon regex { Apple ][+//e} expecting any of: infix
> stopper
>
> HTH, Bill.
>
>
> On Mon, Sep 2, 2019 at 7:54 AM Paul Procacci  wrote:
> >
> > Was talking to folks over on the #perl6 IRC channel.
> > It appears the recommended way is:
> >
> > sub matching_chars(Str $chars_to_match, Str $str) {
> > # warnings, treats as string not variable
> > $str ~~ /<$_>/ given "<[$chars_to_match]>";
> > }
> >
> > ~Paul
> >
> >
> > On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:
> >>
> >> I found something easy in Perl 5 that's puzzling me in Perl 6-
> specifying a character class via a variable.
> >>
> >> Perl 5:
> >> sub matching_chars {
> >>   (my $chars_to_match, local $_) = @_;
> >>   /([$chars_to_match]+)/
> >> }
> >>
> >> say matching_chars('24680', '19584203'); # says 8420
> >> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
> >>
> >> Perl 6:
> >> sub matching_chars(Str $chars_to_match, Str $_) {
> >> # warnings, treats as string not variable
> >> m/<[$chars_to_match]>/;
> >> }
> >>
> >> How do I get Perl 6 to interpret a variable in the contents of a
> character class?
> >> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd
> think that  Rakudo would use the literal contents of $chars_to_match,
> instead it's using the literal chars "$ c h a r s _ t o _ m a t c h" and
> warning about repeated c, underscore, etc.
> >>
> >> -y
> >
> >
> >
> > --
> > __
> >
> > :(){ :|:& };:
>


-- 
__

:(){ :|:& };:


Re: Variable character class

2019-09-02 Thread Paul Procacci
Was talking to folks over on the #perl6 IRC channel.
It appears the recommended way is:

sub matching_chars(Str $chars_to_match, Str $str) {
# warnings, treats as string not variable
$str ~~ /<$_>/ given "<[$chars_to_match]>";
}

~Paul


On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:

> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying
> a character class via a variable.
>
> Perl 5:
> sub matching_chars {
>   (my $chars_to_match, local $_) = @_;
>   /([$chars_to_match]+)/
> }
>
> say matching_chars('24680', '19584203'); # says 8420
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>
> Perl 6:
> sub matching_chars(Str $chars_to_match, Str $_) {
> # warnings, treats as string not variable
> m/<[$chars_to_match]>/;
> }
>
> How do I get Perl 6 to interpret a variable in the contents of a character
> class?
> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think
> that  Rakudo would use the literal contents of $chars_to_match, instead
> it's using the literal chars "$ c h a r s _ t o _ m a t c h" and warning
> about repeated c, underscore, etc.
>
> -y
>


-- 
__

:(){ :|:& };:


Re: Variable character class

2019-09-01 Thread Paul Procacci
I've actually found some weird inconsistancy while playing with this.

sub matching_chars(Str $x, Str $y) {
my $a := $x;
my $b := $y;
say $y.match: /<[$a]>/;
}

That results in a match of one character, yet:

sub matching_chars(Str $x, Str $y) {
my $a := $x;
my $b := $y;
say $y.match: /<[$x]>/;
}

Results in a match of none of the characters.  Yet, $x and $a should
reference the same object.
Something is broken.

On Sun, Sep 1, 2019 at 2:39 PM yary  wrote:

> Thanks for the ideas. The core issue I'm probing is runtime construction
> of character classes, with an eye towards opening a documentation issue, or
> maybe even an issue against the character class implementation.
>
> Simon's workaround m:g/<{$chars.comb}>+/  is interesting, interpolating a
> list which turns into alternation - but it isn't a character class. Bill's
> proposal with intersecting sets - via a junction, though ∩ aka (&) also
> ought to work - is short and clear, but solves a different problem.
>
> Paul's suggestion should create a character class, but my test doesn't
> work:
>
> #!/usr/bin/env perl6
> use v6;
>
> sub matching_chars(Str $chars_to_match, Str $_) {
> print "Looking for $chars_to_match in '$_'- ";
> my regex x { $chars_to_match ** 1 };
> m/<[]>/;
> }
> say matching_chars('24680', '19584203');
> say matching_chars('24680', '24680x');
>
> $ ./*match.p6*
> Looking for 24680 in '19584203'- Nil
> Looking for 24680 in '24680x'- 「x」
>
> Paul, can you post a more complete working test?
>
> The below works but is "ugly", building up the character class syntax as a
> string and then evaluating it
>
> sub matching_chars(Str $chars_to_match, Str $_) {
>   print "Looking for $chars_to_match in '$_'- ";
>
> m/<{ # Interpolate contents as a regex
>  "<[" ~ # Open the character class
> # Add the char list, with closing bracket backslashed
> $chars_to_match.subst(']','\]' ,:g) ~
> "]>" # Close the character class
>   }>+/; # End interpolation, plus sign for one-or-more
> }
> say matching_chars('24680', '19584203'); # matches 8420
> say matching_chars('+/][', 'Apple ][+//e'); # matches ][+//
>
> I see this as a shortcoming in the character class implementation.
> Specifically the "literal" vs "metacharacter" distinction
> <http://docs.perl6.org/language/regexes#Literals_and_metacharacters> goes
> out the window with enumerated character classes, it behaves like a
> single-quoting. I want all the different interpolation goodness
> <http://docs.perl6.org/language/regexes#Regex_interpolation> to work with
> character classes, and it seems that requires a new syntax.
>
> -y
>
>
> On Sun, Sep 1, 2019 at 9:59 AM Simon Proctor 
> wrote:
>
>> Using a set would be good but it doesn't give you the matching string
>> from the original (which is what I thought was required) otherwise Sets
>> would be my first thought.
>>
>> On Sun, 1 Sep 2019, 17:57 William Michels,  wrote:
>>
>>> Hi Yary and Paul and Simon,
>>>
>>> I ran into the same difficulties as Yary with repeated characters, so
>>> I tried the .unique method. Then after a while, I realized that
>>> problems like this might best be treated as "Set" problems in Perl6.
>>> Note the Set Intersection operator "(&)" below:
>>>
>>> sub matching_chars(Str $a, Str $b) {
>>>my @c = $a.comb.unique;
>>>my @d = $b.comb.unique;
>>>#say @c; say @d;
>>>return @c (&) @d;
>>> }
>>>
>>> say matching_chars("24680", "19584203"); #RETURNS set(0 2 4 8)
>>> say matching_chars('+\/\]\[', 'Apple ][+//e'); #RETURNS set(+ / [ ])
>>>
>>>
>>> https://docs.perl6.org/routine/Set
>>> https://docs.perl6.org/language/operators#infix_(&),_infix_∩
>>>
>>> HTH, Bill.
>>>
>>>
>>> On Sat, Aug 31, 2019 at 8:59 PM Paul Procacci 
>>> wrote:
>>> >
>>> > I'm not entirely sure if this is the correct answer, but if you define
>>> your own custom character class
>>> > with a 'regex' object, you can use that in the grouping.
>>> >
>>> > sub matching_chars(Str $chars_to_match, Str $_) {
>>> > my regex x { $chars_to_match ** 1 };
>>> > m/<[]>/;
&

Re: Variable character class

2019-08-31 Thread Paul Procacci
I'm not entirely sure if this is the correct answer, but if you define your
own custom character class
with a 'regex' object, you can use that in the grouping.

sub matching_chars(Str $chars_to_match, Str $_) {
my regex x { $chars_to_match ** 1 };
m/<[]>/;
}

The above worked for me in the very small testing I did.

~Paul

On Sat, Aug 31, 2019 at 9:54 PM yary  wrote:

> I found something easy in Perl 5 that's puzzling me in Perl 6- specifying
> a character class via a variable.
>
> Perl 5:
> sub matching_chars {
>   (my $chars_to_match, local $_) = @_;
>   /([$chars_to_match]+)/
> }
>
> say matching_chars('24680', '19584203'); # says 8420
> say matching_chars('+\/\]\[', 'Apple ][+//e'); # says ][+//
>
> Perl 6:
> sub matching_chars(Str $chars_to_match, Str $_) {
> # warnings, treats as string not variable
> m/<[$chars_to_match]>/;
> }
>
> How do I get Perl 6 to interpret a variable in the contents of a character
> class?
> From http://docs.perl6.org/language/regexes#Regex_interpolation I'd think
> that  Rakudo would use the literal contents of $chars_to_match, instead
> it's using the literal chars "$ c h a r s _ t o _ m a t c h" and warning
> about repeated c, underscore, etc.
>
> -y
>


-- 
__

:(){ :|:& };:


Re: How do I use ~ with Regex syntax?

2019-08-30 Thread Paul Procacci
Drop the '~'.

$b ~~ s/ .*? "In the year " $a//;

~Paul


On Fri, Aug 30, 2019 at 8:26 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> Yes, I know to just put the variable inside the quote, but
> I'd still like to know how to do it outside the quote;
>
>
> $NewPage ) ~~ s/ .*? "Full Change Log for Version " ~ $YearRev //;
>
> --> Full Change Log for Version " ~ $YearRev⏏ //;
> Couldn't find terminator /
> at /home/linuxutil/./GetUpdates.pl6:4960
> -->  s/ .*? "Full Change Log for Version " ~⏏ $YearRev //;
>  expecting any of:
>  /
>
>
> Many thanks,
> -T
>


-- 
__

:(){ :|:& };:


Re: subnet calculator

2019-01-11 Thread Paul Procacci
http://jodies.de/ipcalc

Download link at bottom of page.

On Fri, Jan 11, 2019 at 3:08 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> Anyone know if someone has written a program like this
> in Perl that will run locally and not require the Internet?
>
> http://www.subnet-calculator.com/
>
>
> Many thanks,
> -T
>


-- 
__

:(){ :|:& };:


Re: Need Golf!

2018-11-08 Thread Paul Procacci
Hi Laurent,

I certainly appreciate the other potential references.  I will certainly
explore them.
You got me thinking now about what would be considered the most optimal.
I'll explore that on my own and if I feel the need report any huge
descrepancies.

Thanks for the response!
~Paul

On Thu, Nov 8, 2018 at 6:06 PM Laurent Rosenfeld <
laurent.rosenf...@googlemail.com> wrote:

> To me, using *map *in such a context is not the best (although it can
> obviously be done).
>
> IMHO, *map *is really intended to generate a list from another list. In
> your case, a *for *loop would make more sense to me. Even the use of *grep
> *(or *first*) would be more natural than *map*.
>
> But that's probably a matter of personal taste.
>
> Best,
> Laurent.
>
>
>
>
>
> Le jeu. 8 nov. 2018 à 23:46, Paul Procacci  a écrit :
>
>> Had time to think about this on the drive home.
>> I've eliminated the for loop which was my goal.
>> Any "better" or "cleaner" way of doing this, I'm all ears.  ;)
>>
>> die Some::Exception.new.throw
>> if %!panels.elems && !%!panels.values.map({
>>   $end_y < .start_y || $start_x > .end_x ||
>>   $start_y > .end_y || $end_x < .start_x
>> }).so;
>>
>>
>> On Thu, Nov 8, 2018 at 4:27 PM Paul Procacci 
>> wrote:
>>
>>> I don't like this:
>>>
>>>
>>> for %!panels<>:k {
>>>  die Some::Exception.new.throw
>>>unless $start_y > %!panels{$_}.end_y   || $end_y   <
>>> %!panels{$_}.start_y ||
>>>   $end_x   < %!panels{$_}.start_x || $start_x >
>>> %!panels{$_}.end_x;
>>>
>>> }
>>>
>>>
>>> In short, I have a objects stored in hash %!panels that contain the
>>> (x,y) coordinates of a square.
>>>
>>> The function above works; throwing an exception if any of the squares
>>> overlap, but I don't like using the for loop here.
>>>
>>> I'm hoping someone here can provide a similar golfed example.
>>>
>>> I've been looking at `map` and `so`, but I just can't get my brain to
>>> work.
>>>
>>>
>>> Thanks in Advance,
>>>
>>> ~Paul
>>>
>>>
>>
>> --
>> __
>>
>> :(){ :|:& };:
>>
>

-- 
__

:(){ :|:& };:


Re: Need Golf!

2018-11-08 Thread Paul Procacci
Had time to think about this on the drive home.
I've eliminated the for loop which was my goal.
Any "better" or "cleaner" way of doing this, I'm all ears.  ;)

die Some::Exception.new.throw
if %!panels.elems && !%!panels.values.map({
  $end_y < .start_y || $start_x > .end_x ||
  $start_y > .end_y || $end_x < .start_x
}).so;


On Thu, Nov 8, 2018 at 4:27 PM Paul Procacci 
wrote:

> I don't like this:
>
>
> for %!panels<>:k {
>  die Some::Exception.new.throw
>unless $start_y > %!panels{$_}.end_y   || $end_y   <
> %!panels{$_}.start_y ||
>   $end_x   < %!panels{$_}.start_x || $start_x >
> %!panels{$_}.end_x;
>
> }
>
>
> In short, I have a objects stored in hash %!panels that contain the
> (x,y) coordinates of a square.
>
> The function above works; throwing an exception if any of the squares
> overlap, but I don't like using the for loop here.
>
> I'm hoping someone here can provide a similar golfed example.
>
> I've been looking at `map` and `so`, but I just can't get my brain to work.
>
>
> Thanks in Advance,
>
> ~Paul
>
>

-- 
__

:(){ :|:& };:


Re: need regex help

2018-08-01 Thread Paul Procacci
\d and  both match Unicode characters as well.
If that's not the intention then it's best to be explicit.

die("Horribly") unless "9.b1" ~~ / <[0-9]+> % '.' /;

Typing from my phone so unable to test the above***


On Thu, Aug 2, 2018, 12:56 AM ToddAndMargo  wrote:

> Hi All,
>
> If there are any letter in the string, I want it to fail
>
>
>
> $ p6 'my $x="9.0v1"; if $x~~/<+alnum>-[]>/ {say "Y";}'
> ===SORRY!===
> Unrecognized regex metacharacter - (must be quoted to match literally)
> at -e:1
> --> my $x="9.0v1"; if $x~~/<+alnum>⏏-[]>/ {say "Y";}
> Unable to parse regex; couldn't find final '/'
> at -e:1
> --> my $x="9.0v1"; if $x~~/<+alnum>-⏏[]>/ {say "Y";}
>
>
>
> What am I doing wrong?
>
> Many thanks,
> -T
>


  1   2   >