The problem is that you are using ~ with an uninitialized Buf/Blob

    my Buf $read;
    $read ~ Buf.new;
    # Use of uninitialized value element of type Buf in string context.

Note that it is not complaining about it being a Buf. It is complaining
about it being uninitialized.

If you initialize it then it works just fine.

    my Buf $read .= new;
    $read ~ Buf.new;

It will also work with ~=

    my Buf $read .= new;
    $read ~= Buf.new( 'a'.ord, 'b'.ord );
    $read ~= Blob.new( 'c'.ord, 'd'.ord );
    say $read.decode; # abcd

It also works with Blob, but you may not want to do that if you plan on
modifying the contents with something like `.subbuf-rw()`.

On Tue, Feb 11, 2020 at 3:56 AM David Santiago <deman...@gmail.com> wrote:

> A 11 de fevereiro de 2020 10:47:34 CET, David Santiago <deman...@gmail.com>
> escreveu:
> >A 11 de fevereiro de 2020 09:46:06 CET, David Santiago <
> deman...@gmail.com> escreveu:
> >>
> >>Hi!
> >>
> >>Can someone explain me why this doesn't work:
> >>
> >>my Blob $read;
> >>$read ~= $socket.read(1024);
> >>
> >>Dies with error:
> >>
> >>X::Buf::AsStr: Cannot use a Buf as a string, but you called the Stringy
> method on it
> >>
> >>This also doesn't work:
> >>
> >>my Buf $read;
> >>$read ~= $socket.read(1024);
> >>
> >>Dies with the same error as above.
> >>
> >>
> >>But this works?
> >>
> >>my Blob $read = Buf.new;
> >>$read ~= $socket.read(1024);
> >>
> >>
> >>Best regards,
> >>David Santiago
> >
> >
> >Hi!
> >
> >Can someone explain me why this doesn't work:
> >
> >my Blob $read;
> >$read ~= $socket.read(1024);
> >
> >Dies with error:
> >
> >X::Buf::AsStr: Cannot use a Buf as a string, but you called the Stringy
> method on it
> >
> >This also doesn't work:
> >
> >my Buf $read;
> >$read ~= $socket.read(1024);
> >
> >Dies with the same error as above.
> >
> >
> >But this works?
> >
> >my Blob $read = Buf.new;
> >$read ~= $socket.read(1024);
> >
> >
> >Best regards,
> >David Santiago
>
>
> Hi!
>
> Can someone explain me why this doesn't work:
>
> my Blob $read;
> $read ~= $socket.read(1024);
>
> Dies with error:
>
> X::Buf::AsStr: Cannot use a Buf as a string, but you called the Stringy
> method on it
>
> This also doesn't work:
>
> my Buf $read;
> $read ~= $socket.read(1024);
>
> Dies with the same error as above.
>
>
> But this works?
>
> my Blob $read = Buf.new;
> $read ~= $socket.read(1024);
>
>
> Best regards,
> David Santiago
>
> --
> Sent from my Android device with K-9 Mail. Please excuse my brevity.
>

Reply via email to