in reply to Perl::Critic says don't modify $_ in list functions and other things
Other monks have mentioned this, but not in these words: map, grep, and for/foreach arrange for $_ to be an alias to each element of the list in turn. In a map block, $_ is not its own variable and modifying $_ will reach back into the original list. In some cases, $_ can even be aliased to some value slot that is read-only, and attempting to modify $_ will fail; for this reason, subs using $_ should localize *_, which replaces the entire glob and "shadows" any aliases that have been established in outer contexts.
There are several reasons to avoid eval STRING that do not affect eval BLOCK. An obvious problem is that eval STRING parses Perl code from a string. If that string is untrusted remote user input and not very, very, very strictly checked, you have a remote code execution vulnerability. There is also a performance issue, in that eval STRING must invoke perl's parser each time it is reached at runtime, while the BLOCK in eval BLOCK is compiled along with the rest of the program. This also leads to one of the reasons eval STRING can be needed: because the STRING is not compiled until runtime, eval STRING can also trap compilation errors in STRING, while a compilation error in the BLOCK in eval BLOCK is fatal before execution starts.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl::Critic says don't modify $_ in list functions and other things
by haukex (Archbishop) on Jul 10, 2020 at 19:34 UTC | |
by jcb (Parson) on Jul 10, 2020 at 23:40 UTC | |
by haukex (Archbishop) on Jul 11, 2020 at 06:18 UTC | |
by jcb (Parson) on Jul 12, 2020 at 03:20 UTC | |
by haukex (Archbishop) on Jul 12, 2020 at 05:58 UTC |