On Wed, Dec 13, 2006 at 05:42:03PM -0800, Dean Arnold wrote:
> After a bit more research, I think I've figured out
> how to get the PerlIO object from an SV:
>
> IO *io;
>
> if (!file) /* no arg == no change */
> return 0;
> /* XXX need to support file being a filehandle object */
> if (SvROK(file)) {
> /* DAA must be a filehandle...we hope */
> /* but how do we tell, and how do we get the PerlIO object from an SV ?
> */
> io = sv_2io(file);
> if (io) {
> fp = IoOFP(io);
> }
> else {
> warn("Input trace filehandle is not valid");
> return 0;
> }
> }
> else { /* do things the old way */ }
>
> Does that make sense ? (I kyped the code from some Perl/Tk internals)
sv_2io never returns false, it croaks. But since you're only calling it
if SvROK is true, and the croak error is meaningful ("Bad filehandle ...")
I think that's fine. Also, IoOFP(io) might be false in some odd cases.
So I'd suggest a slight tweak:
if (SvROK(file)) { /* assume it's a filehandle */
io = sv_2io(file); /* will croak if not */
if (!io || !(fp = IoOFP(io))) {
warn("DBI trace filehandle is not valid");
return 0;
}
}
else { ... }
Will need some tests of course. Both for writing to a plain filehandle
and a tied one.
Thanks Dean!
Tim.