VMS 5.003_05 Update.
[p5sagit/p5-mst-13.2.git] / vms / ext / Stdio / Stdio.pm
CommitLineData
0d7e20a5 1# VMS::Stdio - VMS extensions to Perl's stdio calls
748a9306 2#
3# Author: Charles Bailey bailey@genetics.upenn.edu
0d7e20a5 4# Version: 2.0
5# Revised: 28-Feb-1996
6
7package VMS::Stdio;
8
9require 5.002;
10use vars qw( $VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA );
11use Carp '&croak';
12use DynaLoader ();
13use Exporter ();
14
15$VERSION = '2.0';
740ce14c 16@ISA = qw( Exporter DynaLoader IO::File );
0d7e20a5 17@EXPORT = qw( &O_APPEND &O_CREAT &O_EXCL &O_NDELAY &O_NOWAIT
18 &O_RDONLY &O_RDWR &O_TRUNC &O_WRONLY );
19@EXPORT_OK = qw( &flush &getname &remove &rewind &sync &tmpnam
20 &vmsopen &vmssysopen &waitfh );
21%EXPORT_TAGS = ( CONSTANTS => [ qw( &O_APPEND &O_CREAT &O_EXCL &O_NDELAY
22 &O_NOWAIT &O_RDONLY &O_RDWR &O_TRUNC
23 &O_WRONLY ) ],
24 FUNCTIONS => [ qw( &flush &getname &remove &rewind &sync
25 &tmpnam &vmsopen &vmssysopen &waitfh ) ] );
26
27bootstrap VMS::Stdio $VERSION;
28
29sub AUTOLOAD {
30 my($constname) = $AUTOLOAD;
31 $constname =~ s/.*:://;
32 if ($constname =~ /^O_/) {
33 my($val) = constant($constname);
34 defined $val or croak("Unknown VMS::Stdio constant $constname");
35 *$AUTOLOAD = sub { $val };
36 }
740ce14c 37 else { # We don't know about it; hand off to IO::File
38 require IO::File;
0d7e20a5 39 my($obj) = shift(@_);
740ce14c 40 $obj->IO::File::$constname(@_);
0d7e20a5 41 }
42 goto &$AUTOLOAD;
43}
44
45sub DESTROY { close($_[0]); }
46
47
48################################################################################
49# Intercept calls to old VMS::stdio package, complain, and hand off
50# This will be removed in a future version of VMS::Stdio
51
52package VMS::stdio;
53
54sub AUTOLOAD {
55 my($func) = $AUTOLOAD;
56 $func =~ s/.*:://;
57 # Cheap trick: we know DynaLoader has required Carp.pm
58 Carp::carp("Old package VMS::stdio is now VMS::Stdio; please update your code");
59 if ($func eq 'vmsfopen') {
60 Carp::carp("Old function &vmsfopen is now &vmsopen");
61 goto &VMS::Stdio::vmsopen;
62 }
63 elsif ($func eq 'fgetname') {
64 Carp::carp("Old function &fgetname is now &getname");
65 goto &VMS::Stdio::getname;
66 }
67 else { goto &{"VMS::Stdio::$func"}; }
68}
69
70package VMS::Stdio; # in case we ever use AutoLoader
71
721;
73
74__END__
748a9306 75
76=head1 NAME
77
0d7e20a5 78VMS::Stdio
748a9306 79
80=head1 SYNOPSIS
81
0d7e20a5 82use VMS::Stdio qw( &flush &getname &remove &rewind &sync &tmpnam
83 &vmsopen &vmssysopen &waitfh );
84$uniquename = tmpnam;
85$fh = vmsopen("my.file","rfm=var","alq=100",...) or die $!;
86$name = getname($fh);
87print $fh "Hello, world!\n";
88flush($fh);
89sync($fh);
90rewind($fh);
91$line = <$fh>;
92undef $fh; # closes file
93$fh = vmssysopen("another.file", O_RDONLY | O_NDELAY, 0, "ctx=bin");
94sysread($fh,$data,128);
95waitfh($fh);
96close($fh);
97remove("another.file");
748a9306 98
99=head1 DESCRIPTION
100
0d7e20a5 101This package gives Perl scripts access to VMS extensions to several
102C stdio operations not available through Perl's CORE I/O functions.
103The specific routines are described below. These functions are
104prototyped as unary operators, with the exception of C<vmsopen>
105and C<vmssysopen>, which can take any number of arguments, and
106C<tmpnam>, which takes none.
107
108All of the routines are available for export, though none are
109exported by default. All of the constants used by C<vmssysopen>
110to specify access modes are exported by default. The routines
111are associated with the Exporter tag FUNCTIONS, and the constants
112are associated with the Exporter tag CONSTANTS, so you can more
113easily choose what you'd like to import:
114
115 # import constants, but not functions
116 use VMS::Stdio; # same as use VMS::Stdio qw( :DEFAULT );
117 # import functions, but not constants
118 use VMS::Stdio qw( !:CONSTANTS :FUNCTIONS );
119 # import both
120 use VMS::Stdio qw( :CONSTANTS :FUNCTIONS );
121 # import neither
122 use VMS::Stdio ();
123
124Of course, you can also choose to import specific functions by
125name, as usual.
126
740ce14c 127This package C<ISA> IO::File, so that you can call IO::File
0d7e20a5 128methods on the handles returned by C<vmsopen> and C<vmssysopen>.
740ce14c 129The IO::File package is not initialized, however, until you
0d7e20a5 130actually call a method that VMS::Stdio doesn't provide. This
131is doen to save startup time for users who don't wish to use
740ce14c 132the IO::File methods.
0d7e20a5 133
134B<Note:> In order to conform to naming conventions for Perl
135extensions and functions, the name of this package has been
136changed to VMS::Stdio as of Perl 5.002, and the names of some
137routines have been changed. Calls to the old VMS::stdio routines
138will generate a warning, and will be routed to the equivalent
139VMS::Stdio function. This compatibility interface will be
140removed in a future release of this extension, so please
141update your code to use the new routines.
142
143=item flush
144
145This function causes the contents of stdio buffers for the specified
146file handle to be flushed. If C<undef> is used as the argument to
147C<flush>, all currently open file handles are flushed. Like the CRTL
148fflush() routine, it does not flush any underlying RMS buffers for the
149file, so the data may not be flushed all the way to the disk. C<flush>
150returns a true value if successful, and C<undef> if not.
151
152=item getname
153
154The C<getname> function returns the file specification associated
740ce14c 155with a Perl I/O handle. If an error occurs, it returns C<undef>.
748a9306 156
0d7e20a5 157=item remove
748a9306 158
0d7e20a5 159This function deletes the file named in its argument, returning
160a true value if successful and C<undef> if not. It differs from
161the CORE Perl function C<unlink> in that it does not try to
162reset file protection if the original protection does not give
163you delete access to the file (cf. L<perlvms>). In other words,
164C<remove> is equivalent to
165
166 unlink($file) if VMS::Filespec::candelete($file);
748a9306 167
0d7e20a5 168=item rewind
169
170C<rewind> resets the current position of the specified file handle
171to the beginning of the file. It's really just a convenience
172method equivalent in effect to C<seek($fh,0,0)>. It returns a
173true value if successful, and C<undef> if it fails.
174
175=item sync
176
177This function flushes buffered data for the specified file handle
178from stdio and RMS buffers all the way to disk. If successful, it
179returns a true value; otherwise, it returns C<undef>.
180
181=item tmpnam
748a9306 182
183The C<tmpnam> function returns a unique string which can be used
184as a filename when creating temporary files. If, for some
185reason, it is unable to generate a name, it returns C<undef>.
186
0d7e20a5 187=item vmsopen
748a9306 188
0d7e20a5 189The C<vmsopen> function enables you to specify optional RMS arguments
190to the VMS CRTL when opening a file. It is similar to the built-in
191Perl C<open> function (see L<perlfunc> for a complete description),
192but will only open normal files; it cannot open pipes or duplicate
740ce14c 193existing I/O handles. Up to 8 optional arguments may follow the
748a9306 194file name. These arguments should be strings which specify
0d7e20a5 195optional file characteristics as allowed by the CRTL. (See the
196CRTL reference manual description of creat() and fopen() for details.)
197If successful, C<vmsopen> returns a VMS::Stdio file handle; if an
198error occurs, it returns C<undef>.
199
200You can use the file handle returned by C<vmsfopen> just as you
201would any other Perl file handle. The class VMS::Stdio ISA
740ce14c 202IO::File, so you can call IO::File methods using the handle
0d7e20a5 203returned by C<vmsopen>. However, C<use>ing VMS::Stdio does not
740ce14c 204automatically C<use> IO::File; you must do so explicitly in
205your program if you want to call IO::File methods. This is
206done to avoid the overhead of initializing the IO::File package
0d7e20a5 207in programs which intend to use the handle returned by C<vmsopen>
208as a normal Perl file handle only. When the scalar containing
209a VMS::Stdio file handle is overwritten, C<undef>d, or goes
210out of scope, the associated file is closed automatically.
211
212=item vmssysopen
213
214This function bears the same relationship to the CORE function
215C<sysopen> as C<vmsopen> does to C<open>. Its first three arguments
216are the name, access flags, and permissions for the file. Like
217C<vmsopen>, it takes up to 8 additional string arguments which
218specify file characteristics. Its return value is identical to
219that of C<vmsopen>.
220
221The symbolic constants for the mode argument are exported by
222VMS::Stdio by default, and are also exported by the Fcntl package.
223
224=item waitfh
225
226This function causes Perl to wait for the completion of an I/O
227operation on the file handle specified as its argument. It is
228used with handles opened for asynchronous I/O, and performs its
229task by calling the CRTL routine fwait().
748a9306 230
231=head1 REVISION
232
0d7e20a5 233This document was last revised on 28-Jan-1996, for Perl 5.002.
748a9306 234
235=cut