home *** CD-ROM | disk | FTP | other *** search
/ 216.17.185.16 / 2015.04-216.17.185.16.tar / 216.17.185.16 / pub / gnat / plain-1.00.tar.gz / plain-1.00.tar / plain-1.00 / plain.pm < prev    next >
Text File  |  1998-12-01  |  3KB  |  90 lines

  1. #!/usr/bin/perl -w
  2.  
  3. package plain;
  4.  
  5. =head1 NAME
  6.  
  7. plain - pragma to disable magic open by giving you a 3-arg open
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.     use plain "open";
  12.     open(F, "<", "foo|") or die;    # opens file "foo|" for reading
  13.     $name = " foo";
  14.     open(F, "<", "$name") or die;    # opens file " foo" for writing
  15.  
  16. =head1 DESCRIPTION
  17.  
  18. Perl comes with two ways of opening a file: open() and sysopen().
  19. open() mixes the file mode (how the file is to be opened) in with the
  20. file name.  The rules for combining the mode and the name make it hard
  21. to open files with spaces at the start or end of their names without
  22. doing jiggerypokery like:
  23.  
  24.     $name =~ s{^(\s)}{./$1};
  25.     open(FH, "< $name\0") or die;
  26.  
  27. This is neither convenient nor obvious.  This module provides you with
  28. an open() function that can take either two arguments or three.  If
  29. called with two arguments, it behaves exactly like Perl's regular
  30. open().  If called with three arguments, it interprets the first as a
  31. filehandle, the second as a open()-style mode string, and the third as
  32. the file name.
  33.  
  34. Perl's built-in open() allows you to open pipes to processes by
  35. specifying filenames like "|tocmd" or "fromcmd|".  The three-argument
  36. open has no way of specifying these things.  This is construed by some
  37. as a bug, and by others as a feature.  The special filename "-" is
  38. grandfathered to mean STDIN or STDOUT.
  39.  
  40. =head1 AUTHOR
  41.  
  42. Nathan Torkington, C<gnat@frii.com>.  1998-12-01.
  43.  
  44. =head1 BUGS
  45.  
  46. It takes away some of the magic in open().  Sorry, Tom.  It also uses
  47. sysopen() to do the three-arg Perl open().  This may (although it's
  48. yet to be shown that it happens) cause problems on systems where
  49. fopen(3) adds non-POSIX O_* constants to the open(2) call.  If this
  50. ever gets moved into the core, we'd be able to use fopen(3) there and
  51. bypass this problem.
  52.  
  53. =cut
  54.  
  55. use Exporter;
  56. use File::Spec;
  57. use Fcntl;
  58. use strict;
  59.  
  60. use vars qw(@ISA @EXPORT_OK $VERSION);
  61.  
  62. @ISA = qw(Exporter);
  63. @EXPORT_OK = qw(open);
  64.  
  65. $VERSION = "1.00";
  66.  
  67. my %equivalent = (
  68.           '<'   => O_RDONLY,
  69.           '>'   => O_WRONLY|O_TRUNC |O_CREAT,
  70.           '>>'  => O_WRONLY|O_APPEND|O_CREAT,
  71.           '+<'  => O_RDWR,
  72.           '+>'  => O_RDWR  |O_TRUNC |O_CREAT,
  73.           '+>>' => O_RDWR  |O_APPEND|O_CREAT,
  74.           );
  75.  
  76. sub open (*@) {
  77.     # allow "real" open
  78.     croak("one-argument open is dead.") if @_ == 1;
  79.     return open($_[0], $_[1]) if @_ == 2;
  80.     die 'usage: open(FH, "<", $filename)' unless @_ == 3;
  81.  
  82.     my ($fh,$mode,$name) = @_;
  83.     exists($equivalent{$mode})
  84.     or croak("Unknown mode: $mode\n");
  85.     return open($fh, "$mode $name") if $name eq "-";
  86.     return sysopen($fh, $name, $equivalent{$mode});
  87. }
  88.  
  89. 1;
  90.