On 02/14/2011 02:41 AM, Carl Mäsak wrote:

--snip--
to me that a Buf could be output without first having been converted
to a Str. A conversion may or may not be necessary for it to work.


S32/IO says that IO.write will output a Buf.


On Feb 14, 2011, at 12:37 AM, Richard Hainsworth wrote:
--snip--
I am not too clear on how pack works in perl5.

Is there a work around in perl6 to achieve the same result, viz., not using pack?

This code works for me on the latest released Rakudo;
   use v6;

   my @fake =
[ 0, 0, 0, 0, -1, -1, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, ], # 0f, 8f [ -1, 0, -1, 0, -1, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1, 0, ], # aa, 2a [ 0, -1, 0, -1, 0, -1, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1, ], # 55, d5
   ;
   my ( $h, $w ) = ( @fake.elems, @fake[0].elems );

   sub dot ( $x, $y ) { return @fake[$x][$y] }

   my $fh = open( 'mandelbrot.pbm', :w, :bin );

   for ^$h -> $x {
       for [^$w].map({ -1 * dot($x, $_) }) {
$fh.write( Buf.new( :2("$^a$^b$^c$^d$^e$^f$^g $^h"), :size(8) ) );
       }
   }

   $fh.close;

Output:
   $ hexdump mandelbrot.pbm
       0000000 0f 8f aa 2a 55 d5
       0000006


Note that 8 implicit params (like $^h) are needed, only because .batch(8) is not yet implemented.
   for [^$w].map({ -1*dot($x, $_) }).batch(8) -> @b { # NYI
       $fh.write( Buf.new( :2( @b.join('') ), :size(8) ) );
   }

Note also that Buf is specced to work differently than as I used it here, and so may change in the future. Buf.new should have a different API, and a Buf with size(1) should (I think) autopack 8 bits into a byte on output.

--
Hope this helps,
Bruce Gray (Util of PerlMonks)

Reply via email to