http://www.perlmonks.org?node_id=1044560


in reply to What is wrong here (Replacing Strin pieces)

It is possible, but I am not sure it is comprehensible. I would not recommend it for production code:
$string =~ s/\A(-+)|(-+)$/("?" x length $1).("?" x length $2)/eg;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: What is wrong here (Replacing Strin pieces)
by AnomalousMonk (Archbishop) on Jul 16, 2013 at 16:23 UTC
    $string =~ s/\A(-+)|(-+)$/("?" x length $1).("?" x length $2)/eg;

    That does the substitutions, but will also produce warnings if run with warnings:

    >perl -wMstrict -le "my $s = '---------xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy--------'; print qq{'$s'}; ;; $s =~ s{ \A (-+) | (-+) \z } { ('?' x length $1) . ('?' x length $2) }xmsge; print qq{'$s'}; " '---------xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy--------' Use of uninitialized value $2 in repeat (x) at -e line 1. Use of uninitialized value $1 in repeat (x) at -e line 1. '?????????xxxxxxxxxxxxxxx------yyyyyyyyyyyyyy????????'