| 1 | package VMS::FlatFile; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | $VERSION = "0.01"; | ||
| 7 | |||
| 8 | # use the IndexedFile module | ||
| 9 | 0 | ▶ | use VMS::IndexedFile; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Data::FixedFormat; | ||
| 11 | |||
| 12 | 1; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my $self = {}; | ||
| 17 | bless $self, $class; | ||
| 18 | $self->_initialize(@_); | ||
| 19 | return $self; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub _initialize { | ||
| 23 | my $self = shift; | ||
| 24 | $self->{FileName} = shift; | ||
| 25 | $self->{Access} = shift; | ||
| 26 | my $fmt = shift; | ||
| 27 | $self->{KeyNum} = shift || 0; | ||
| 28 | $self->{Handle} = tie(%{$self->{File}}, 'VMS::IndexedFile', | ||
| 29 | $self->{FileName}, $self->{KeyNum}, | ||
| 30 | $self->{Access} ? O_RDWR : O_RDONLY) | ||
| 31 | || die "Unable to tie to file $self->{FileName}\n$!\n"; | ||
| 32 | $self->{Formatter} = Data::FixedFormat->new($fmt); | ||
| 33 | 1; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub get { | ||
| 37 | my $self = shift; | ||
| 38 | my $key = shift; | ||
| 39 | my $frec = ${$self->{File}}{$key}; | ||
| 40 | return undef unless $frec; | ||
| 41 | $self->{Formatter}->unformat($frec); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub put { | ||
| 45 | my $self = shift; | ||
| 46 | my $frec = $self->{Formatter}->format(@_); | ||
| 47 | $self->{Handle}->store($frec); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub delete { | ||
| 51 | my $self = shift; | ||
| 52 | my $key = shift; | ||
| 53 | delete(${$self->{File}}{$key}); | ||
| 54 | } | ||
| 55 | |||
| 56 | =head1 NAME | ||
| 57 | |||
| 58 | VMS::FlatFile - read and write hashes with VMS::IndexedFile. | ||
| 59 | |||
| 60 | =head1 SYNOPSIS | ||
| 61 | |||
| 62 | =head2 Standalone | ||
| 63 | |||
| 64 | # Load the module | ||
| 65 | use VMS::FlatFile; | ||
| 66 | |||
| 67 | # Create an instance | ||
| 68 | # args - file name, access (ro=0, rw=1), format, key number | ||
| 69 | my $file = new VMS::FlatFile 'disk$user01:[user]file.dat', 0, | ||
| 70 | [ 'field1:a10', 'field2:a16' ], 0; | ||
| 71 | |||
| 72 | # Read a hash | ||
| 73 | my $hashref = $file->get($key); | ||
| 74 | # Write a hash | ||
| 75 | my $sts = $file->put($hashref); | ||
| 76 | # Delete a record | ||
| 77 | $sts = $file->delete($key); | ||
| 78 | |||
| 79 | =head2 As a Base Class | ||
| 80 | |||
| 81 | # name your derived class | ||
| 82 | package MyFile; | ||
| 83 | |||
| 84 | # Load the module and derive | ||
| 85 | use VMS::FlatFile; | ||
| 86 | use var qw(@ISA); | ||
| 87 | @ISA = qw(VMS::FlatFile); | ||
| 88 | 1; | ||
| 89 | |||
| 90 | # override new | ||
| 91 | sub new { | ||
| 92 | my $class = shift; | ||
| 93 | my $self = {}; | ||
| 94 | bless $self,$class; | ||
| 95 | # default to read only | ||
| 96 | my $access = shift || 0; | ||
| 97 | # use key 0 | ||
| 98 | my $krf = shift || 0; | ||
| 99 | $self->_initialize('disk:[dir]filename.type', $access, | ||
| 100 | [ 'field1:a10', 'field2:a16' ], $krf); | ||
| 101 | return $self; | ||
| 102 | } | ||
| 103 | |||
| 104 | package main; | ||
| 105 | |||
| 106 | # create an instance | ||
| 107 | my $file = new MyFile; | ||
| 108 | my $hashref = $file->get('keyvalue'); | ||
| 109 | |||
| 110 | =head1 DESCRIPTION | ||
| 111 | |||
| 112 | VMS::FlatFile combines VMS::IndexedFile and Data::FixedFormat to make | ||
| 113 | it possible to read and write hashes to VMS indexed files. | ||
| 114 | |||
| 115 | First, load the module: | ||
| 116 | |||
| 117 | use VMS::FlatFile; | ||
| 118 | |||
| 119 | Next, create an instance: | ||
| 120 | |||
| 121 | my $file = new VMS::FlatFile 'disk$user01:[user]file.dat', 0, | ||
| 122 | [ 'field1:a10', 'field2:a16' ], 0; | ||
| 123 | |||
| 124 | The B<new> method accepts four arguments: | ||
| 125 | |||
| 126 | =over 4 | ||
| 127 | |||
| 128 | =item filename | ||
| 129 | |||
| 130 | The filename argument is passed directly to VMS::IndexedFile. | ||
| 131 | |||
| 132 | =item access | ||
| 133 | |||
| 134 | If access is true, the file is opened read/write. If false the file | ||
| 135 | is opened read only. | ||
| 136 | |||
| 137 | =item format | ||
| 138 | |||
| 139 | The format argument is used to construct a Data::FixedFormat instance | ||
| 140 | for the file. This argument is passed directly to | ||
| 141 | Data::FixedFormat::new. | ||
| 142 | |||
| 143 | =item key of reference | ||
| 144 | |||
| 145 | This argument is passed to VMS::IndexedFile to select a key of | ||
| 146 | reference. If not specified or if specified as 0, the file's primary | ||
| 147 | key is used. Specify 1 for the first alternate key, etc. | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | To read records, use the B<get> method: | ||
| 152 | |||
| 153 | my $hashref = $file->get($key); | ||
| 154 | |||
| 155 | B<get> returns a reference to a hash created by | ||
| 156 | Data::FixedFormat::unformat. | ||
| 157 | |||
| 158 | B<get> accepts one argument which is the key of the record to be read. | ||
| 159 | If specified as the null string, the next sequential record is read | ||
| 160 | from the file. | ||
| 161 | |||
| 162 | To write records, use the B<put> method: | ||
| 163 | |||
| 164 | my $sts = $file->put($hashref); | ||
| 165 | |||
| 166 | The status returned by B<put> comes from VMS::IndexedFile::store. The | ||
| 167 | lone argument is a reference to a hash which is converted into a file | ||
| 168 | buffer with Data::FixedFormat::format and written to the file. | ||
| 169 | |||
| 170 | To delete records, use the B<delete> method: | ||
| 171 | |||
| 172 | my $sts = $file->delete($key); | ||
| 173 | |||
| 174 | The record with the specified key value will be deleted from the file. | ||
| 175 | |||
| 176 | The easiest way to use VMS::FlatFile as a base class would be to write a | ||
| 177 | derived module that provides the filename and format arguments for | ||
| 178 | each file you need to access. To do this, override the B<new> method | ||
| 179 | with a routine like the following: | ||
| 180 | |||
| 181 | package MyFile; | ||
| 182 | use VMS::FlatFile; | ||
| 183 | use vars qw(@ISA); | ||
| 184 | @ISA = qw(VMS::FlatFile); | ||
| 185 | |||
| 186 | sub new { | ||
| 187 | my $class = shift; | ||
| 188 | my $self = {}; | ||
| 189 | bless $self,$class; | ||
| 190 | # default to read only | ||
| 191 | my $access = shift || 0; | ||
| 192 | # use key 0 | ||
| 193 | my $krf = shift || 0; | ||
| 194 | $self->_initialize('disk:[dir]filename.type', $access, | ||
| 195 | [ 'field1:a10', 'field2:a16' ], $krf); | ||
| 196 | return $self; | ||
| 197 | } | ||
| 198 | |||
| 199 | The B<_initialize> routine takes the same arguments as B<new>. This | ||
| 200 | new constructor takes two arguments; the access mode (true for | ||
| 201 | read/write) and the key of reference. | ||
| 202 | |||
| 203 | VMS::FlatFile instances contain the attributes: | ||
| 204 | |||
| 205 | =over 4 | ||
| 206 | |||
| 207 | =item File | ||
| 208 | |||
| 209 | This is the hash bound to the file with B<tie>. Records are read from | ||
| 210 | the file by reading attributes (i.e., file keys) from this hash. | ||
| 211 | |||
| 212 | =item Handle | ||
| 213 | |||
| 214 | This attribute receives the result from the call to B<tie> which | ||
| 215 | connects the VMS file to the hash. | ||
| 216 | |||
| 217 | =item Formatter | ||
| 218 | |||
| 219 | The Formatter attribute is an instance of a Data::FixedFormat which is | ||
| 220 | used for converting between file records and hashes. | ||
| 221 | |||
| 222 | =back | ||
| 223 | |||
| 224 | =head1 AUTHOR | ||
| 225 | |||
| 226 | VMS::FlatFile was written by Thomas Pfau <pfau@eclipse.net> | ||
| 227 | http://www.eclipse.net/~pfau/. | ||
| 228 | |||
| 229 | =head1 COPYRIGHT | ||
| 230 | |||
| 231 | Copyright (C) 2000 Thomas Pfau. All rights reserved. | ||
| 232 | |||
| 233 | This module is free software; you can redistribute it and/or modify it | ||
| 234 | under the terms of the GNU General Public License as published by the | ||
| 235 | Free Software Foundation; either version 2 of the License, or (at your | ||
| 236 | option) any later version. | ||
| 237 | |||
| 238 | This library is distributed in the hope that it will be useful, but | ||
| 239 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 240 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 241 | Library General Public License for more details. | ||
| 242 | |||
| 243 | You should have received a copy of the GNU General Public License | ||
| 244 | along with this progam; if not, write to the Free Software Foundation, | ||
| 245 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 246 | |||
| 247 | =cut | ||