|
AZ::Splitter - Splitting and extracting data from file or string, with loading into memory in parts |
AZ::Splitter - Splitting and extracting data from file or string, with loading into memory in parts
use AZ::Splitter; # With flags importing into current namespace use AZ::Splitter (); # Access to flags from AZ:: namespace only
$s = AZ::Splitter->new( \*IN ); $s = AZ::Splitter->new( \*IN, $limit ); $s = AZ::Splitter->new( \*IN, $limit, $buffsize ); $s = AZ::Splitter->new( \*IN, $limit, $buffsize, s_2BUFF|s_UTFOK ); $s = AZ::Splitter->new( \$SCALAR, ... ); # Also for scalar variables
$s->read_to( \*OUT, $separator ); $s->read_to( \*OUT, $separator, $limit ); $s->read_to( \*OUT, $separator, $limit, s_APPEND|s_WEND|s_WCASE ); $s->read_to( \*OUT, \@separators, ... ) # Many separators $s->read_to( \$SCALAR, ... ); # Also for scalar variables $s->read_to( undef, ... ); # Only rewind
$s->read_some( \*OUT ); $s->read_some( \*OUT, $limit ); $s->read_some( \*OUT, $limit, s_APPEND ); $s->read_some( \$SCALAR, ... ); # Also for scalar variables $s->read_some( undef, ... ); # Only rewind
$s->stat_rsize(); $s->stat_wsize(); $s->stat_match(); $s->stat_error();
This is an object-oriented class for fast and simple splitting of big files or strings using little memory. Also can be used for searching data in files, but the main fuction is parsing big volume data.
new(INPUT[,LIMIT[,BUFFSIZE[,FLAGS]]])INPUT - is a reference to file stream, opened for reading, or reference to string.
LIMIT - limit size of input stream data in characters. If this argument is not defined or equal to -1, then all input stream data will be available for read.
BUFFSIZE - size of buffer in characters. If this argument is not defined or equal to -1, then will be used default buffer size 32768 characters.
FLAGS - 2 modificators are available in this method:
s_2BUFF - use second buffer. Can really speed up search in case insensitive mode.
s_UTFOK - disable UTF-8 data check in UTF-8 mode. Use this flag if you are absolutely sure, that your UTF-8 data is valid.
read_to(OUTPUT,SEPARATOR[,LIMIT[,FLAGS]])OUTPUT - is a reference to file stream, opened for writing, or reference to string. If another type of value (ex: undef), then data will be not stored.
SEPARATOR - is a string-separator or reference to array with many separators.
Remember! In case of many separators, left separator alltimes have more priority then right!
LIMIT - size in characters. Defines, the maximum number of characters that must be stored in OUTPUT. If this argument not defined or equal to -1, then this method will be trying to store all readed data.
FLAGS - 3 modificators are available in this method:
s_APPEND - append data to OUTPUT if OUTPUT is a reference to string.
s_WCASE - search in case insensitive mode.
s_WEND - at the end of input stream returns only false value. Without this modificator, if end of stream expected at first time, then will be returned true value.
read_some(OUTPUT[,LIMIT[,FLAGS]])OUTPUT - same as in read_to() method.
LIMIT - limit size in characters, how many it is necessary to read. If this argument is not defined or equal to -1, then will be read all available data from input stream.
FLAGS - only one modificator available:
s_APPEND - same as in read_to() method.
stat_wsize()read_some() this
method returns quantity of readed characters.
stat_rsize()stat_match()read_to() returns found separator.
Otherwise returns empty string.
stat_error()read_to()
and read_some() immediately stops any operations, returns
false status and sets up this parameter to true value.
Fully supported when using perl version 5.8.1, or higher. Input stream, separators, and result reference(if this is file stream), must be in UTF-8 mode. If input stream will has malformed UTF-8 data, then reading from stream will be immediately stopped.
Remember! This class is using block reading and before destruct class-object, you should work with input stream only through these class methods.
In UTF-8 mode search without case sensitive is very slowly.. It is because operation of changing case on UTF-8 data has slow speed.
Remember, in UTF-8 mode all sizes of this module contain characters, not bytes!
All flags exist in AZ:: namespace and you can disable importing them to your namespace.
open( my $fh, '<', 'config.txt' ) or die $!; my $stream = AZ::Splitter->new($fh);
my $line;
while( $stream->read_to( \$line, "\r\n" ) ) {
# Do something with $line
}
close($fh) or die $!;
# Initialize array of strings my @strings = ( 'word1', 'word2', 'phrase 1', 'word3' );
open( my $fh, '<', 'file.txt' ) or die $!; my $stream = AZ::Splitter->new($fh);
# Now, let trying to find one of substrings my $result = $stream->read_to( undef, \@strings, -1, s_WCASE|s_WEND );
if($result) {
print "Found substring '", $stream->stat_match(), "'\n";
}
elsif( $stream->stat_error() ) {
print "Fatal error during reading file!\n";
}
else {
print "Nothing found..\n";
}
close($fh) or die $!;
Andrey Fimushkin, <plohaja@mail.ru>
Andrian Zubko aka Ondr, <ondr@cpan.org>
|
AZ::Splitter - Splitting and extracting data from file or string, with loading into memory in parts |