map and grep are powerful Perl functions for working with arrays:
my @nums = (1, 2, 3, 4, 5, 6); my @squared = map { $_ * $_ } @nums; # (1, 4, 9, 16, 25, 36) my @even = grep { $_ % 2 == 0 } @nums; # (2, 4, 6)
map and grep, the variable $_ by default contains the current element. Modifying $_ will change the element of the original array if the array is passed by reference or if $_ is explicitly used (e.g., through aliasing with foreach).map always returns a list, grep returns a subset of the original list.Question: What does the following code do and why?
my @nums = (1..5); my @result = map { $_++ } @nums; print "@nums ";
Answer: This code will not change the original array @nums. The operator $_++ increments the value of the variable inside the block but does not save these changes to the original array, since map returns the modified value but the original array remains unaffected (unless aliasing is used through foreach).
my @nums = (1..5); my @result = map { $_++ } @nums; # @result will be (1,2,3,4,5), @nums remains unchanged print "@nums "; # Prints: 1 2 3 4 5
Story In one project, a developer expected that after
map { $_++ } @array, the array@arrayitself would be modified. As a result, the program continued to work with the old values, leading to incorrect calculations during data aggregation.
Story In a reporting system, when filtering an array using
grep, inside the block, an assignment command$result = $_was accidentally used, causing all elements to be overwritten in the same variable, losing the data source. This complicated debugging and led to losses in reporting.
Story In an integration script, nested
maps were used and it was forgotten that the inner context also operates with the shared variable$_, causing unpredictable behavior when modifying elements, as the innermapoverwrote values in the resulting array.