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

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

Thread Previous | Thread Next
From:
Linda Walsh via RT
Date:
September 30, 2013 00:17
Subject:
[perl #120047] perl should enable "$_" for use before calling subs
Message ID:
rt-3.6.HEAD-31239-1380500247-208.120047-15-0@perl.org
On Sun Sep 29 13:24:28 2013, plokinom@gmail.com 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>) {
  ...
  }
  $_=$save;
}

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()
}

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
https://rt.perl.org:443/rt3/Ticket/Display.html?id=120047

Thread Previous | Thread Next


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