In Perl 5.6.1, writing out a little test script i got my error to one little line. Here's the entire script to start with: #!/usr/bin/perl -w
use strict;
my @one = qw( one two three );
my $two = sub { print "we were passed @_\n" };
# here's the problem line:
$two->( map { anda => $_ } @one );
# But this would work fine:
#$two->( map { 1 && anda => $_ } @one );
When compiling that as is Perl bails on a syntax error. The second (more ugly) version works fine, as well as using a variable instead of a hard-coded string. That's fine and good, but why is it happening that way?
i would think one would be able to code as i have above, but (on a Solaris 9 machine running the standard Perl 5.6.1 Sun install) apparently not. If i add parentheses i find out that apparently Perl is viewing { anda => $_ } as a hash reference, but map is expecting a code reference, so that's probably where the discrepency comes from.
Questions:
- Is there some nuance in the code i'm missing that will make it be short and readable instead of adding ugly hacks to get map to read that as a code block instead of a hash reference?
- Is anyone else having this problem? Or is it just me?
- Assuming it's standard behavior from 5.6.1, is it fixed in 5.8?
thanks for any and all help...
jynx
-
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.
|