in reply to Non-destructive substitution?

A similar question was answered on c.l.p.misc in May. The solution is not mine, but I present it here.

Wanted: function composition using tr, s, etc. operators:

$var2 = s/123/456/g (tr/a-z/A-Z/ ($var));
The answer:
sub apply (&$) { local $_ = $_[1]; $_[0]->(); $_; } $var2 = apply {s/123/456/g; tr/a-z/A-Z/} $var;
or in this case:    $string = apply {s/foo/blah/} $string;

HTH,
Charles K. Clarkson

Replies are listed 'Best First'.
Re: Re: Non-destructive substitution?
by bobione (Pilgrim) on Jul 03, 2001 at 15:32 UTC
    I am not sure that this function "apply()" is shorter or more usefull than a temporary variable... :)

    BobiOne KenoBi ;)

      Probably not, but it may be what was requested: "a cleaner way to do that, preferably without having to use a temporary variable each time".

      TIMTOWTDI,
      Charles K. Clarkson