Richard Hansen
Richard Hansen

Reputation: 54223

Perl `map`: When to use EXPR vs. BLOCK form?

Perl newbie here.

The Perl documentation for map shows two usage forms:

I don't fully understand the semantic difference between the two. When should I choose one form over the other? Does the EXPR form limit me to one expression while the BLOCK form allows me to have multiple statements (more complex logic)?

Upvotes: 2

Views: 579

Answers (1)

ikegami
ikegami

Reputation: 386541

The only difference other than the syntax is the scope introduced by the curlies. For example,

>perl -E"use strict; map my $x = $_, 1,2,3,4; say $x"


>perl -E"use strict; map { my $x = $_ } 1,2,3,4; say $x"
Global symbol "$x" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.

Upvotes: 4

Related Questions