How do I Stringy a Buf?

Hi All,

Yes, I know there are other ways to read a file.  I
have a specific reason for using `read`.

How do I properly turn a Buf into a Str (all the bytes will
have been tested to make sure they are printable first)?

$ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  my Buf $f = 
$fh.read(200); $fh.close; say "<" ~ $f.Stringy ~ ">";'

Cannot use a Buf as a string, but you called the Stringy method on it
   in block <unit> at -e line 1


This isn't working either

$ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  my Buf $f = 
$fh.read(200); $fh.close; say "<" ~ @$f ~ ">";'

<35 33 47 117 115 114 47 98 105 110 47 101 110 118 32 112 101 114 108 54 
10 10 115 117 98 32 102 40 41 32 123 32 112 117 116 32 38 63 82 79 85 84 
73 78 69 46 103 105 115 116 59 32 125 59 10 10 115 117 98 32 97 98 99 32 
40 41 32 123 10 32 32 32 32 115 97 121 32 34 84 104 105 115 32 115 117 
98 114 111 117 116 105 110 101 39 115 32 73 68 32 105 115 32 34 44 32 
102 59 10 32 32 32 32 112 114 105 110 116 32 34 92 110 34 59 10 10 32 32 
32 32 38 63 82 79 85 84 73 78 69 46 103 105 115 116 32 126 126 32 109 47 
39 32 39 40 46 42 63 41 39 32 39 92 40 47 59 32 10 32 32 32 32 109 121 
32 36 83 117 98 78 97 109 101 32 61 32 36 48 59 10 32 32 32 32 115 97 
121 32 34 84 104 105>


Many thanks,
-T
0
perl6
10/9/2018 1:20:26 PM
perl.perl6.users 1085 articles. 0 followers. Follow

3 Replies
20 Views

Similar Articles

[PageSpeed] 16

--000000000000e71a430577cbaf0b
Content-Type: text/plain; charset="UTF-8"

On Tue, Oct 9, 2018 at 9:21 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

>
> Yes, I know there are other ways to read a file.  I
> have a specific reason for using `read`.
>
> How do I properly turn a Buf into a Str (all the bytes will
> have been tested to make sure they are printable first)?
>

If the characters are UTF-8 compatible, you can call $buf.decode on it to
decode it into characters (graphemes) that make up a Str.
If they are in some other encoding, and that encoding is supported, you can
pass that in to .decode()

https://docs.perl6.org/routine/decode

Curt

--000000000000e71a430577cbaf0b
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr">On Tue, Oct 9, 2018 at 9:21 AM ToddAndMargo via perl6-users &lt;=
<a href=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:=
<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8=
ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Yes, I know there are other ways to read a file.=C2=A0 I<br>
have a specific reason for using `read`.<br>
<br>
How do I properly turn a Buf into a Str (all the bytes will<br>
have been tested to make sure they are printable first)?<br></blockquote><d=
iv><br></div><div>If the characters are UTF-8 compatible, you can call $buf=
..decode on it to decode it into characters (graphemes) that make up a Str.<=
/div><div>If they are in some other encoding, and that encoding is supporte=
d, you can pass that in to .decode()</div><div><br></div><div><a href=3D"ht=
tps://docs.perl6.org/routine/decode">https://docs.perl6.org/routine/decode<=
/a><br></div><div><br></div><div>Curt</div><div><br></div></div></div></div=
>

--000000000000e71a430577cbaf0b--
0
curt
10/9/2018 1:26:24 PM
On 10/9/18 6:26 AM, Curt Tilmes wrote:
>=20
>=20
> On Tue, Oct 9, 2018 at 9:21 AM ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>=20
>     Yes, I know there are other ways to read a file.=C2=A0 I
>     have a specific reason for using `read`.
>=20
>     How do I properly turn a Buf into a Str (all the bytes will
>     have been tested to make sure they are printable first)?
>=20
>=20
> If the characters are UTF-8 compatible, you can call $buf.decode on it =

> to decode it into characters (graphemes) that make up a Str.
> If they are in some other encoding, and that encoding is supported, you=
=20
> can pass that in to .decode()
>=20
> https://docs.perl6.org/routine/decode
>=20
> Curt
>=20

Hi Curt,

Thank you!

I still have trouble wrapping my head around the fact that
there are other charters out there other than ASCII
characters.

-T


$ p6 'my $fh=3Dopen "/home/linuxutil/WhoIsMySub.pl6", :r;  my Buf $f =3D =

$fh.read(100); $fh.close; say "<" ~ $f.decode("utf-8") ~ ">";'

<#!/usr/bin/env perl6

sub f() { put &?ROUTINE.gist; };

sub abc () {
     say "This subroutine's ID i>


If you are curious, this is what became of all the advice I
have been getting lately.  Basically, when you are downloading
files from the Internet, you never really know what you are
going to get.  Web sites change.  The text for an update gets
posted, but they forget to include the corresponding file
(ftp,mozilla.org is really bad at that), etc..  So I
send a know Good Size: something just under what I excpet the
file to be.  Then I test the downloaded file to see if
it is predicted size.  If not, TooSmall gets added to the
status variable.  And then I read the first 200 characters
to see if I downloada somethign binary or test.  If text
("Error 400 not found", "premenantly moved", etc.), I
print it out tell assist in troubleshooting and maintenance.
I saves me several steps.


    my $NewFileSize  =3D 0;
    my $Status;

    my $FileHandle;
    my Buf $File;

   if $NewFileSize < $GoodSize {
       # $Status +|=3D %StatusHash<DOWNLOAD_FAIL>;
       $Status +|=3D %StatusHash<TooSmall>;
       PrintRedErr( "$Caller\: Ooops,\n   $NewFileName\n   is too small. =

File Size =3D $NewFileSize.  Deleting\n" );

       $FileHandle =3D open $NewFileName;
       $File =3D $FileHandle.read( 200 );
       $FileHandle.close;

       for @$File -> $Byte {
          if $Byte =3D=3D 0 {
             $BinaryFile =3D True;
             last; }
       }
       if not $BinaryFile { PrintRedErr( "\n" ~ $File.decode("utf-8") ~=20
"\n\n" ); }

       unlink( $NewFileName );
     } # if $NewFileSize < $GoodSize
0
perl6
10/9/2018 11:36:01 PM
On 10/9/18 4:36 PM, ToddAndMargo via perl6-users wrote:

> $ p6 'my $fh=3Dopen "/home/linuxutil/WhoIsMySub.pl6", :r;=C2=A0 my Buf =
$f =3D=20
> $fh.read(100); $fh.close; say "<" ~ $f.decode("utf-8") ~ ">";'

I changed `$f.decode("utf-8")` to `$f.decode("utf8-c8")` as
you never know what a web page will have on it.
0
perl6
10/9/2018 11:44:48 PM
Reply: