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

I tried to convert this code line:

binmode STDIN, ":utf8";
to an object-oriented variant using IO::Handle, but this:
use IO; STDIN->binmode(":utf8");
does not work, it dies with Can't locate object method "binmode" via package "IO::Handle". (I am using perl, v5.8.2 built for i686-linux here).

I've grepped the IO, IO::Handle, IO::Seekable, IO::File manpages, and they don't say anything about binmode either.

It seems that the IO::File::open function accepts io layers, but what do I do with an already open file.

See Getting IO::File, PerlIO Layers and POSIX to play together for the original question that made me think about this.

Replies are listed 'Best First'.
Re: Object-oriented interface for binmode
by simonm (Vicar) on Jul 08, 2004 at 22:54 UTC
    I think this is just an oversight and that you can patch in your own binmode method:
    use IO::Handle; sub IO::Handle::binmode { @_ == 2 or Carp::croak 'usage: $io->binmode(DISCIPLINE)'; binmode($_[0], $_[1]); } STDIN->binmode(":raw");

    Does anyone know of a drawback or problem with this approach? And should it be submitted as an improvement to the core IO::Handle module?

      The only problem I can see with this is that something derived fron IO::Handle may not be able to cope with being binmoded (or vice versa) - the tests would need to include all of the child IO::* classes to ensure they don't get broken.

      /J\

      It should probably also be usable in the original way:

      STDIN->binmode;

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: Object-oriented interface for binmode
by BrowserUk (Patriarch) on Jul 08, 2004 at 19:14 UTC

    Given STDIN isn't an object, what would be the benefit of STDIN->binmode(...); over binmode( STDIN, ... )?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
      Given STDIN isn't an object, what would be the benefit of STDIN->binmode(...); over binmode( STDIN, ... )?

      As with the rest of IO::Handle, the advantage is in being able to use a standard idiom of lexical references and method calls -- if you're accustomed to using object techniques, it's awkward for one part of your program to suddenly switch to a different idiom of barewords and builtins.

      Given that some people find IO::Handle useful, the question about why it's missing the binmode method is perfectly reasonable.

        Had the question been framed in terms of an IO::Handle or IO::File object, which can be lexical, I probably wouldn't have asked my question.

        I too find it strange that neither of these implement the binmode method. It is a part of the Tie::Handle spec. I'm guessing that prior to the advent of IOlayers, there wasn't much call for binmode on unix systems.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: Object-oriented interface for binmode
by ambrus (Abbot) on Jul 07, 2006 at 14:13 UTC

    I've just found that newer IO::File modules (starting from IO-1.22) have a binmode method according to their documentation. Back when I asked the original question, this wasn't the case.