On 01/20/2015 11:28 PM, Charles DeRykus wrote:
...
or something odd
my $contents = do { local $/; map { chomp } <HANDLE> };
I'm afraid this, while appealing, in my testing generates an
incorrect result, ie, 1.
<SPECULATION>
What happens I suspect is that the map{ } is in void context, not the
scalar context of the outer do{}. Remember parsers are not as
all-knowing as we'd like them to be. Therefore map churns merrily
along and tosses its intermediate results until the final line.
Only at that point does parser wake up and say "Aha, the do{ } wants
scalar context so my final map{} value needs
to be returned in scalar context. So here's what map{} does in
scalar context. From 'perldoc -f map' :
the map call is in scalar, not void context. the do block returns the
value of the last statement and that is being assigned to a scalar.
context is propogated in (like with subs) so map is in scalar context.
regardless, using map with no return value is never a good idea as it
subverts its purpose and misleads the reader of the code.
also chomp works on a list too. but as i asked in another post, why does
the OP want to remove newlines? it will make it hard if not impossible
to parse the text then as criticial whitespace will be gone. if the
lines are kept in an array, that is ok but wanting a scalar of a file
with no newlines is likely not wanted here.
uri
In scalar context,
returns the total number of elements so generated. Evaluates
BLOCK or EXPR in list context, so each element of LIST may
produce zero, one, or more elements in the returned value.
^^^^
Remember, map has tossed all but the final value at this point so it
just returns 1.
Clear as mud?
</SPECULATION>
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/