K
kj
Consider the following demo script:
# first line
# second line
use File::Slurp 'slurp';
use IO::String;
{
my $string = slurp( $0 );
my $io_string = IO::String->new( $string );
printf ">>>%s<<<\n", scalar $io_string->READLINE;
printf ">>>%s<<<\n", scalar $io_string->READLINE;
}
{
my $io_string = IO::String->new( slurp( $0 ) );
printf ">>>%s<<<\n", scalar $io_string->READLINE;
printf ">>>%s<<<\n", scalar $io_string->READLINE;
}
__END__
The only difference between the two blocks is that the first one
assigns the value returned by slurp( $0 ) to the intermediate
lexical variable $io_string, while the second one uses this value
directly.
As far as I can tell, the output of both blocks should be identical,
but they are not even close. The output I get for the script above
is:
Note that in the second block, the second line never gets printed;
it is treated as an empty string.
Stepping through the code with the debugger, I narrowed down the
problem to the first line in the definition of IO::String::READLINE:
sub READLINE
{
goto &getlines if wantarray;
goto &getline;
}
Somehow, in the failing cases, the wantarray in the first line of
READLINE evaluates to true, even though the original calling
environment clearly specifies the scalar keyword. Therefore getlines
gets inappropriately called, rather than the correct getline.
How can the *really* force a scalar environment? (Clearly, the
scalar keyword is not doing the job here.)
FWIW, I'm using perl v5.10.0 on Ubuntu Linux.
TIA!
~K
# first line
# second line
use File::Slurp 'slurp';
use IO::String;
{
my $string = slurp( $0 );
my $io_string = IO::String->new( $string );
printf ">>>%s<<<\n", scalar $io_string->READLINE;
printf ">>>%s<<<\n", scalar $io_string->READLINE;
}
{
my $io_string = IO::String->new( slurp( $0 ) );
printf ">>>%s<<<\n", scalar $io_string->READLINE;
printf ">>>%s<<<\n", scalar $io_string->READLINE;
}
__END__
The only difference between the two blocks is that the first one
assigns the value returned by slurp( $0 ) to the intermediate
lexical variable $io_string, while the second one uses this value
directly.
As far as I can tell, the output of both blocks should be identical,
but they are not even close. The output I get for the script above
is:
Note that in the second block, the second line never gets printed;
it is treated as an empty string.
Stepping through the code with the debugger, I narrowed down the
problem to the first line in the definition of IO::String::READLINE:
sub READLINE
{
goto &getlines if wantarray;
goto &getline;
}
Somehow, in the failing cases, the wantarray in the first line of
READLINE evaluates to true, even though the original calling
environment clearly specifies the scalar keyword. Therefore getlines
gets inappropriately called, rather than the correct getline.
How can the *really* force a scalar environment? (Clearly, the
scalar keyword is not doing the job here.)
FWIW, I'm using perl v5.10.0 on Ubuntu Linux.
TIA!
~K