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