1Dpkg::Compression::FileHandle(3perlli)bdpkg-peDrplkg::Compression::FileHandle(3perl)
2
3
4
6 Dpkg::Compression::FileHandle - class dealing transparently with file
7 compression
8
10 use Dpkg::Compression::FileHandle;
11
12 my ($fh, @lines);
13
14 $fh = Dpkg::Compression::FileHandle->new(filename => 'sample.gz');
15 print $fh "Something\n";
16 close $fh;
17
18 $fh = Dpkg::Compression::FileHandle->new();
19 open($fh, '>', 'sample.bz2');
20 print $fh "Something\n";
21 close $fh;
22
23 $fh = Dpkg::Compression::FileHandle->new();
24 $fh->open('sample.xz', 'w');
25 $fh->print("Something\n");
26 $fh->close();
27
28 $fh = Dpkg::Compression::FileHandle->new(filename => 'sample.gz');
29 @lines = <$fh>;
30 close $fh;
31
32 $fh = Dpkg::Compression::FileHandle->new();
33 open($fh, '<', 'sample.bz2');
34 @lines = <$fh>;
35 close $fh;
36
37 $fh = Dpkg::Compression::FileHandle->new();
38 $fh->open('sample.xz', 'r');
39 @lines = $fh->getlines();
40 $fh->close();
41
43 Dpkg::Compression::FileHandle is a class that can be used like any
44 filehandle and that deals transparently with compressed files. By
45 default, the compression scheme is guessed from the filename but you
46 can override this behaviour with the method "set_compression".
47
48 If you don't open the file explicitly, it will be auto-opened on the
49 first read or write operation based on the filename set at creation
50 time (or later with the "set_filename" method).
51
52 Once a file has been opened, the filehandle must be closed before being
53 able to open another file.
54
56 The standard functions acting on filehandles should accept a
57 Dpkg::Compression::FileHandle object transparently including "open"
58 (only when using the variant with 3 parameters), "close", "binmode",
59 "eof", "fileno", "getc", "print", "printf", "read", "sysread", "say",
60 "write", "syswrite", "seek", "sysseek", "tell".
61
62 Note however that "seek" and "sysseek" will only work on uncompressed
63 files as compressed files are really pipes to the compressor programs
64 and you can't seek on a pipe.
65
67 The class inherits from IO::File so all methods that work on this class
68 should work for Dpkg::Compression::FileHandle too. There may be
69 exceptions though.
70
72 $fh = Dpkg::Compression::FileHandle->new(%opts)
73 Creates a new filehandle supporting on-the-fly
74 compression/decompression. Supported options are "filename",
75 "compression", "compression_level" (see respective set_* functions)
76 and "add_comp_ext". If "add_comp_ext" evaluates to true, then the
77 extension corresponding to the selected compression scheme is
78 automatically added to the recorded filename. It's obviously
79 incompatible with automatic detection of the compression method.
80
81 $fh->ensure_open($mode, %opts)
82 Ensure the file is opened in the requested mode ("r" for read and
83 "w" for write). The options are passed down to the compressor's
84 spawn() call, if one is used. Opens the file with the recorded
85 filename if needed. If the file is already open but not in the
86 requested mode, then it errors out.
87
88 $fh->set_compression($comp)
89 Defines the compression method used. $comp should one of the
90 methods supported by Dpkg::Compression or "none" or "auto". "none"
91 indicates that the file is uncompressed and "auto" indicates that
92 the method must be guessed based on the filename extension used.
93
94 $fh->set_compression_level($level)
95 Indicate the desired compression level. It should be a value
96 accepted by the function "compression_is_valid_level" of
97 Dpkg::Compression.
98
99 $fh->set_filename($name, [$add_comp_ext])
100 Use $name as filename when the file must be opened/created. If
101 $add_comp_ext is passed, it indicates whether the default extension
102 of the compression method must be automatically added to the
103 filename (or not).
104
105 $file = $fh->get_filename()
106 Returns the filename that would be used when the filehandle must be
107 opened (both in read and write mode). This function errors out if
108 "add_comp_ext" is enabled while the compression method is set to
109 "auto". The returned filename includes the extension of the
110 compression method if "add_comp_ext" is enabled.
111
112 $ret = $fh->use_compression()
113 Returns "0" if no compression is used and the compression method
114 used otherwise. If the compression is set to "auto", the value
115 returned depends on the extension of the filename obtained with the
116 get_filename method.
117
118 $real_fh = $fh->get_filehandle()
119 Returns the real underlying filehandle. Useful if you want to pass
120 it along in a derived class.
121
123 If you want to create a class that inherits from
124 Dpkg::Compression::FileHandle you must be aware that the object is a
125 reference to a GLOB that is returned by Symbol::gensym() and as such
126 it's not a HASH.
127
128 You can store internal data in a hash but you have to use
129 "*$self-"{...}> to access the associated hash like in the example
130 below:
131
132 sub set_option {
133 my ($self, $value) = @_;
134 *$self->{option} = $value;
135 }
136
138 Version 1.01 (dpkg 1.17.11)
139 New argument: $fh->ensure_open() accepts an %opts argument.
140
141 Version 1.00 (dpkg 1.15.6)
142 Mark the module as public.
143
144
145
1461.21.21 2023-03-2D8pkg::Compression::FileHandle(3perl)