NAME
Test::MockCommand::ScalarReadline - reads scalars using $/ behaviour
SYNOPSIS
use Test::MockCommand::ScalarReadline qw(scalar_readline);
my $s = "Hello\nWorld\n";
$/ = "\n"; my @x = scalar_readline($s); # returns ("Hello\n", "World\n");
$/ = "xyz"; my @x = scalar_readline($s); # returns ("Hello\nWorld\n");
$/ = "or"; my @x = scalar_readline($s); # returns ("Hello\nWor", "ld\n");
my $record_size = 3; $/ = \$record_size;
my @x = scalar_readline($string); # returns ("Hel", "lo\n", "Wor", "ld\n");
# can also be used in scalar context to get one line at a time
my ($line, $chars_to_cut);
while (defined ($line = scalar_readline($s, $chars_to_cut))) {
# ...
$s = substr($s, $chars_to_cut);
}
DESCRIPTION
This module provides the scalar_readline
function, which breaks a scalar into a list the same way that readline
breaks an input stream up into lines, depending on the current value of $/
.
FUNCTIONS
- @all_lines = scalar_readline($string)
- $line = scalar_readline($string, $chars_to_cut)
-
In list context, returns a list containing
$string
broken apart into lines according to the current value of$/
.In a scalar context, returns the first line from
$string
, In order to get the next line, you have to provide a second parameter, which must be a scalar variable. The number of bytes to cut from the start of your data will be written back into this variable.In order to fully emulate readline(), when
$/
is set toundef
(slurp mode), you must return""
the first time a string is empty andundef
thereafter. This function does not do that.