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