http://qs1969.pair.com?node_id=1150034

mordibity has asked for the wisdom of the Perl Monks concerning the following question:

Simplest I think is to just post the canonical example that I run across again and again... I would dearly like this to be all on one line for concision and clarity:

(my @v = keys %foo) == 1 or die "Got more than one: @v\n"; print "V:@v\n";
This gives:
Possible unintended interpolation of @v in string... Global symbol "@v" requires explicit package name...

It's the scoping of the parentheses around the my assignment that causes the problem. If I leave out @v from the error message, the logic is correct and everything works: @v, if assigned to successfully, is correctly set and visible for the print statement. I'm trying to avoid doing work twice (repeating the call to keys in the error message).

The ugly alternative is to move my @v outside the comparison, but it just looks clunky and messes up a nice column of assignments. It's not just hash checking, it could be anything:

my $x = parse_thing(); my $y = $x->{item} or die "Item not found in x.\n"; my @z; (@z = do_xyz()) < 3 or die "Got extra Z: @z\n"; print "Z:@z\n";

This has been bugging me for years, and I'm resigned to it, just wondering if there was a clever way around it.

Thanks!