develooper Front page | perl.perl5.porters | Postings from March 2006

useless localization of substr?

From:
hv
Date:
March 19, 2006 08:40
Subject:
useless localization of substr?
Message ID:
200603191648.k2JGmr216349@zen.crypt.org
Blead has a raft of new warnings such as "useless localization of substr",
but the warning and associated diagnostic text are misleading about the
current behaviour. Witness:

zen% perl -wle 'our $a = "ab"; { local substr($a,0,1) = "c"; print "<$a>"; } print "<$a>"'
Useless localization of substr at -e line 1.
Use of uninitialized value $a in substr at -e line 1.
<c>
<ab>
zen% 

.. which shows (I think) that rather than the local having no effect it
is actually doing:
  { local $a; substr($a, 0, 1) = "c"; ... }

.. and that interpretation is also hinted at by the fatal "Can't localize
lexical variable" if you s/our/my/.

The diagnostics text says:
  [...] but in fact the local() currently has no effect. [...]

This behaviour appears to predate the addition of the warning.

It isn't clear to me what the ideal behaviour would be before we can
support what is actually being asked for, but the current behaviour
seems decidedly counterintuitive - I'm inclined to suggest that die()
would be a more correct response.

(The patch for the warning was introduced in
  http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2003-05/msg00014.html
and applied as change #19588.)

Hugo



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About