On 10/9/18 6:26 AM, Curt Tilmes wrote:


On Tue, Oct 9, 2018 at 9:21 AM ToddAndMargo via perl6-users <perl6-users@perl.org <mailto: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


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=open "/home/linuxutil/WhoIsMySub.pl6", :r; my Buf $f = $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  = 0;
   my $Status;

   my $FileHandle;
   my Buf $File;

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

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

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

      unlink( $NewFileName );
    } # if $NewFileSize < $GoodSize

Reply via email to