(The same Forrest who posted before, now at home.)
I think I'm really getting this now!
I've come up with a simple example which seems to work everywhere I
need it. Perhaps this can be developed into a reference document; or
at least the people who would find it helpful may stumble across it
while searching the archives:
-- cut here 8<------------(iotest.xs)-----------------------
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define BUFSIZE 10
MODULE = iotest PACKAGE = iotest
##################################################
void
write_foo_to_handle(iop)
PerlIO *iop = IoOFP(sv_2io($arg));
CODE:
PerlIO_printf(iop, "foo!\n");
##################################################
void
read_from_handle(iop)
PerlIO *iop = IoIFP(sv_2io($arg));
INIT:
char buffer[BUFSIZE];
int bytes_read;
CODE:
while(!PerlIO_eof(iop)) {
bytes_read = PerlIO_read(iop, buffer, BUFSIZE - 1);
buffer[bytes_read] = '\0';
printf("Read buffer: %s\n", buffer);
}
##################################################
---------------------(end of iotest.xs)-------------------
-- cut here 8<------------(test.pl)-----------------------
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
#########################
# change 'tests => 1' to 'tests => last_test_to_print';
use Test;
BEGIN { plan tests => 1 };
use iotest;
ok(1); # If we made it this far, we're ok.
#########################
# No ok() tests here, just examples of function calls:
use IO::File;
$out = IO::File->new(">test.1");
print $out "this is before before...\n";
iotest::write_foo_to_handle($out);
print $out "... and this is after\n";
close $out;
open IN, "test.1";
iotest::read_from_handle(\*IN);
close IN;
---------------------(end of test.pl)-------------------
This is what my output was from the above:
1..1
ok 1
Read buffer: this is b
Read buffer: efore bef
Read buffer: ore...
fo
Read buffer: o!
.... an
Read buffer: d this is
Read buffer: after
Notes:
1) Both IO::File objects and globrefs work; you don't have to worry
about which will be used. I used one of each in the example
arbitrarily.
2) Tye McQueen's approach is worth looking at
(http://www.xray.mpe.mpg.de/mailing-lists/perl-xs/2001-11/msg00026.html),
but I have to confess that it's more opaque to me than the PerlIO * stuff.
3) If there is anything stupid or wrong or ugly about these examples,
I do hope someone will let me know!
| Forrest Cahoon | [EMAIL PROTECTED] |------------------------------|
| 850 21st Ave SE |----------------------| Only unbalanced people |
| Mpls MN 55414-2514 | | can tip the scales... |