http://qs321.pair.com?node_id=197559

joe++ has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm in the process of connecting the ouput of one external module to the input of another external module. The problem ist that the caller gives me a scalar ref, while I can call the imported method only with a file handle for output.

My solution is to pass the incoming scalar reference to one of IO::File or IO::Scalar, and then passing the resulting file handle along to the next function.

Now my question is: which IO::* module should I pick, considering that I want to ditribute my code to the general Perl users audience?

The docs of IO::Scalar mention IO::String as being its potential successor, but there is only one release of this module on CPAN, which also predates IO::Scalar's latest release...

Example code (snippet):

... # do they provide a scalar ref as output buffer? if ($output_ref) { $fh_out = new IO::Scalar($output_ref); } ... if (defined $fh_out) { # write XML to the scalar-tied file handle $self->{x_out} = new XML::Writer(OUTPUT => $fh_out); } else { # default: print to STDOUT $self->{x_out} = new XML::Writer(); } ...
Any help and opinions are welcome!

Joe.

Replies are listed 'Best First'.
Re: IO::Scalar vs IO::String
by hiseldl (Priest) on Sep 13, 2002 at 13:51 UTC
    If you need to use IO::Scalar in more than one place, and you want to keep IO::String in mind for later, you could wrap the call in a subroutine, and then if IO::Scalar is deprecated, or you just want to switch to IO::String, you can just change the one instance in your subroutine wrapper.

    It adds the overhead of an extra subroutine call, but if I am relying on someone else's code, I have found it to be good practice to wrap it in my own method call so that later if the 3rd party code changes, I only have to change one place. The change can be to swap out the code for another 3rd party solution, or to write it myself.

    --
    hiseldl
    "Act better than you feel"

      Hello hiseldl,

      Thanks for the suggestion - but I need this trick inside my own module, and just in one place, so the module itself can be considered the "wrapper" in a way.

      My consideration is which module to use right now, just hoping someone who has experience with both of them to give the right suggestion.

      This really would be a no-brainer if one of these modules was part of Perl's core distribution (or considered to become part of is for that matter).

      Cheers,
      Joe

        My recommendation is to use IO::Scalar because it is more mature and from the pod

          Note: as of version 2.x, these classes all work like their IO::Handle counterparts, so we have comparable functionality to IO::String.

        it has similar functionality to IO::String, so using IO::Scalar seems to be a win-win.

        --
        hiseldl
        "Act better than you feel"

Re: IO::Scalar vs IO::String
by PodMaster (Abbot) on Sep 14, 2002 at 04:37 UTC