This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
applied suggested patch, modulo superseded parts
[perl5.git] / vms / ext / Stdio / Stdio.pm
CommitLineData
0d7e20a5 1# VMS::Stdio - VMS extensions to Perl's stdio calls
748a9306
LW
2#
3# Author: Charles Bailey bailey@genetics.upenn.edu
17f28c40
CB
4# Version: 2.1
5# Revised: 24-Mar-1998
c1441b10 6# Docs revised: 13-Oct-1998 Dan Sugalski <sugalskd@ous.edu>
0d7e20a5 7
8package VMS::Stdio;
9
10require 5.002;
11use vars qw( $VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA );
12use Carp '&croak';
13use DynaLoader ();
14use Exporter ();
15
17f28c40 16$VERSION = '2.1';
740ce14c 17@ISA = qw( Exporter DynaLoader IO::File );
0d7e20a5 18@EXPORT = qw( &O_APPEND &O_CREAT &O_EXCL &O_NDELAY &O_NOWAIT
19 &O_RDONLY &O_RDWR &O_TRUNC &O_WRONLY );
17f28c40
CB
20@EXPORT_OK = qw( &flush &getname &remove &rewind &sync &setdef &tmpnam
21 &vmsopen &vmssysopen &waitfh &writeof );
0d7e20a5 22%EXPORT_TAGS = ( CONSTANTS => [ qw( &O_APPEND &O_CREAT &O_EXCL &O_NDELAY
23 &O_NOWAIT &O_RDONLY &O_RDWR &O_TRUNC
24 &O_WRONLY ) ],
17f28c40
CB
25 FUNCTIONS => [ qw( &flush &getname &remove &rewind &setdef
26 &sync &tmpnam &vmsopen &vmssysopen
27 &waitfh &writeof ) ] );
0d7e20a5 28
29bootstrap VMS::Stdio $VERSION;
30
31sub AUTOLOAD {
32 my($constname) = $AUTOLOAD;
33 $constname =~ s/.*:://;
34 if ($constname =~ /^O_/) {
35 my($val) = constant($constname);
36 defined $val or croak("Unknown VMS::Stdio constant $constname");
09b7f37c 37 *$AUTOLOAD = sub { $val; }
0d7e20a5 38 }
740ce14c 39 else { # We don't know about it; hand off to IO::File
40 require IO::File;
55497cff 41
5f05dabc 42 *$AUTOLOAD = eval "sub { shift->IO::File::$constname(\@_) }";
43 croak "Error autoloading IO::File::$constname: $@" if $@;
0d7e20a5 44 }
45 goto &$AUTOLOAD;
46}
47
48sub DESTROY { close($_[0]); }
49
50
51################################################################################
52# Intercept calls to old VMS::stdio package, complain, and hand off
53# This will be removed in a future version of VMS::Stdio
54
55package VMS::stdio;
56
57sub AUTOLOAD {
58 my($func) = $AUTOLOAD;
59 $func =~ s/.*:://;
60 # Cheap trick: we know DynaLoader has required Carp.pm
61 Carp::carp("Old package VMS::stdio is now VMS::Stdio; please update your code");
62 if ($func eq 'vmsfopen') {
63 Carp::carp("Old function &vmsfopen is now &vmsopen");
64 goto &VMS::Stdio::vmsopen;
65 }
66 elsif ($func eq 'fgetname') {
67 Carp::carp("Old function &fgetname is now &getname");
68 goto &VMS::Stdio::getname;
69 }
70 else { goto &{"VMS::Stdio::$func"}; }
71}
72
73package VMS::Stdio; # in case we ever use AutoLoader
74
751;
76
77__END__
748a9306
LW
78
79=head1 NAME
80
2ceaccd7 81VMS::Stdio - standard I/O functions via VMS extensions
748a9306
LW
82
83=head1 SYNOPSIS
84
c1441b10
HM
85 use VMS::Stdio qw( &flush &getname &remove &rewind &setdef &sync &tmpnam
86 &vmsopen &vmssysopen &waitfh &writeof );
87 setdef("new:[default.dir]");
88 $uniquename = tmpnam;
89 $fh = vmsopen("my.file","rfm=var","alq=100",...) or die $!;
90 $name = getname($fh);
91 print $fh "Hello, world!\n";
92 flush($fh);
93 sync($fh);
94 rewind($fh);
95 $line = <$fh>;
96 undef $fh; # closes file
97 $fh = vmssysopen("another.file", O_RDONLY | O_NDELAY, 0, "ctx=bin");
98 sysread($fh,$data,128);
99 waitfh($fh);
100 close($fh);
101 remove("another.file");
102 writeof($pipefh);
103
748a9306
LW
104=head1 DESCRIPTION
105
2ceaccd7 106This package gives Perl scripts access via VMS extensions to several
0d7e20a5 107C stdio operations not available through Perl's CORE I/O functions.
108The specific routines are described below. These functions are
109prototyped as unary operators, with the exception of C<vmsopen>
110and C<vmssysopen>, which can take any number of arguments, and
111C<tmpnam>, which takes none.
112
113All of the routines are available for export, though none are
114exported by default. All of the constants used by C<vmssysopen>
115to specify access modes are exported by default. The routines
116are associated with the Exporter tag FUNCTIONS, and the constants
117are associated with the Exporter tag CONSTANTS, so you can more
118easily choose what you'd like to import:
119
120 # import constants, but not functions
121 use VMS::Stdio; # same as use VMS::Stdio qw( :DEFAULT );
122 # import functions, but not constants
123 use VMS::Stdio qw( !:CONSTANTS :FUNCTIONS );
124 # import both
125 use VMS::Stdio qw( :CONSTANTS :FUNCTIONS );
126 # import neither
127 use VMS::Stdio ();
128
129Of course, you can also choose to import specific functions by
130name, as usual.
131
740ce14c 132This package C<ISA> IO::File, so that you can call IO::File
0d7e20a5 133methods on the handles returned by C<vmsopen> and C<vmssysopen>.
740ce14c 134The IO::File package is not initialized, however, until you
0d7e20a5 135actually call a method that VMS::Stdio doesn't provide. This
136is doen to save startup time for users who don't wish to use
740ce14c 137the IO::File methods.
0d7e20a5 138
139B<Note:> In order to conform to naming conventions for Perl
140extensions and functions, the name of this package has been
141changed to VMS::Stdio as of Perl 5.002, and the names of some
142routines have been changed. Calls to the old VMS::stdio routines
143will generate a warning, and will be routed to the equivalent
144VMS::Stdio function. This compatibility interface will be
145removed in a future release of this extension, so please
146update your code to use the new routines.
147
2ceaccd7
LV
148=over
149
0d7e20a5 150=item flush
151
152This function causes the contents of stdio buffers for the specified
153file handle to be flushed. If C<undef> is used as the argument to
154C<flush>, all currently open file handles are flushed. Like the CRTL
155fflush() routine, it does not flush any underlying RMS buffers for the
156file, so the data may not be flushed all the way to the disk. C<flush>
157returns a true value if successful, and C<undef> if not.
158
159=item getname
160
161The C<getname> function returns the file specification associated
740ce14c 162with a Perl I/O handle. If an error occurs, it returns C<undef>.
748a9306 163
0d7e20a5 164=item remove
748a9306 165
0d7e20a5 166This function deletes the file named in its argument, returning
167a true value if successful and C<undef> if not. It differs from
168the CORE Perl function C<unlink> in that it does not try to
169reset file protection if the original protection does not give
170you delete access to the file (cf. L<perlvms>). In other words,
171C<remove> is equivalent to
172
173 unlink($file) if VMS::Filespec::candelete($file);
748a9306 174
0d7e20a5 175=item rewind
176
177C<rewind> resets the current position of the specified file handle
178to the beginning of the file. It's really just a convenience
179method equivalent in effect to C<seek($fh,0,0)>. It returns a
180true value if successful, and C<undef> if it fails.
181
17f28c40
CB
182=item setdef
183
184This function sets the default device and directory for the process.
185It is identical to the built-in chdir() operator, except that the change
186persists after Perl exits. It returns a true value on success, and
187C<undef> if it encounters and error.
188
0d7e20a5 189=item sync
190
191This function flushes buffered data for the specified file handle
192from stdio and RMS buffers all the way to disk. If successful, it
193returns a true value; otherwise, it returns C<undef>.
194
195=item tmpnam
748a9306
LW
196
197The C<tmpnam> function returns a unique string which can be used
198as a filename when creating temporary files. If, for some
199reason, it is unable to generate a name, it returns C<undef>.
200
0d7e20a5 201=item vmsopen
748a9306 202
0d7e20a5 203The C<vmsopen> function enables you to specify optional RMS arguments
5f05dabc 204to the VMS CRTL when opening a file. Its operation is similar to the built-in
0d7e20a5 205Perl C<open> function (see L<perlfunc> for a complete description),
5f05dabc 206but it will only open normal files; it cannot open pipes or duplicate
740ce14c 207existing I/O handles. Up to 8 optional arguments may follow the
748a9306 208file name. These arguments should be strings which specify
0d7e20a5 209optional file characteristics as allowed by the CRTL. (See the
210CRTL reference manual description of creat() and fopen() for details.)
211If successful, C<vmsopen> returns a VMS::Stdio file handle; if an
212error occurs, it returns C<undef>.
213
5f05dabc 214You can use the file handle returned by C<vmsopen> just as you
0d7e20a5 215would any other Perl file handle. The class VMS::Stdio ISA
740ce14c 216IO::File, so you can call IO::File methods using the handle
0d7e20a5 217returned by C<vmsopen>. However, C<use>ing VMS::Stdio does not
740ce14c 218automatically C<use> IO::File; you must do so explicitly in
219your program if you want to call IO::File methods. This is
220done to avoid the overhead of initializing the IO::File package
0d7e20a5 221in programs which intend to use the handle returned by C<vmsopen>
222as a normal Perl file handle only. When the scalar containing
223a VMS::Stdio file handle is overwritten, C<undef>d, or goes
224out of scope, the associated file is closed automatically.
225
c1441b10
HM
226=over 4
227
228=head2 File characteristic options
229
230=over 2
231
232=item alq=INTEGER
233
234Sets the allocation quantity for this file
235
236=item bls=INTEGER
237
238File blocksize
239
240=item ctx=STRING
241
242Sets the context for the file. Takes one of these arguments:
243
244=over 4
245
246=item bin
247
248Disables LF to CRLF translation
249
250=item cvt
251
252Negates previous setting of C<ctx=noctx>
253
254=item nocvt
255
256Disables conversion of FORTRAN carriage control
257
258=item rec
259
260Force record-mode access
261
262=item stm
263
264Force stream mode
265
266=item xplct
267
268Causes records to be flushed I<only> when the file is closed, or when an
269explicit flush is done
270
271=back
272
273=item deq=INTEGER
274
275Sets the default extension quantity
276
277=item dna=FILESPEC
278
279Sets the default filename string. Used to fill in any missing pieces of the
280filename passed.
281
282=item fop=STRING
283
284File processing option. Takes one or more of the following (in a
285comma-separated list if there's more than one)
286
287=over 4
288
289=item ctg
290
291Contiguous.
292
293=item cbt
294
295Contiguous-best-try.
296
297=item dfw
298
299Deferred write; only applicable to files opened for shared access.
300
301=item dlt
302
303Delete file on close.
304
305=item tef
306
307Truncate at end-of-file.
308
309=item cif
310
311Create if nonexistent.
312
313=item sup
314
315Supersede.
316
317=item scf
318
319Submit as command file on close.
320
321=item spl
322
323Spool to system printer on close.
324
325=item tmd
326
327Temporary delete.
328
329=item tmp
330
331Temporary (no file directory).
332
333=item nef
334
335Not end-of-file.
336
337=item rck
338
339Read check compare operation.
340
341=item wck
342
343Write check compare operation.
344
345=item mxv
346
347Maximize version number.
348
349=item rwo
350
351Rewind file on open.
352
353=item pos
354
355Current position.
356
357=item rwc
358
359Rewind file on close.
360
361=item sqo
362
363File can only be processed in a sequential manner.
364
365=back
366
367=item fsz=INTEGER
368
369Fixed header size
370
371=item gbc=INTEGER
372
373Global buffers requested for the file
374
375=item mbc=INTEGER
376
377Multiblock count
378
379=item mbf=INTEGER
380
381Bultibuffer count
382
383=item mrs=INTEGER
384
385Maximum record size
386
387=item rat=STRING
388
389File record attributes. Takes one of the following:
390
391=over 4
392
393=item cr
394
395Carriage-return control.
396
397=item blk
398
399Disallow records to span block boundaries.
400
401=item ftn
402
403FORTRAN print control.
404
405=item none
406
407Explicitly forces no carriage control.
408
409=item prn
410
411Print file format.
412
413=back
414
415=item rfm=STRING
416
417File record format. Takes one of the following:
418
419=over 4
420
421=item fix
422
423Fixed-length record format.
424
425=item stm
426
427RMS stream record format.
428
429=item stmlf
430
431Stream format with line-feed terminator.
432
433=item stmcr
434
435Stream format with carriage-return terminator.
436
437=item var
438
439Variable-length record format.
440
441=item vfc
442
443Variable-length record with fixed control.
444
445=item udf
446
447Undefined format
448
449=back
450
451=item rop=STRING
452
453Record processing operations. Takes one or more of the following in a
454comma-separated list:
455
456=over 4
457
458=item asy
459
460Asynchronous I/O.
461
462=item cco
463
464Cancel Ctrl/O (used with Terminal I/O).
465
466=item cvt
467
468Capitalizes characters on a read from the terminal.
469
470=item eof
471
472Positions the record stream to the end-of-file for the connect operation
473only.
474
475=item nlk
476
477Do not lock record.
478
479=item pmt
480
481Enables use of the prompt specified by pmt=usr-prmpt on input from the
482terminal.
483
484=item pta
485
486Eliminates any information in the type-ahead buffer on a read from the
487terminal.
488
489=item rea
490
491Locks record for a read operation for this process, while allowing other
492accessors to read the record.
493
494=item rlk
495
496Locks record for write.
497
498=item rne
499
500Suppresses echoing of input data on the screen as it is entered on the
501keyboard.
502
503=item rnf
504
505Indicates that Ctrl/U, Ctrl/R, and DELETE are not to be considered control
506commands on terminal input, but are to be passed to the application
507program.
508
509=item rrl
510
511Reads regardless of lock.
512
513=item syncsts
514
515Returns success status of RMS$_SYNCH if the requested service completes its
516task immediately.
517
518=item tmo
519
520Timeout I/O.
521
522=item tpt
523
524Allows put/write services using sequential record access mode to occur at
525any point in the file, truncating the file at that point.
526
527=item ulk
528
529Prohibits RMS from automatically unlocking records.
530
531=item wat
532
533Wait until record is available, if currently locked by another stream.
534
535=item rah
536
537Read ahead.
538
539=item wbh
540
541Write behind.
542
543=back
544
545=item rtv=INTEGER
546
547The number of retrieval pointers that RMS has to maintain (0 to 127255)
548
549=item shr=STRING
550
551File sharing options. Choose one of the following:
552
553=over 4
554
555=item del
556
557Allows users to delete.
558
559=item get
560
561Allows users to read.
562
563=item mse
564
565Allows mainstream access.
566
567=item nil
568
569Prohibits file sharing.
570
571=item put
572
573Allows users to write.
574
575=item upd
576
577Allows users to update.
578
579=item upi
580
581Allows one or more writers.
582
583=back
584
585=item tmo=INTEGER
586
587I/O timeout value
588
589=back
590
591=back
592
0d7e20a5 593=item vmssysopen
594
595This function bears the same relationship to the CORE function
596C<sysopen> as C<vmsopen> does to C<open>. Its first three arguments
597are the name, access flags, and permissions for the file. Like
598C<vmsopen>, it takes up to 8 additional string arguments which
599specify file characteristics. Its return value is identical to
600that of C<vmsopen>.
601
602The symbolic constants for the mode argument are exported by
603VMS::Stdio by default, and are also exported by the Fcntl package.
604
605=item waitfh
606
607This function causes Perl to wait for the completion of an I/O
608operation on the file handle specified as its argument. It is
609used with handles opened for asynchronous I/O, and performs its
610task by calling the CRTL routine fwait().
748a9306 611
17f28c40
CB
612=item writeof
613
614This function writes an EOF to a file handle, if the device driver
615supports this operation. Its primary use is to send an EOF to a
616subprocess through a pipe opened for writing without closing the
617pipe. It returns a true value if successful, and C<undef> if
618it encounters an error.
619
748a9306
LW
620=head1 REVISION
621
c1441b10
HM
622This document was last revised on 13-Oct-1998, for Perl 5.004, 5.005, and
6235.006.
748a9306
LW
624
625=cut