develooper Front page | perl.perl5.porters | Postings from September 2013

[perl #120047] perl should enable "$_" for use before calling subs

Thread Previous | Thread Next
Linda Walsh via RT
September 30, 2013 00:17
[perl #120047] perl should enable "$_" for use before calling subs
Message ID:
On Sun Sep 29 13:24:28 2013, wrote:
> But regarding library code: If your library code blindly modifies global
> variables (such as $_), it's probably broken.
It didn't "blindly modify" it.  The value of $_ was *saved*
in the code that made use of it and restored when done.

That would *seem* to be a perfectly reasonable solution in most 
languages.  Even in assembler, if you use registers, you save them,
you use them, you restore them.

Asking for that ability in perl.

if I have:

sub reader() {
  my save=$_";
  open(my $fh, "<myfile") or die "open:$!";
  while (<$fh>) {

that saves $_ and restores it upon exit -- it will still
fail if, in the callstack above "somewhere", that called this --
it was called from a 

for ('a' 'b' 'c') {

reader will fail with an error claim it attempted to modify
a read-only var.

The library function cannot use while(<>) while "$_" is read
only -- despite the fact that it was trying to be "good" and
saved $_ before using it (and restored it upon exit).

That is the issue.

via perlbug:  queue: perl5 status: rejected

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About