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

"use" not allowed in expression #16791

Closed
p5pRT opened this issue Dec 19, 2018 · 7 comments
Closed

"use" not allowed in expression #16791

p5pRT opened this issue Dec 19, 2018 · 7 comments

Comments

@p5pRT
Copy link

p5pRT commented Dec 19, 2018

Migrated from rt.perl.org#133732 (status was 'rejected')

Searchable as RT133732$

@p5pRT
Copy link
Author

p5pRT commented Dec 19, 2018

From Ulrich.Windl@rz.uni-regensburg.de

This is a feature request​:
In Perl 5.18.2 the following code is not allowed (ignore the surrounding {} for now) (``"use" not allowed in expression at /tmp/t.pl line 3, at end of line'')​:
{
use constant VALS => map {
  use integer;
  $_; #...
} (0..255);
}

However this is allowed​:
{
  use integer;
use constant VALS => map {
  $_; #...
} (0..255);
}

I consider both equivalent, with the first (without the surrounding braces) being more elegant. Is there a reason why the first variant does not work?

Regards,
Ulrich

@p5pRT
Copy link
Author

p5pRT commented Dec 19, 2018

From @Abigail

On Wed, Dec 19, 2018 at 02​:28​:06AM -0800, Ulrich Windl (via RT) wrote​:

# New Ticket Created by "Ulrich Windl"
# Please include the string​: [perl #133732]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=133732 >

This is a feature request​:
In Perl 5.18.2 the following code is not allowed (ignore the surrounding {} for now) (``"use" not allowed in expression at /tmp/t.pl line 3, at end of line'')​:
{
use constant VALS => map {
use integer;
$_; #...
} (0..255);
}

However this is allowed​:
{
use integer;
use constant VALS => map {
$_; #...
} (0..255);
}

I consider both equivalent, with the first (without the surrounding braces) being more elegant. Is there a reason why the first variant does not work?

The problem is that if perl sees 'map {' it has to guess what is coming
next. Either it's the start of a 'map BLOCK LIST' expression, or the start
of a 'map EXPR, LIST' expression, where EXPR happens to be a reference
to hash. Perl uses heuristics, and sometimes, it gets it wrong.

Having said that, I am unable to reproduce your issue​:

  $ /opt/perl/5.29.6/bin/perl5.29.6 -c
  use strict;
  use warnings;
  {
  use constant VALS => map {
  use integer;
  $_; #...
  } (0 .. 255);
  }
  ^D
  - syntax OK
  $

But in general, you can help perl determine whether you have 'map EXPR',
or 'map BLOCK', by using a '+' or a ';' as the first character​:

  map {+... # Start of map ANONHASHREF, LIST
  map {;... # Start of map BLOCK LIST

Abigail

@p5pRT
Copy link
Author

p5pRT commented Dec 19, 2018

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

@p5pRT
Copy link
Author

p5pRT commented Dec 19, 2018

From @sisyphus

On Wed, 19 Dec 2018 08​:10​:35 -0800, abigail@​abigail.be wrote​:

Having said that, I am unable to reproduce your issue​:

Yes, on Windows I had to go back to perl-5.20 to reproduce the reported behaviour.
IIRC the OP was running 5.18.

Cheers,
Rob

@p5pRT
Copy link
Author

p5pRT commented Dec 20, 2018

From Ulrich.Windl@rz.uni-regensburg.de

I can confirm that putting a ';' at the start of the block, prevents the ``"use" not allowed in expression'' error (in my older version of Perl), but I'm unsure about the '+'​: The plus has to be added in front of an expression (like "+1;", I guess. But in that case an empty statement (';') is much simpler...

As the most recent version of Perl seems smarter anyway, your resolved my feature request even before you knew it ;-)

Ok to close it!

@p5pRT
Copy link
Author

p5pRT commented Dec 20, 2018

From @jkeenan

On Thu, 20 Dec 2018 08​:27​:55 GMT, Ulrich.Windl@​rz.uni-regensburg.de wrote​:

I can confirm that putting a ';' at the start of the block, prevents
the ``"use" not allowed in expression'' error (in my older version of
Perl), but I'm unsure about the '+'​: The plus has to be added in front
of an expression (like "+1;", I guess. But in that case an empty
statement (';') is much simpler...

As the most recent version of Perl seems smarter anyway, your resolved
my feature request even before you knew it ;-)

Ok to close it!

Per OP, new feature not needed. Closing ticket.

Thank you very much.

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

@p5pRT
Copy link
Author

p5pRT commented Dec 20, 2018

@jkeenan - Status changed from 'open' to 'rejected'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant