Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Is there a simply way to mark the end a variable name like $1 ?

e.g. I want to add a 'n' after the $1:

s/abc(\w+)x/$1.'n'/e; _____ is working.

Is this also possible without the modifier "e" ?

s/abc(\w+)x/$1n/; _____ is not working because "$1" and "n" is merged to "$1n"

The same problem I have at strings: $1."n" is needed for "$1n"

Thanks for your help !!!

Replies are listed 'Best First'.
Re: mark end of variable name (e.g.$1)
by choroba (Cardinal) on Feb 21, 2023 at 08:52 UTC
    Use curly braces:
    ${1}n

    Update: But you don't need them for $1n:

    $ perl -wE 'say "abc" =~ s/(b)/$1n/r' abnc

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Thanks for your answer !
Re: mark end of variable name (e.g.$1)
by Anonymous Monk on Feb 21, 2023 at 10:30 UTC
    s/abc(\w+)x/$1n/; _____ is not working

    youre wrong, it works fine