Re: XML module question?

2021-07-16 Thread David Warring
The XML::Writer  module side-steps any
ordering issues by directly serializing data structures to xml:

$ raku -M XML::Writer -e'say XML::Writer.serialize: :wpt[ :lat<42>,
:long<69>, "blah" ]'
blah

On Sat, Jul 17, 2021 at 4:48 AM Bruce Gray 
wrote:

> > On Jul 15, 2021, at 11:02 PM, William Michels via perl6-users <
> perl6-users@perl.org> wrote:
>
> (and I re-ordered):
>
> > Any help appreciated,
> > Thanks, Bill.
> > REFERENCE:  https://unix.stackexchange.com/q/166675/227738
>
>
> > I'm trying to put an answer together for SO, and I keep running into the
> same difficulty. Given a CSV file,
>
> Note: input is a log file, not in CSV format.
>
> > how can one produce an XML file with latitude and longitude values
> properly quoted? Every time I get proper quoting, "lat" and "lon" are
> scrambled (i.e. sometimes "lat" first, sometimes "lat" second).
> ...
> > using the "make-xml()" function from the XML module, I end up scrambling
> "lat/lon”:
>
> You are using the correct XML module, and you are using it correctly.
>
> Similar to a Raku hash, the attributes in an XML start-tag are *unordered*.
> While the inconsistant ordering may be displeasing to our human eyes, any
> program reading XML should treat your current output identically to your
> desired output.
>
> In Raku-speak, these two hashes have the same contents:
> my %wpt1 = lat => 'A', lon => 'z';
> my %wpt2 = lon => 'z', lat => 'A’;
>
> Designers of XML libraries are reluctant to add the ability to force an
> ordering; it would lead to XML-reading programs making improper assumptions.
> Like many major XML libraries in other languages, I see no such ability in
> the Raku XML module.
> See:
> https://www.w3.org/TR/REC-xml/#sec-starttags
> Note that the order of attribute specifications
> in a start-tag or empty-element tag is not significant.
>
> https://stackoverflow.com/questions/57756005/writing-xml-attributes-and-namespace-declarations-in-a-specific-order
>
> https://community.adobe.com/t5/coldfusion/maintain-attribute-order-in-xml-with-a-structure/m-p/3838398
>
> > Conversely, if I manage to restrict "lat/lon" to the correct order, I
> lose the equals sign and proper (value) quotation.
>
>
> Unless you just hardcode a template and insert the lat/lon/datetime values
> (never a good idea), you will have to live with the varying order.
> I will point out that the XML library also does not output the multi-line
> indentation that the OP shows; it is irrelevant in XML.
>
>
> > $ raku -MXML -e 'my @xml = do for lines() {make-xml("wpt",
> :lat(.words[5].subst(";")), :lon(.words[8]), \("time",.words[0..2]))}; .put
> for @xml;’  lat_lon.txt
>
> Minor critiques:
> That use of `@array = do for lines() {…}` is more commonly written as
> `@array = lines().map: {…}`
> raku -MXML -e 'my @xml = lines().map: {make-xml("wpt",
> :lat(.words[5].subst(";")), :lon(.words[8]), \("time",.words[0..2]))}; .put
> for @xml;'  lat_lon.txt
>
> Nothing is gained by creating an array, just to print each line of the
> array, so your can just `put` each time you construct an XML line.
> raku -MXML -e 'for lines() { put make-xml("wpt",
> :lat(.words[5].subst(";")), :lon(.words[8]), \("time",.words[0..2])) }'
> lat_lon.txt
>
> When all the output is per-line based on the input, the `-p` or `-n` flag
> removes the need for `for lines`.
> raku -MXML -ne 'put make-xml("wpt", :lat(.words[5].subst(";")),
> :lon(.words[8]), \("time",.words[0..2]))'  lat_lon.txt
>
>
>
> > So far, I can correctly swap the date/time to the end:
> >
> > $ raku -MXML -e 'my @reordered = do for lines()
> {(.words[3..*],.words[0..2])}; .put for @reordered;’  lat_lon.txt
>
> I am sure that `-MXML` is leftover from other experiments.
> Removing `@reordered` and switching to `-n` like my critiques above, this
> becomes:
> raku -ne 'put (.words[3..*],.words[0..2]);' lat_lon.txt
> To “wow” the SO crowd, you might note that @array[N..*],@array[0..N-1] has
> a shortcut in Raku (.rotate), making this the tightest of all the solutions
> I see so far:
> raku -ne 'put .words.rotate(3);'  lat_lon.txt
>
> --
> Hope this helps,
> Bruce Gray (Util of PerlMonks)
>
>


Re: ^methods doesn't show all methods

2021-02-16 Thread David Warring
I'm getting this on Rakudo blead.

$ raku -v
Welcome to Rakudo(tm) v2020.12-129-g291cc5f39.
Implementing the Raku(tm) programming language v6.d.
Built on MoarVM version 2020.12-100-gc93531608.
$ raku -e'my $s = set 2, 4, 6; say $s.^methods>>.name.sort;'
(ACCEPTS ASSIGN-KEY AT-KEY BUILDALL Bag BagHash Baggy Bool Capture
DELETE-KEY EXISTS-KEY Hash Int Map Mix MixHash Mixy Num Numeric Real STORE
Set SetHash Setty Str WHICH antipairs default elems fmt gist grab grabpairs
hash item iterator keyof keys kv list maxpairs minpairs new new-from-pairs
of pairs pick pickpairs raku roll total values)

Which does seem to include associative methods
- David

On Wed, Feb 17, 2021 at 8:25 AM Joseph Brenner  wrote:

> Set objects have Associative methods:
>
>   my $s = set 2, 4, 6;
>   say $s.keys;   # (4 2 6)
>
> But I don't see them in the list from .^methods:
>
>   say $s.^methods;
>   # (menu default pick minpairs Setty grabpairs SET-SELF raku
> Method+{is-nodal}.new Real Baggy iterator keyof Method+{is-nodal}.new
> Method+{is-nodal}.new Method+{is-nodal}.new Mixy of grab
> Method+{is-nodal}.new ACCEPTS new elems Method+{is-nodal}.new Str
> Method+{is-nodal}.new WHICH Bool RAW-HASH fmt new-from-pairs Numeric
> total maxpairs Method+{is-nodal}.new Capture Method+{is-nodal}.new
> Method+{is-nodal}.new Method+{is-nodal}.new Int gist
> Method+{is-nodal}.new Method+{is-nodal}.new Method+{is-nodal}.new
> Method+{is-nodal}.new Num Method+{is-nodal}.new Method+{is-nodal}.new
> Method+{is-nodal}.new roll pickpairs STORE BUILDALL)
>
>   say so $s.^methods.gist.grep(/<>/); # False
>
> I'm using the latest rakudo star release: v2020.10.
>
> Has this been fixed in later versions of Raku?
>


Re: Is LibraryMake still current?

2019-12-22 Thread David Warring
Hi Fernando,
I'm still in the habit of using LibraryMake. E.g. for
https://github.com/p6-xml/LibXML-raku released in this last few months.
Regards,
David

On Sun, Dec 22, 2019 at 7:47 AM Fernando Santagata <
nando.santag...@gmail.com> wrote:

> Hello,
>
> What can I use to help building native code for a Raku module?
> Is LibraryMake still the preferred method or there's something else to use
> nowadays?
>
> Thanks!
>
> --
> Fernando Santagata
>


Re: Fastest way to convert from a Buf to a Str?

2019-02-03 Thread David Warring
Are all characters in the range 0-255, ie latin-1 characters?

You could then try: my $str =  $buf.decode("latin-1");

There's one potential  issue if your data could contain DOS end of lines
("\r\n"), which will get translated to a single logical "\n" in the decoded
string.

- David


On Sun, Feb 3, 2019 at 7:16 PM Brad Gilbert  wrote:

> This:
>
> for ( @$BinaryFile ) -> $Char { $StrFile ~= chr($Char); }
>
> is better written as
>
> my $StrFile = $BinaryFile.map(*.chr).reduce(* ~ *);
>
> It is also exactly equivalent to just e
>
> # if $BinaryFile is a Buf
> my $StrFile = $BinaryFile.decode('latin1');
>
> # if it isn't
> my $StrFile = Buf.new($BinaryFile).decode('latin1');
>
> If you don't otherwise need $BinaryFile
>
> my $fh = open 'test', :enc('latin1');
> my $StrFile = $fh.slurp;
>
> or
>
> my $StrFile = 'test'.IO.slurp(:enc('latin1'));
>
> ---
>
> Buf and Str used to be treated more alike, and it was very confusing.
>
> There should be more methods on Buf that work like the methods on Str,
> but that is about it.
>
> Having a string act like a buffer in Modula 2 probably works fine
> because it barely supports Unicode at all.
>
> Here is an example of why it can't work like that in Perl6:
>
> my $a = 'a';
> my $b = "\c[COMBINING ACUTE ACCENT]";
>
> my $c = $a ~ $b;
> my $d = $a.encode ~ $b.encode;
> my $e = Buf.new($a.encode) ~ Buf.new($b.encode);
>
> say $a.encode; # utf8:0x<61>
> say $b.encode; # utf8:0x
>
> say $c.encode; # utf8:0x
>
> say $d; # utf8:0x<61 CC 81>
> say $e; # Buf:0x<61 CC 81>
>
> Notice that `$c.encode` and `$d` are different even though they are
> made from the same parts.
> `$d` and `$e` are similar because they are dealing with lists of
> numbers not strings.
>
> On Sat, Feb 2, 2019 at 9:23 PM ToddAndMargo via perl6-users
>  wrote:
> >
> > Hi All,
> >
> > I need to read a file into a buffer (NO CONVERSIONS!)
> > and then convert it to a string (again with no
> > conversions).
> >
> > I have been doing this:
> >
> > for ( @$BinaryFile ) -> $Char { $StrFile ~= chr($Char); }
> >
> > But it takes a bit of time.  What is the fastest way to do this?
> >
> > I guess there is not a way to create/declare a variable that is
> > both Buf and Str at the same time?  That would mean I did not
> > have to convert anything.  I use to get away with this under
> > Module 2 all the time.
> >
> > $ p6 'my $B = Buf.new(0x66, 0x66, 0x77); $B.Str ~= "z";'
> > Cannot use a Buf as a string, but you called the Str method on it
> >in block  at -e line 1
> >
> > $ p6 'my $B = Buf.new(0x66, 0x66, 0x77); Str($B) ~= "z";'
> > Cannot use a Buf as a string, but you called the Str method on it
> >in block  at -e line 1
> >
> >
> > Many thanks,
> > -T
> >
> > --
> > 
> > A computer without Microsoft is like
> > a chocolate cake without the mustard
> > 
>


Re: Are 64 bit natives universally available?

2017-08-27 Thread David Warring
Thanks Liz,
I just had a momentary concern that they might not be available on Win32 or
other 32 bit platforms.
Roast confirms that they're available everywhere as well.

Thanks,
David

On Mon, Aug 28, 2017 at 9:08 AM, Elizabeth Mattijsen  wrote:

> They have been available since Christmas, afaik.
>
> > On 27 Aug 2017, at 22:42, David Warring  wrote:
> >
> > Quick question.
> >
> > I just want to doublle check that int64,  uint64 are universally
> available via Perl 6, before introducing them into modules. e.g.
> > % perl6 -e'my uint64 $n = 99; say $n'
> > 99
> >
> > - David
>


Are 64 bit natives universally available?

2017-08-27 Thread David Warring
Quick question.

I just want to doublle check that int64,  uint64 are universally available
via Perl 6, before introducing them into modules. e.g.
% perl6 -e'my uint64 $n = 99; say $n'
99

- David


Re: Bi-directional communication with another process

2017-07-27 Thread David Warring
Thanks Timo,
A Proc::Async example, after reading the doco. Agree, that't better, even
for the simple case :-)
- David

my $proc = Proc::Async.new('sh', '-c', 'for x in `seq 1 1` ; do echo
"o$x"; echo "e$x" 1>&2; done');

# subscribe to new output from out and err handles:
$proc.stdout.tap(-> $v { print "o:$v" }, quit => { say 'caught exception '
~ .^name });
$proc.stderr.tap(-> $v { print "e:$v" });

say "Starting...";
my $promise = $proc.start;

# wait for the external program to terminate
await $promise;
say "Done.";


On Fri, Jul 28, 2017 at 12:42 PM, Timo Paulssen  wrote:

> We have Proc::Async which removes the need for the select call itself
>


Re: Bi-directional communication with another process

2017-07-27 Thread David Warring
Perl 5 and C have the select call that lets you determine which of a group
of file-descriptor are ready for reading and writing. I thought it might be
useful here.

https://en.wikipedia.org/wiki/Select_(Unix)

I've found a module by Tadzik, https://github.com/tadzik/IO-Select, but
it's looking defunct and Parrot specifc.

Are we missing a working 'select' call?
- David

On Fri, Jul 28, 2017 at 12:04 PM, Brandon Allbery 
wrote:

> On Thu, Jul 27, 2017 at 7:49 PM, Norman Gaywood 
> wrote:
>
>> my $input = q:to/EOS/;
>> line of text
>> another line
>> EOS
>>
>> my $cat  = run 'cat', '-n', :in($input.print), :out;
>> my $output = $cat.out.get;
>> $cat.in.close;
>> $cat.out.close;
>>
>> say "done";
>> say $output;
>>
>> But that is not correct. I seem to be missing something.
>>
>
> :in, :out, :err want a filehandle (or will create one if you specify True,
> which is what pair syntax gives you if you specify one of them without a
> filehandle). Not a string; and .print applied to a Str does not turn the
> string into a filehandle, it sends the string to the filehandle in $*OUT.
>
> my $cat = run 'cat', '-n', :in, :out;
> $cat.in.print($input); # this .print, on IO::Handle, prints the string to
> that handle
> $cat.in.close;
> my $output = $cat.out.slurp: :close;
>
> Note that you need to be very careful to avoid the "open3" deadlock: pipes
> have limited buffers, and if you do this all in one thread when the data is
> large enough to block on write, and the receiving program then blocks on
> write back to you, you will deadlock. No language can protect you from
> this. The solution is that either the writer or the reader (or both) needs
> to be in its own thread so that if one blocks, the other can keep running
> and eventually clear the block.
>
> --
> brandon s allbery kf8nh   sine nomine
> associates
> allber...@gmail.com
> ballb...@sinenomine.net
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>


Re: ding!

2017-05-31 Thread David Warring
Does printing an ASCII BEL character do what you want?

perl6 -e'print "\x7"'


On Thu, Jun 1, 2017 at 3:23 PM, ToddAndMargo  wrote:

> Hi All,
>
> Does Perl 6 have a build in "ding" sound, or do I
> need to make a system call (which I do all
> the time in bash script)?
>
> Many thanks,
> -T
>
> --
> ~
> When we ask for advice, we are usually looking for an accomplice.
>--  Charles Varlet de La Grange
> ~
>


Re: Union

2016-04-12 Thread David Warring
Hi Marcel,
Not that I know of.
With regard to your original question. NativeCall has nativecast (as above)
for casting and a CUnion representation for unions. There are some tests in
https://github.com/rakudo/rakudo/blob/nom/t/04-nativecall/13-union.t for
this.

NativeCall is letting you get pretty close to the metal. Should be fast,
but the same dangers and caveats apply, to doing this directly in C.
- David


On Tue, Apr 12, 2016 at 6:51 PM, JuhiMarcel LangbroekTimmerman <
mt195...@gmail.com> wrote:

> Hi David,
>
> Thanks very much for your code, I will try it when I get home at the end
> of the week although I only have little endian Intell chips. I will have to
> look around for others. Btw, are there still chips storing big endian
> paired in two bytes? So your example number would become 0x23, 0x01, 0x67,
> 0x45 ?
>
> Thanks again,
> Marcel
>
> On April 11, 2016 9:45:13 PM David Warring 
> wrote:
>
>> Hi Marcel,
>> With regard to checking for endianess. I don't think there's anything
>> built in NativeCall that directly determines this, but hopefuly the
>> following should do it, without resorting to a C compiler.
>>
>> use NativeCall;
>> sub little-endian returns Bool {
>> my $i = CArray[uint32].new: 0x01234567;
>> my $j = nativecast(CArray[uint8], $i);
>> say sprintf("j[0]:%x j[3]:%x", $j[0], $j[3]);
>> $j[0] == 0x67;
>> }
>>
>> say "little-endian:" ~ little-endian;
>>
>> I've only tested this on a little endian architecture.
>>
>> Source: I ported the 'C' answer to this SO question:
>> http://stackoverflow.com/questions/12791864/c-program-to-check-little-vs-big-endian
>>
>>
>>
>> On Tue, Apr 12, 2016 at 6:30 AM, Marcel Timmerman 
>> wrote:
>>
>>> Hi
>>>
>>> Thanks for your answer. I was thinking in perl6, I should have been more
>>> explicit. At the moment I am converting Num to a float representation in
>>> my
>>> BSON module and was wondering if there where easier ways and maybe
>>> faster too.
>>>
>>> Regards,
>>> Marcel
>>>
>>>
>>>
>>>
>>>
>>>
>>> --- Forwarded message ---
>>> From: Marcel Timmerman 
>>> Date: April 11, 2016 8:28:22 PM
>>> Subject: Re: Union
>>> To: Parrot Raiser <1parr...@gmail.com>
>>>
>>> Hi
>>> Thanks for your answer. I was thinking in perl6, I should have been more
>>> explicit. At the moment I am converting Num to a float representation in
>>> my
>>> BSON module and was wondering if there where easier ways and maybe
>>> faster too.
>>>
>>> Regards,
>>> Marcel
>>>
>>>
>>>
>>>
>>>
>>


Re: Union

2016-04-11 Thread David Warring
Hi Marcel,
With regard to checking for endianess. I don't think there's anything built
in NativeCall that directly determines this, but hopefuly the following
should do it, without resorting to a C compiler.

use NativeCall;
sub little-endian returns Bool {
my $i = CArray[uint32].new: 0x01234567;
my $j = nativecast(CArray[uint8], $i);
say sprintf("j[0]:%x j[3]:%x", $j[0], $j[3]);
$j[0] == 0x67;
}

say "little-endian:" ~ little-endian;

I've only tested this on a little endian architecture.

Source: I ported the 'C' answer to this SO question:
http://stackoverflow.com/questions/12791864/c-program-to-check-little-vs-big-endian



On Tue, Apr 12, 2016 at 6:30 AM, Marcel Timmerman  wrote:

> Hi
>
> Thanks for your answer. I was thinking in perl6, I should have been more
> explicit. At the moment I am converting Num to a float representation in
> my
> BSON module and was wondering if there where easier ways and maybe faster
> too.
>
> Regards,
> Marcel
>
>
>
>
>
>
> --- Forwarded message ---
> From: Marcel Timmerman 
> Date: April 11, 2016 8:28:22 PM
> Subject: Re: Union
> To: Parrot Raiser <1parr...@gmail.com>
>
> Hi
> Thanks for your answer. I was thinking in perl6, I should have been more
> explicit. At the moment I am converting Num to a float representation in my
> BSON module and was wondering if there where easier ways and maybe faster
> too.
>
> Regards,
> Marcel
>
>
>
>
>


Re: Custom accessor compose via traits

2015-07-30 Thread David Warring
Just to give a bit more background.

If you've ever looked at a PDF, it's mostly constructed of fairly
conventional data structures such as arrays, dictionaries, names (hash
keys), byte-stings and numbers. These mostly map reasonably well to Perl
6's built in types.

However, these are then organized into indirect objects, e.g.

3
 0 obj
<< /Type /Pages /Count 1 /Kids [ 4 0 R ] >>
endobj
4 0 obj
<< /Type /Page /MediaBox [ 0 0 420 595 ] >>

endobj



Which is roughly equivalent to:

my $Page = { :Type, :MediaBox[0, 0, 420, 595 ] };
my $Pages = { :Type , :Count(1) };

Or at least should be this simple if we can hide indirect objects. These
only need to be handled at a low level when reading or writing to PDF's.

On top of that gradual typing can be used for validation. MediaBox for
example should always be present in Page of objects and is always an array.

At least that's the theory. I've got an experimental module under
construction over at https://github.com/p6-pdf/perl6-PDF-Tools.

Field access need to be flexible. It's sometimes the case that user code
will need to get at additional entries which are specified.

It's got a pretty simple tied interface to arrays and hashes, which are a
bit more evolved than the above.

hash methods are also overrride: AT-KEY ( $foo) , and ASSIGN-KEY
($foo = 42)

It's an experiment, which is so far, progressing pretty well, the above is
just a nit.
- David

On Fri, Jul 31, 2015 at 9:47 AM, David Warring 
wrote:

> The example below is combines traits (S14)  Lvalue subs (S06) to create a
> base class SimpleTiedHash that ties Attributes to the hash using the
> 'Entry' accessor.
>
> PDFCatalog is an example instance class.
>
> This mostly works well. But SimpleTiedHash currently has a hacky way of
> overriding the built-in accessor.
>
> -- The :entry trait detects then overrides the Attribute `has_accessor
> property, setting it back to false. This `fools` Perl6 class system into
> not generating a standard accessor.
>
> -- The SimpleTiedHash.compose method only needs to be invoked once per
> class instance initialization, but is currently getting called too late and
> repeatedly via the  new method.
>
> Is there a better way of adding custom accessors from traits, just once
> when classes are being initialized?
>
> Code sample below
> - David
> ==
> my role TiedAtt {
> has Bool $.is-entry = True;
> has Bool $.gen-accessor is rw;
> has method has_accessor { False }
> }
>
> multi trait_mod:(Attribute $att is rw, :$entry!) is export(:DEFAULT) {
> # fool rakudo into not generating a standard accessor
>
> my $gen-accessor = $att.has_accessor;
> $att does TiedAtt;
> $att.gen-accessor = $gen-accessor;
> }
>
> class SimpleTiedHash
> is Hash {
> # base class
>
> sub att-accessor($obj, Str $key, Attribute $att) {
> Proxy.new(
> FETCH => method {
> $obj{$key};
> },
> STORE => method ($val is copy) {
> # simple typecheck
>
> die "$key: {$val.perl} has wrong type"
> unless $val ~~ $att.type;
> $obj{$key} = $val;
> });
> }
>
> # create accessors
>
> method compose {
> for self.^attributes.grep({ .name ~~ /^'$!'<[A..Z]>/ &&
> .can('is-entry'\
> ) }) -> $att {
> my $key = $att.name.subst(/^'$!'/, '');
> warn "setting up attribute $key";
> if $att.gen-accessor &&  ! self.^declares_method($key) {
> $att.set_rw;
> self.^add_method( $key, method {
> att-accessor(self, $key, $att ) } );
> }
> }
> }
>
>
> method new(|c) {
> my $obj = callsame;
> # happening too late.
>
> # should ideally be invoked once during class initialisation
>
> warn "composing...";
> $obj.compose();
> $obj;
> }
> }
>
> class PDFCatalog
> is SimpleTiedHash {
>  # data instance class
>
>
> has Str $.Version is entry;
> has Hash $.Pages is entry;
> method ping{say 42}
>
>
> }
>
> my $cat = PDFCatalog.new;
> $cat.ping;
> $cat.Version = '1.3';
> $cat.Pages = { :Count(0), :Kids[] };
>
> say $cat.Version;
> say $cat;
> say $cat.Pages.perl;
>
>


Custom accessor compose via traits

2015-07-30 Thread David Warring
The example below is combines traits (S14)  Lvalue subs (S06) to create a
base class SimpleTiedHash that ties Attributes to the hash using the
'Entry' accessor.

PDFCatalog is an example instance class.

This mostly works well. But SimpleTiedHash currently has a hacky way of
overriding the built-in accessor.

-- The :entry trait detects then overrides the Attribute `has_accessor
property, setting it back to false. This `fools` Perl6 class system into
not generating a standard accessor.

-- The SimpleTiedHash.compose method only needs to be invoked once per
class instance initialization, but is currently getting called too late and
repeatedly via the  new method.

Is there a better way of adding custom accessors from traits, just once
when classes are being initialized?

Code sample below
- David
==
my role TiedAtt {
has Bool $.is-entry = True;
has Bool $.gen-accessor is rw;
has method has_accessor { False }
}

multi trait_mod:(Attribute $att is rw, :$entry!) is export(:DEFAULT) {
# fool rakudo into not generating a standard accessor

my $gen-accessor = $att.has_accessor;
$att does TiedAtt;
$att.gen-accessor = $gen-accessor;
}

class SimpleTiedHash
is Hash {
# base class

sub att-accessor($obj, Str $key, Attribute $att) {
Proxy.new(
FETCH => method {
$obj{$key};
},
STORE => method ($val is copy) {
# simple typecheck

die "$key: {$val.perl} has wrong type"
unless $val ~~ $att.type;
$obj{$key} = $val;
});
}

# create accessors

method compose {
for self.^attributes.grep({ .name ~~ /^'$!'<[A..Z]>/ &&
.can('is-entry'\
) }) -> $att {
my $key = $att.name.subst(/^'$!'/, '');
warn "setting up attribute $key";
if $att.gen-accessor &&  ! self.^declares_method($key) {
$att.set_rw;
self.^add_method( $key, method {
att-accessor(self, $key, $att ) } );
}
}
}


method new(|c) {
my $obj = callsame;
# happening too late.

# should ideally be invoked once during class initialisation

warn "composing...";
$obj.compose();
$obj;
}
}

class PDFCatalog
is SimpleTiedHash {
 # data instance class


has Str $.Version is entry;
has Hash $.Pages is entry;
method ping{say 42}


}

my $cat = PDFCatalog.new;
$cat.ping;
$cat.Version = '1.3';
$cat.Pages = { :Count(0), :Kids[] };

say $cat.Version;
say $cat;
say $cat.Pages.perl;


Re: Need Help with Perl 6 Module Test::Builder

2015-04-01 Thread David Warring
Hi Tom,
I'm seeing the failure(s) as well.

I've put in a PR that hopefully addresses this issue -
https://github.com/soh-cah-toa/p6-test-builder/pull/2.

While we're waiting for the author, you can try checkout out, and building
https://github.com/dwarring/p6-test-builder.git

Cheers,
David


On Thu, Apr 2, 2015 at 3:51 AM, Tom Browder  wrote:

> I need Perl 6 module Test::Builder to continue porting CPAN Perl 5
> module Geo::Ellipdoid to Perl 6.
>
> Test::Builder currently is in the Task::Star module but it fails
> during the build process with Rakudo 2015.03 (it did NOT fail with
> Rakudo 2015.02).
>
> I have filed a bug report at the github source repo
> (https://github.com/soh-cah-toa/p6-test-builder), and have attempted
> to contact the original author (Kevin Polulak, Email:
> kpolu...@gmail.com, IRC: soh_cah_toa) with no success yet.
>
> The offending line with 'make test' is shown in the error message:
>
> ===SORRY!=== Error while compiling lib/Test/Builder/Plan.pm
> Virtual call $.expected may not be used on partially constructed objects
> at lib/Test/Builder/Plan.pm:102
> --> alid or missing plan!' unless $.expected�.defined;
> expecting any of:
> prefix or term
> make: *** [blib/lib/Test/Builder/Plan.pm.moarvm] Error 1
>
> Then, when I comment out the offending line, I get this with 'make test':
>
> ===SORRY!=== Error while compiling lib/Test/Builder.pm
> The following packages were stubbed but not defined:
> Test::Builder
> at lib/Test/Builder.pm:287
> --> done unless $TEST_BUILDER.done_testing }�
> expecting any of:
> statement end
> make: *** [blib/lib/Test/Builder.pm.moarvm] Error 1
>
> And I, a mere noob, have no idea how to proceed.  I think that it will
> take some careful study of the whole package to see how the class
> constructors interrelate, but that will take some time.  Could the
> singleton use be the problem?
>
> In the meantime, does anyone have any idea what might have triggered
> the failure between Rakudo 2015.02 and 2015.03?  Were there any
> significant changes in the class construction that might be a place to
> look?  Singleton use?
>
> I did re-visit at the Rakudo 2015.03 announcement and didn't notice
> anything obvious that seemed to affect the modules involved.
>
> Final questions:
>
> 1. Am I the only person seeing this failure?
>
> 2. If the failure continues (assuming  I'm not the only one, shouldn't
> it be marked bad and removed from Task::Star?
>
> Thanks for any ideas.
>
> Cheers!
>
> -Tom
>


Re: match an empty string?

2014-07-12 Thread David Warring
Hi Marc,
This looks like a Perl 5 complementary character match:  [^:]*

But it means something quite different in Perl6. From S05


Bracket rationalization
  [ ... ] is no
longer a character class. It now delimits a non-capturing group.

Changed metacharacters
^ and $ now
always match the start/end of a string

Backtracking control
To force the
preceding atom to do no backtracking, use a single :

In summary, this pattern is repeatably matching an empty start-of-string.

A Perl 6 equivalent is:

token gecos { <- [':']>* }

This matches a complementary character class; all but ':'.

Also, the  and  rules aren't fully matching their test input
at the moment.

Cheers,
- David


On Sat, Jul 12, 2014 at 10:07 AM, Marc Chantreux  wrote:

> hello,
>
> using perl6 version 2014.04 built on MoarVM version 2014.04
>
> i'm trying to write a grammar to parse the result of a `getent passwd`
> but when i test the gecos (which could be empty), i fall into a loop.
>
> the pb appears whenever i try to match something that can be empty using
> the * quantifier.
>
> I guess i miss a simple point there but i can't figure out what. Any
> help (or feedback on any other part of the code) would be appreciated.
>
> regards
>
> use v6;
> use Test;
>
> grammar PAccountDB {
> token TOP { ^ *  $ }
> token account {
> 
> ':' 
> ':' 
> ':' 
> ':' 
> ':' 
> ':' 
> \n
> }
> token login  { <[a..z]>+ }
> token passwd { 'x' }
> token numid  { \d+ }
> token uid{ <.numid> }
> token gid{ <.numid> }
> token gecos  { [^:]* }
> token path   { '/' <-[\x00'/']>+ }
> token home   { <.path> }
> token shell  { <.path> }
> }
>
> for
> ( uid   => '445'
> , gid   => '445'
> , shell => '/bin/false'
> , home  => '/home/mc'
> # , gecos => 'server account,,,'
> , login => 'root' ) {
> my ( $rule, $candidate ) = .kv;
> ok ?PAccountDB.subparse( $candidate, :rule($rule) )
> , "subrule $rule matches $candidate";
> }
>
>
> --
> Marc Chantreux
> Université de Strasbourg, Direction Informatique
> 14 Rue René Descartes,
> 67084  STRASBOURG CEDEX
> ☎: 03.68.85.57.40
> http://unistra.fr
> "Don't believe everything you read on the Internet"
> -- Abraham Lincoln
>


Re: Gather, take, newbie questions

2013-12-11 Thread David Warring
Hi Jacinta,
I get a bit further with my build of Perl 6, which at least runs:

% perl6 fib.pl
0

Are you maybe using an old Perl6? Try:

% perl6 -e'say $*PERL'

("name" => "rakudo", "compiler" => {"name" => "rakudo", "ver" =>
"2013.11-22-g262e600", "release-number" => "", "build-date" =>
"2013-12-05T22:52:45Z", "codename" => ""}).hash


> ===SORRY!===
> Confused at line 19, near "}\n\nsub fib"
>

- David


Re: Perl 6 / Rakudo unicode code point ranges

2013-02-20 Thread David Warring
Hi Will,

On Wed, Feb 20, 2013 at 3:52 AM, Will Coleda  wrote:

> The code points giving you trouble are 0xFDD0..0xFDEF:
>
>
> http://stackoverflow.com/questions/5188679/whats-the-purpose-of-the-noncharacters-ufdd0-to-ufdef
>
> You can split this into two ranges to avoid the problematic points (and
> could use this to combine the distinct ranges you have above.)
>
> $  perl6 -e 'say ?("\c[0xFDCF]" ~~
> /<[\c[0xE000]..\c[0xFDCF]\c[0xFDF0]..\c[0xFFFD]]>/)'
> True
>

Thanks for clarifying that.

>
> Note that if you have invalid UTF-8 input, though, you'll still get the
> invalid character error, so you'll need to deal with that before trying to
> use the rule.
>
> $  perl6 -e 'say ?("\c[0xFDD0]" ~~
> /<[\c[0xE000]..\c[0xFDCF]\c[0xFDF0]..\c[0xFFFD]]>/)'
> ===SORRY!===
> Invalid character for UTF-8 encoding
>
> Hope this helps.
>
>
> Yes I think so. The rule seems to be trying to step around invalid
code-points; and every thing that it rejects blows up when you try to
encode it anyway.

So as long as I'm only parsing encoded strings, the following much simpler
rule should suffice:

token nonascii   {<- [\x0..\x7F]>}

(also using \x7F rather than \c[0x7F] - as suggested)

Thanks,
- David


> On Mon, Feb 18, 2013 at 11:29 PM, David Warring 
> wrote:
>
>> Hi Guys,
>> A quick question.
>>
>> I'm trying to interpret unicode code-point ranges from the CSS 3 spec -
>> http://www.w3.org/TR/css3-syntax/#CHARSETS
>>
>> The rule in question is
>>
>> nonascii :== #x80-#xD7FF #xE000-#xFFFD #x1-#x10
>>
>> Where (I think) these are unicode code-point ranges.
>>
>> The latest rakudo build is fine with:
>>
>>
>> % perl6 -e perl6 -e '/<[\c[0x80]..\c[0xD7FF]]>/'
>>
>>
>> ...but doesn't like the second (or third) range:
>>
>>
>> % perl6 -e '/<[\c[0xE000]..\c[0xFFFD]]>/'
>> ===SORRY!===
>> Invalid character for UTF-8 encoding
>>
>>
>> ...the individual code points are ok:
>>
>>
>> % perl6 -e '/<[\c[0xE000]]>/'
>> % perl6 -e '/<[\c[0xFFFD]]>/'
>>
>>
>> I'm think I'm getting the above error because not all unicode code-points
>> are defined for the range xE000 to xFFFD - see
>> http://www.utf8-chartable.de/unicode-utf8-table.pl  .
>>
>> I'm just having a problem implementing a concise regex/grammar rule for
>> the
>> above. Looking for advice.
>>
>> Cheers,
>> David Warring
>>
>
>
>
> --
> Will "Coke" Coleda
>


Perl 6 / Rakudo unicode code point ranges

2013-02-18 Thread David Warring
Hi Guys,
A quick question.

I'm trying to interpret unicode code-point ranges from the CSS 3 spec -
http://www.w3.org/TR/css3-syntax/#CHARSETS

The rule in question is

nonascii :== #x80-#xD7FF #xE000-#xFFFD #x1-#x10

Where (I think) these are unicode code-point ranges.

The latest rakudo build is fine with:


% perl6 -e perl6 -e '/<[\c[0x80]..\c[0xD7FF]]>/'


...but doesn't like the second (or third) range:


% perl6 -e '/<[\c[0xE000]..\c[0xFFFD]]>/'
===SORRY!===
Invalid character for UTF-8 encoding


...the individual code points are ok:


% perl6 -e '/<[\c[0xE000]]>/'
% perl6 -e '/<[\c[0xFFFD]]>/'


I'm think I'm getting the above error because not all unicode code-points
are defined for the range xE000 to xFFFD - see
http://www.utf8-chartable.de/unicode-utf8-table.pl  .

I'm just having a problem implementing a concise regex/grammar rule for the
above. Looking for advice.

Cheers,
David Warring