Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Roles definitely sounds like the right way to go, and are probably the Perl 6 solution.

    But the common Perl 5 solution does seem to be "If it got passed in as a filehandle, assume it's a filehandle". Though I'm not claiming that's not problematic (many modules die spectactularly and unhelpfully if passed the wrong parameters, rather than giving a useful "invalid usage" exception), is it really worth getting your module's users to have to create an iterator function wrapping a filehandle purely because there
    --

    osfameron

    • Yeah, the bigger problem comes up when you don't even know that "it got passed in as a filehandle." Some modules try to allow many kinds of data to be passed in, each of which is used in a useful way. They'll say "if (ref $x eq 'ARRAY')" and so on. Of course, there's the same problem if you're passing in an Object::Array.

      So, yeah, a simple solution is to make it explicit: pass in a named foo_data that's a scalar or foo_stream that's a coderef... or something like that. Then again, with _CALLABLE, a litt
      --
      rjbs
  • OK, so you take a coderef...

    Now how do we tell that it's an iterator coderef? How do we know that this coderef that the user invented to satisfy your API actually works?

    Because that's yet another type of bug, one that's probably even harder to find again.

    Oh, and as for your tell and seek, there's a role for that already, called IO::Seekable. IO::Handle on it's own DOESN'T assume that the filehandle is seekable, it may well be a stream.

    So something like IO::File responds to both ->isa('IO::Handle') and -
    • We don't know that it works, but the user knows that it will be called. That's what I'm concerned with. It provides one simple test: if the passed value is callable, it gets called. Whether it has bugs is your problem.

      That's a big improvement from "if our undisclosed criterion for being a filehandle passes, we will diamond your value." Even if they disclose their method (which would be unusual) it means you have to know what it is. Callability is a solved problem, as I see it.
      --
      rjbs
  • >if (my $result = eval { $data->() }) { act_on $result }

    >This sucks the most. It work as long as $data is
    >callable and returns something true. (It could be
    >tweaked to work as long as it's callable, but would
    >get even uglier and no better.)

    IMO It gets a little better:

    my $result;
    if (eval { $result=$data->(); 1 }) { ....
    }