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

what should be output of perl -we 'print do { foreach (1, 2) { 1; } }' #265

Closed
p5pRT opened this issue Jul 27, 1999 · 4 comments
Closed

Comments

@p5pRT
Copy link

p5pRT commented Jul 27, 1999

Migrated from rt.perl.org#1085 (status was 'resolved')

Searchable as RT1085$

@p5pRT
Copy link
Author

p5pRT commented Jul 27, 1999

From ilya@math.ohio-state.edu

If this were true, then

perl -we "print do { 17; foreach (1,2) { 1; } }"

would print 17. But it prints

  Useless use of a constant in void context at -e line 1.

I consider this (no return of foreach) as a bug, and report it as such.

Ilya

@p5pRT
Copy link
Author

p5pRT commented Jul 16, 2003

From @schwern

If Data​::Dumper is to be believed, it returns an empty string.

$ perl -MData​::Dumper -wle "@​a = do { foreach (1,2) { 1; } }; print Dumper \@​a"
$VAR1 = [
  ''
  ];

Different from an empty do block.

$ perl -MData​::Dumper -wle "@​a = do { }; print Dumper \@​a"
$VAR1 = [];

This explains why do { 17; foreach ... } complains about a useless constant.

A while loop returns 0.

$ perl -MData​::Dumper -wle '$foo = 42; sub foo { $bar = 2; $foo++; while ($bar--) { 1;
} } print Dumper [foo]'
$VAR1 = [
  0
  ];

Changing this behavior can cause serious backwards compat issues. Consider the
options​:

1) Have foreach be "null" so the statement before it is what is returned from the block.

That will alter subroutines such as​:

  sub foo {
  $foo++;
  foreach (...) { ... }
  }

Currently this will return an empty string. If we change the foreach behavior, it will
return the value of $foo.

2) Have foreach return its last evaluated expression in its block.

Similar problem. Any subroutine ending in a foreach loop will suddenly start returning
values.

I suppose it should be documented, but at this point it would likely be better to simply
declare it undefined rather than paint ourselves into a corner by documenting that it
returns false. Perhaps to say simply that for purposes of the "last evaluated expression"
rule, a loop is an expression and its value is undefined.

@p5pRT
Copy link
Author

p5pRT commented Jun 3, 2009

From skasal@redhat.com

On Tue Jul 15 19​:20​:10 2003, schwern wrote​:

A while loop returns 0.

Actually, that has changed​: in my perl-5.10.0, while loop returns an
empty string as well.

So the situation is more consistent now.

It's almost ten years since the ticket was started, almost 6 years since
it was noted that foreach returns an empty string. Let's call it a
tradition and close the ticket.

@p5pRT
Copy link
Author

p5pRT commented Jun 3, 2009

skasal@redhat.com - Status changed from 'open' to 'resolved'

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

1 participant