top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Unable to understanding map usage in perl

0 votes
315 views

I am looking at the following code someone wrote and I have difficultly in understand map usage. Can somebody please explain how does the following code work?

print OUT2 join( ',', map { $_=~ s/"/'/g; ""$_"" } @data )
posted Jul 24, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Here is the documentation for map from 'perldoc -f map':

map BLOCK LIST
map EXPR,LIST
Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation.

In your case, BLOCK is

{ $_=~ s/"/'/g; ""$_"" } 

and LIST is @data. Therefore, each element of @data is aliased to $_, the substitution s//"/'/g is performed on it, which changes all double-quote characters to single-quote apostrophes, Then, the second 'line' of the BLOCK is evaluated and pushed onto the result list, which puts the transformed string between double-quotes. The resulting list is joined with '.' characters and printed to the OUT2 file stream.

answer Jul 24, 2013 by anonymous
Similar Questions
+1 vote

I would like to use Perl on an embedded device, which only has 64MB of RAM.
Are there any tricks to reduce the memory usage of the perl interpreter?

+1 vote

I'm looking for introductory to advanced examples of RESTful programming in Perl preferably with some good explanations and best practices.

+1 vote

I have tried to install module PDF::FromHTML from cpan but not able to get it.
Please let me know any other module to do the same. please help

...