Re: The ,= operator

2020-12-06 Thread William Michels via perl6-users
On Sun, Nov 29, 2020 at 6:38 PM Ralph Mellor 
wrote:

> > Zen slicing as a possible way of 'de-containerizing' :
> > https://docs.raku.org/language/subscripts#index-entry-Zen_slices
>
> A zen-slice only affects the single reference it's applied to.
>
> And it is a no op when applied to anything other than a `Scalar`.
>
> So it'll have no effect when applied directly to an array, list, hash,
> or other non `Scalar`.
>
> Perhaps you're thinking "decontainerizing" does something other
> than what it does? It's really a very simple operation. An analogy
> is having a bunch of folk, some of whom always go by their legal
> name, but others who sometimes go by their nickname. And let's
> say there's an operation called "use legal name". When applied
> to a nickname it yields the corresponding legal name. But when
> applied to a legal name it just yields that name, i.e. it's a no op.
>
> That's all that decont does, except the analog to a nickname is
> a `Scalar`, and the analog to a legal name is anything else.
>
> > Even if the ",=" postfix operator were to gain this ability on non-hash
> > objects, then hash-objects would be special-cased in **not** requiring
> > a Zen-sliced decontainerized object on the LHS, so people would have
> > to consider that outcome.
>
> I think the focus on `,=`.is really unfortunate, because the real issue is
> just about what `=` does with a non-`Scalar` on its LHS and a list of
> values on its RHS. It's really got nothing whatsoever to do with `,`.
>
> It just depends on the type of non-`Scalar` on the LHS of `=`.
>
> Look what happens without not a comma in sight:
>
> my %hash = do for ^1 { %hash }
> say %hash; # {}
>
> If it's a hash,, the only sensible thing to do with a list of values that
> may
> each be a pair, or a list of pairs, or an array of pairs, or a hash of
> pairs,
> is to flatten and concatenate all the individual pairs into the hash.
>
> my @array = do for ^1 { @array }
> say @array; # (\Array_73652568 = [Array_73652568])
>
> But if it's an array, the sensible thing to do is to *not* flatten by
> default.
>


This topic was a subject of discussion during our (SF_Perl) Raku Meetup
Group today. We reviewed the three URL references below. One member of our
Meetup went throught the 2017 Advent Calendar reference extensively
(hopefully we'll see a post on that). Afterwards, I played a bit with
indexing constructs. Posting these for posterity's sake:

https://docs.raku.org/language/subscripts#index-entry-Zen_slices
https://docs.raku.org/language/glossary#index-entry-decont
https://perl6advent.wordpress.com/2017/12/02/#theoneandonly

user@mbook:~$ raku
Welcome to 퐑퐚퐤퐮퐝퐨™ v2020.10.
Implementing the 퐑퐚퐤퐮™ programming language v6.d.
Built on MoarVM version 2020.10.

You may want to `zef install Readline` or `zef install Linenoise` or use
rlwrap for a line editor

To exit type 'exit' or '^D'
> my %hash = do for ^1 { %hash }
{}
> my %hash2 = do for ^1 { %hash2<> }
{}
> my %hash3 = do for ^1 { %hash3<*> }
Odd number of elements found where hash initializer expected:
Only saw 1 element
  in block  at  line 1

> my @array = do for ^1 { @array }
(\Array_140587615723064 = [Array_140587615723064])
> my @array2 = do for ^1 { @array2[] }
(\Array_140587615723904 = [Array_140587615723904])
> my @array3 = do for ^1 { @array3[*] }
[()]

Best Regards, Bill.


Re: The ,= operator

2020-12-06 Thread Joseph Brenner
ToddAndMargo via perl6-users  wrote:

> I am a little late to this conversation, but `,=`
> looks a lot like `push` to me.

Yes that was my first impression, if you read ahead a bit in the
discussion you'll see it explained.

In summary: the = shortcuts all work in a precisely parallel way, so

@r ,= 'd';

is the same as:

@r = @r , 'd';

And remember, back in perl-land the default behavior is to flatten,
but in raku arrays don't flatten unless you do something to make it
happen-- by default it's oriented toward building up complex
structures like arrays of arrays.   So with this construct, you end up
with the array containing a circular reference to itself in the first
location, which is highly unlikely to be something you want.

Hashes, on the other hand, *don't* tend to flatten by default, so ,=
does something useful with them.

If you wanted to just add an element to the end, you could do one of these:

@r.append('d');
@r = | @r, 'd';
@r.push('d');

The trouble with that last one though, is you may find it surprising
if you're pushing more than a single element, this will get you an
array inside an array:

@r.push(@a);

This on the other hand, behaves just like an @r.append(@a) would:
@r.push(| @a);


Re: Pointer and OpaquePointer

2020-12-06 Thread Marcel Timmerman

Hi,
I'm afraid that I've done something wrong somewhere else... Changed it 
all back to OpaquePointer and kept the same problem. So I must look into 
it deeper and just use the Pointer instead.


Regards,
Marcel


Re: list of printers

2020-12-06 Thread JJ Merelo
El dom, 6 dic 2020 a las 10:09, ToddAndMargo via perl6-users (<
perl6-us...@perl.org>) escribió:

> On 12/4/20 8:55 PM, ToddAndMargo via perl6-users wrote:
> > On 2020-12-04 20:28, Curt Tilmes wrote:
> >> On Fri, Dec 4, 2020 at 10:52 PM ToddAndMargo via perl6-users
> >>  wrote:
> >>
> >>> This is the C way, although it shows deleted printers as well:
> >>>
> >>> #include 
> >>> #include 
> >>>
> >>> int main() {
> >>> cups_dest_t* dests;
> >>> int nCount = cupsGetDests2(CUPS_HTTP_DEFAULT, );
> >>>
> >>> for (int i = 0; i < nCount; i++) {
> >>> cups_dest_t dest = dests[i];
> >>> std::cout << dest.name << std::endl;
> >>> }
> >>> }
> >>
> >> Roughly translating that into Raku Nativecall (my system has
> >> cupsGetDests(), but not cupsGetDests2() -- you'll have to update to
> >> that)
> >>
> >> use NativeCall;
> >> class CupsDest is repr('CStruct') {
> >>  has Str $.name;   # This is the first field in the struct --
> >> add more if you need them
> >> }
> >>
> >> sub cupsGetDests(Pointer is rw --> int32) is native('cups') {}
> >>
> >> my $ptr = Pointer.new;
> >> my $nCount = cupsGetDests($ptr);
> >>
> >> for ^$nCount -> $i {
> >>  my $dest = nativecast(CupsDest, Pointer.new($ptr + $i *
> >> nativesizeof(Pointer)));
> >>  say $dest.name;
> >> }
> >>
> >
> >
> >
> > Hi Curt,
> >
> > What am I doing wrong?
> >
> > #!/usr/bin/env raku
> >
> > use NativeCall;
> > class CupsDest is repr('CStruct') {
> >  has Str $.name;   # This is the first field in the struct --
> > add more if you need them
> > }
> >
> > sub cupsGetDests(Pointer is rw --> int32) is native('cups') {}
> >
> > my $ptr = Pointer.new;
> > my $nCount = cupsGetDests($ptr);
> >
> > for ^$nCount -> $i {
> >  my $dest = nativecast(CupsDest, Pointer.new($ptr + $i *
> > nativesizeof(Pointer)));
> >  say $dest.name;
> > }
> >
> > $ ListPrinters.pl6
> > Cannot locate native library 'libcups.so': libcups.so: cannot open
> > shared object file: No such file or directory
> >in method setup at
> >
> /opt/rakudo-pkg/share/perl6/core/sources/947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B
>
> > (NativeCall) line 298
> >in block cupsGetDests at
> >
> /opt/rakudo-pkg/share/perl6/core/sources/947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B
>
> > (NativeCall) line 587
> >
> >
> > $ locate libcups.so
> > /usr/lib/libcups.so.2
> > /usr/lib64/libcups.so.2
> >
> >
> > -T
>
> I did the following:
>
> # cd /usr/lib
> # gls libcups
> libcupsimage.so.2
> libcups.so.2
>
> # ln -s libcups.so.2 libcups.so
>

Why did you do so? ;-)

# ls -al libcups*
> -rwxr-xr-x. 1 root root  14612 Nov 10 06:07 libcupsimage.so.2
> lrwxrwxrwx. 1 root root 12 Dec  6 01:03 libcups.so -> libcups.so.2
> -rwxr-xr-x. 1 root root 710236 Nov 10 06:07 libcups.so.2
>
> # cd /usr/lib64
> # ln -s libcups.so.2 libcups.so
> # ls -al libcups*
> lrwxrwxrwx. 1 root root 23 Nov 23 22:09 libcupsfilters.so.1 ->
> libcupsfilters.so.1.0.0
> -rwxr-xr-x. 1 root root 264440 Nov 23 22:09 libcupsfilters.so.1.0.0
> -rwxr-xr-x. 1 root root  15256 Nov 10 06:08 libcupsimage.so.2
> lrwxrwxrwx. 1 root root 12 Dec  6 01:04 libcups.so -> libcups.so.2
> -rwxr-xr-x. 1 root root 686128 Nov 10 06:08 libcups.so.2
>
>
> Running the program now gives:
>
> $ ListPrinters.pl6
> B4350
> (Str)
> Segmentation fault (core dumped)
>

Well, the second $dest clearly has no value in "name". And in the next one
you are getting into a loop and casting pointers and stuff without actually
checking if they're good. It looks like it's pointing nowhere; either the
size is off, or there's any other problem.

Cheers

JJ


-- 
JJ


Re: list of printers

2020-12-06 Thread ToddAndMargo via perl6-users

On 12/4/20 8:55 PM, ToddAndMargo via perl6-users wrote:

On 2020-12-04 20:28, Curt Tilmes wrote:

On Fri, Dec 4, 2020 at 10:52 PM ToddAndMargo via perl6-users
 wrote:


This is the C way, although it shows deleted printers as well:

#include 
#include 

int main() {
cups_dest_t* dests;
int nCount = cupsGetDests2(CUPS_HTTP_DEFAULT, );

for (int i = 0; i < nCount; i++) {
cups_dest_t dest = dests[i];
std::cout << dest.name << std::endl;
}
}


Roughly translating that into Raku Nativecall (my system has
cupsGetDests(), but not cupsGetDests2() -- you'll have to update to
that)

use NativeCall;
class CupsDest is repr('CStruct') {
 has Str $.name;   # This is the first field in the struct --
add more if you need them
}

sub cupsGetDests(Pointer is rw --> int32) is native('cups') {}

my $ptr = Pointer.new;
my $nCount = cupsGetDests($ptr);

for ^$nCount -> $i {
 my $dest = nativecast(CupsDest, Pointer.new($ptr + $i *
nativesizeof(Pointer)));
 say $dest.name;
}





Hi Curt,

What am I doing wrong?

#!/usr/bin/env raku

use NativeCall;
class CupsDest is repr('CStruct') {
     has Str $.name;   # This is the first field in the struct -- 
add more if you need them

}

sub cupsGetDests(Pointer is rw --> int32) is native('cups') {}

my $ptr = Pointer.new;
my $nCount = cupsGetDests($ptr);

for ^$nCount -> $i {
     my $dest = nativecast(CupsDest, Pointer.new($ptr + $i *
nativesizeof(Pointer)));
     say $dest.name;
}

$ ListPrinters.pl6
Cannot locate native library 'libcups.so': libcups.so: cannot open 
shared object file: No such file or directory
   in method setup at 
/opt/rakudo-pkg/share/perl6/core/sources/947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B 
(NativeCall) line 298
   in block cupsGetDests at 
/opt/rakudo-pkg/share/perl6/core/sources/947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B 
(NativeCall) line 587



$ locate libcups.so
/usr/lib/libcups.so.2
/usr/lib64/libcups.so.2


-T


I did the following:

# cd /usr/lib
# gls libcups
libcupsimage.so.2
libcups.so.2

# ln -s libcups.so.2 libcups.so
# ls -al libcups*
-rwxr-xr-x. 1 root root  14612 Nov 10 06:07 libcupsimage.so.2
lrwxrwxrwx. 1 root root 12 Dec  6 01:03 libcups.so -> libcups.so.2
-rwxr-xr-x. 1 root root 710236 Nov 10 06:07 libcups.so.2

# cd /usr/lib64
# ln -s libcups.so.2 libcups.so
# ls -al libcups*
lrwxrwxrwx. 1 root root 23 Nov 23 22:09 libcupsfilters.so.1 -> 
libcupsfilters.so.1.0.0

-rwxr-xr-x. 1 root root 264440 Nov 23 22:09 libcupsfilters.so.1.0.0
-rwxr-xr-x. 1 root root  15256 Nov 10 06:08 libcupsimage.so.2
lrwxrwxrwx. 1 root root 12 Dec  6 01:04 libcups.so -> libcups.so.2
-rwxr-xr-x. 1 root root 686128 Nov 10 06:08 libcups.so.2


Running the program now gives:

$ ListPrinters.pl6
B4350
(Str)
Segmentation fault (core dumped)