[Melbourne-pm] When is an lvalue an lvalue?
Tim Connors
tconnors at rather.puzzling.org
Sun Feb 19 23:24:45 PST 2012
Got a strange one:
substr EXPR,OFFSET,LENGTH
If OFFSET and LENGTH specify a substring that is partly outside the
string, only the part within the string is returned. If the
substring is beyond either end of the string, substr() returns the
undefined value and produces a warning. When used as an lvalue,
specifying a substring that is entirely outside the string raises
an exception. Here's an example showing the behavior for boundary
cases:
And yet the following exits:
$md5->add(substr($$dataRef, $seekPosn, 131072));
(part of lib/BackupPC/Lib.pm in backuppc, triggered by BackupPC_tarPCCopy
- I'm pretty sure it's raising an excepting, but the path is tortuous,
not least because I wasn't quite sure why the exit code was 0, but then I
just realised I was then piping the output to tar on another machine.
duh)
My workaround stops the premature bailing out:
my $tmp_str = substr($$dataRef, $seekPosn, 131072);
#print STDERR "gothere3\n";
$md5->add($tmp_str);
Not really an lvalue, is it? I can sort of see why it does that - it
doesn't know that the value is being passed to a function that isn't then
going to modify its arguments, but ... counterintuitive. I wonder if
there's another way substr could have handled the returning of null data
that pleases legacy programs that don't follow perl best practices.
If only perl were lisp, then it would know its value wasn't going to be
modified :)
--
Tim Connors
More information about the Melbourne-pm
mailing list