in reply to Multiple variable assignments with RexExp

Here's a somewhat heretical solution:

map {s/\n/<br\/>/g} $var1, $var2;
Yes, map in void context. It is acting as an operator, modifying its arguments as do chomp, splice, and others. The substution acts on $_ which is, in map's codeblock, an alias to the current list argument This won't work for constant arguments, of course.

Go ahead and assign from it if you need statistics on the number of substitutions. The style campaign against map in void context makes the device all the more helpful at calling attention to using map for side effects.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Multiple variable assignments with RexExp
by rob_au (Abbot) on May 22, 2002 at 05:23 UTC
    Map in a void context! Bad Zaxo! :-)

    I cannot fathom why you would take this approach when the following would be much clearer and avoid the building of a list return which is being discarded:

    s/\n/<br\/>/g for $var1, $var2;

    Note too, that this isn't a "style" issue, but one of using the best function for a given task - Yes, map does fit the task here, however it is only the loop function of map which you are making use of, not the list return, a function better suited to a for or foreach loop.