sid_com
sid_com

Reputation: 25117

How many open-backends does IO::File use?

Is the built-in open function, the basic Perl open operator and the three-argument open operator` described in IO::File#METHODS all the same function?

Upvotes: 1

Views: 82

Answers (1)

amon
amon

Reputation: 57640

Yes and no.

The open built-in is described in perldoc -f open. This function is well suited to do actual, text-oriented work. It can use PerlIO-layers, to do automatic de- or encoding of the input stream.

Perl has another way to open files, called sysopen. This is essentially a very thin wrapper around C's fdopen, with all gotchas and issues. In Perl, sysopen is called like

sysopen FILEHANDLE, FILENAME, MODE[, PERMS]

Now, IO::File provides an object-oriented interface for opening files, and inherits from IO::Handle. The open method contains the following code:

sub open {
  @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
  my ($fh, $file) = @_;
  if (@_ > 2) {
    my ($mode, $perms) = @_[2, 3];
    if ($mode =~ /^\d+$/) {
      defined $perms or $perms = 0666;
      return sysopen($fh, $file, $mode, $perms);
    } elsif ($mode =~ /:/) {
      return open($fh, $mode, $file) if @_ == 3;
      croak 'usage: $fh->open(FILENAME, IOLAYERS)';
    } else {
      return open($fh, IO::Handle::_open_mode_string($mode), $file);
    }
  }
  open($fh, $file);
}

As you can see, it is a wrapper around open and sysopen, so it is safe to say that this method is not identical to the core open ;-) Also, the doc (to which you linked) says so.

Upvotes: 6

Related Questions