Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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!

In reply to Declaring with my, assigning, and testing in same line by mordibity

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 02:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found