S
sln
I just have a simple question.
When I call the length function on a scalar, is it read directly
(ie: already know its length), or does it traverse the string
counting its characters until it hits a nul terminator?
As an example, which one of these would be a more efficient test?
I'm not saying these constructs hold any practicality, its just to
test the nature of length.
my $str = 'Start';
my $cnt = 1;
# method 1
while (length ($str) )
{
$str .= (sprintf "more %d", $cnt);
$str = '' if ( $cnt % 10000000 == 0);
$cnt++;
}
# method 2
while (defined $str )
{
$str .= (sprintf "more %d", $cnt);
$str = undef if ( $cnt % 10000000 == 0);
$cnt++;
}
Thanks!
When I call the length function on a scalar, is it read directly
(ie: already know its length), or does it traverse the string
counting its characters until it hits a nul terminator?
As an example, which one of these would be a more efficient test?
I'm not saying these constructs hold any practicality, its just to
test the nature of length.
my $str = 'Start';
my $cnt = 1;
# method 1
while (length ($str) )
{
$str .= (sprintf "more %d", $cnt);
$str = '' if ( $cnt % 10000000 == 0);
$cnt++;
}
# method 2
while (defined $str )
{
$str .= (sprintf "more %d", $cnt);
$str = undef if ( $cnt % 10000000 == 0);
$cnt++;
}
Thanks!