Thursday, December 3, 2015

Perl6: Basic File I/O

NOTE: The I/O Role is explained in detail here: http://doc.perl6.org/type/IO

   my $content = slurp 'text.txt';
   say $content.chars, " chars";

35 chars

   my $output = "And now for some multiline data:
   The first line.
   The second line.
   Anudda line.
   Okay I'm done.";

   spurt "output.txt", $output; 



OK, it wrapped the file handle for me.  That's cool.

File test flags are attached to an IO Role:

   say "text.txt".IO.d;  # am I a dir?
   say "text.txt".IO.e;  # do I exist?
   say "text.txt".IO.f;  # am I a file?

False
True
True

...and if you try it the old way, you'll get the amusing:

   say -e "text.txt";

Confused






No comments:

Post a Comment