Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow access to outer $_'s #17165

Closed
p5pRT opened this issue Oct 2, 2019 · 6 comments
Closed

Allow access to outer $_'s #17165

p5pRT opened this issue Oct 2, 2019 · 6 comments
Assignees

Comments

@p5pRT
Copy link

p5pRT commented Oct 2, 2019

Migrated from rt.perl.org#134466 (status was 'open')

Searchable as RT134466$

@p5pRT
Copy link
Author

p5pRT commented Oct 2, 2019

From @jidanni

Created by [email protected]

$ man perlvar

$_ : the default iterator variable in a "foreach" loop if no other
variable is supplied.

OK, but wouldn't it be neat if one could reference "outer $_'s" too,
without the fuss of naming them?

for ( $doc->createElement('Folder') ) {
  for ( $doc->createElement('Placemark') ) {
$__->appendChild($_);
for ( $doc->createElement('Camera') ) {
  $__->appendChild($_);
}
  }

Here $__ refers to the "parent $_".

A $___ could refer to the "grandparent $_".

Anyway, all of them are remembered in some stack, just currently not
accessible from inner loops, for "no good reason." ($__ probably isn't
the best name for it, I just made it up on the spur of the moment.
Maybe have a number in the variable indication how many levels up it
is to access.)

Perl Info

Flags:
    category=core
    severity=wishlist

Site configuration information for perl 5.28.1:...

@p5pRT
Copy link
Author

p5pRT commented Oct 5, 2019

From @jkeenan

On Wed, 02 Oct 2019 19​:45​:48 GMT, jidanni wrote​:

This is a bug report for perl from jidanni@​gmail.com,
generated with the help of perlbug 1.41 running under perl 5.28.1.

-----------------------------------------------------------------
[Please describe your issue here]

$ man perlvar

$_ : the default iterator variable in a "foreach" loop if no other
variable is supplied.

OK, but wouldn't it be neat if one could reference "outer $_'s" too,
without the fuss of naming them?

for ( $doc->createElement('Folder') ) {
for ( $doc->createElement('Placemark') ) {
$__->appendChild($_);
for ( $doc->createElement('Camera') ) {
$__->appendChild($_);
}
}

Here $__ refers to the "parent $_".

A $___ could refer to the "grandparent $_".

Anyway, all of them are remembered in some stack, just currently not
accessible from inner loops, for "no good reason." ($__ probably isn't
the best name for it, I just made it up on the spur of the moment.
Maybe have a number in the variable indication how many levels up it
is to access.)

I suspect that, given the many other uses of '$_' (perldoc perlvar), this would be an implementation nightmare and very bug-prone.

Thank you very much.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Oct 5, 2019

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Oct 8, 2019

From @tonycoz

On Wed, 02 Oct 2019 12​:45​:48 -0700, jidanni wrote​:

$ man perlvar

$_ : the default iterator variable in a "foreach" loop if no other
variable is supplied.

OK, but wouldn't it be neat if one could reference "outer $_'s" too,
without the fuss of naming them?

for ( $doc->createElement('Folder') ) {
for ( $doc->createElement('Placemark') ) {
$__->appendChild($_);
for ( $doc->createElement('Camera') ) {
$__->appendChild($_);
}
}

Here $__ refers to the "parent $_".

A $___ could refer to the "grandparent $_".

Anyway, all of them are remembered in some stack, just currently not
accessible from inner loops, for "no good reason." ($__ probably isn't
the best name for it, I just made it up on the spur of the moment.
Maybe have a number in the variable indication how many levels up it
is to access.)

They are remembered on the save stack, but it's non-trivial to get to it, which strikes me as not "no good reason".

We could make it simpler to get to those values (add magic to each $_ to point to the parent for example), but that would cost to all such localizing of $_, since $_ is global, and we don't know if the programmer is accessing $__, $___ etc in a called function.

The $__, $___ names are bad, since you'd forcing a maintainer to count _ which due to the _ glyph joining together will be difficult to count at a glance.

You could just name the values, which will make the code more readable.

Tony

@tonycoz
Copy link
Contributor

tonycoz commented Nov 18, 2019

I plan to reject this, unless someone else has an opposing opinion.

@jidanni
Copy link
Member

jidanni commented Nov 18, 2019

OK go ahead. Maybe it wasn't a good idea.

@tonycoz tonycoz closed this as completed Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants