Module::Generic::Scalar::IO - Generic Module Scalar IO Class
my $s = Module::Generic::Scalar->new; my $io = $s->open || die( $s->error ); # or my $io = Module::Generic::Scalar::IO->new( $scalar_object ); $io->opened; # Return true if opened or false otherwise $io->fileno; # no-op method $io->flush; # ditto $io->print( <<EOT ); Mignonne, allons voir si la rose Qui ce matin avoit desclose Sa robe de pourpre au Soleil, A point perdu cette vesprée Les plis de sa robe pourprée, Et son teint au vostre pareil. EOT $io->printf( "Author: %s\n", 'Pierre de Ronsard' ); $io->getc; # return nothing, because we are at the end of file $io->eof; # return true if we are at the end of string $io->tell; # tells us our position in string $io->seek(0,0); my $l = $io->getline; # fetch the first line $io->seek( $io->length - 1, 0 ); my $n = $io->write( ', Les Odes', 10 ); $io->seek(0,0); @lines = $io->getlines; print $lines[-1], "\n"; # Returns: Author: Pierre de Ronsard, Les Odes\n # The file handle is overloaded, so we can do: print "Data is now: $io\n"; # This will return the content of the string. $io->close;
v0.1.0
This class Module::Generic::Scalar::IO inherits from IO::Scalar while enable it to use Module::Generic::Scalar objects.
Thus, this makes it possible to use Module::Generic::Scalar object as file handle to print and get data to and from it.
It takes an Module::Generic::Scalar object and calls "open". It returns a blessed file handle created by IO::Scalar
This is a no-ope; it does not do anything.
This merely untie the scalar. In the IO::Scalar, the scalar reference passed are tied so they can be used in non-object oriented way also.
This method overrides the one from IO::Scalar that would otherwise destroy our underlying Module::Generic::Scalar object.
Returns true if we are positioned at the end of the string, false otherwise.
This is a no-op that does not do anything.
Return the next character from our last position, or undef if none remain.
head2 getline
Return the next line, or undef on end of string.
This can only be called in list context, or IO::Scalar will raise an exception.
Get all the lines from the position we are in the string.
So using our example in the "SYNOPSIS" above :
$io->seek(0,0); # position ourself at the start of the string my $c = $io->getc; # get the first character, which is 'M' my @lines = $io->getlines; This will fetch all 6 lines, except the first one will only contain: ignonne, allons voir si la rose
i.e. without the leading "M", since "getc" positioned us after.
SO, be careful about your position in the string.
Returns the underlying Module::Generic::Scalar object.
This takes as its sole argument an Module::Generic::Scalar object
Returns true if the scalar is opened or false otherwise.
When the filehandle is active, it is "tied" in perlfunc, so this checks that. Otherwise, it does not do anything else.
This print at the last position in the string the list of data provided.
As of version 2.113 of IO::Scalar, this method merely pushes data at the end of the string without accounting for the last position.
This method corrects this. If you want to make sure you are at the end, do:
$io->seek(0,2);
my $buff; $io->read( $buff, 1024 ); # or $io->read( $buff, 1024, $offset );
Takes a string as a buffer, a length and an optional offset in the buffer and will attempt to read from our scalar the requested length and place the result in the buffer.
It returns the number of data read at the last position, or undef if there was an error.
undef
$io->seek( POSITION, WHENCE );
This takes 2 arguments: an integer representing a position in the string and another integer representing the action to take.
Quoting from "seek" in perlfunc: "The values for WHENCE are 0 to set the new position *in bytes* to POSITION; 1 to set it to the current position plus POSITION; and 2 to set it to EOF plus POSITION, typically negative."
This is an alias for "read"
This is an alias for "seek"
This is an alias for "write"
Returns the current position in the string.
$io->truncate( $length ); $io->truncate( $io->tell );
This truncates the string to the specified length $length. It returns true if successful, and false otherwise, such as when at the end of the string.
$length
The position in the string is left unchanged. You may want to call seek before writing to the string.
$io->write( $data ); $io->write( $data, 1024 ); $io->write( $data, 1024, $offset );
This takes some data $data, and optionally some length of those data to take and optionally at a given $offset in $data, and will "print" those data at the last position in string.
$data
$offset
Module::Generic::Scalar, IO::Scalar
Jacques Deguest <jack@deguest.jp>
Copyright (c) 2021 DEGUEST Pte. Ltd.
You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.
3 POD Errors
The following errors were encountered while parsing the POD:
Unknown directive: =head
'=item' outside of any '=over'
You forgot a '=back' before '=head2'
To install Module::Generic, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Module::Generic
CPAN shell
perl -MCPAN -e shell install Module::Generic
For more information on module installation, please visit the detailed CPAN module installation guide.