in reply to appending a variable

You need to use braces as you have with the other variables to detach the trailing underscore from the variable name.

use strict; use warnings; my $opstr = '01'; my $foo = "a_${opstr}_b"; print "$foo\n";

If you remove the braces even this short snippet will not compile.


🦛

Replies are listed 'Best First'.
Re^2: appending a variable
by Marshall (Canon) on Apr 28, 2022 at 20:17 UTC
    Yes, braces are one way to disambiguate where the variable name ends.
    Often I will use the concatenate operator to avoid the need for braces.
    This probably runs slower, but you can space out the var names to be substituted in order to make them more obvious (YMMV).

    my $opstr = '01'; my $foo = "a_${opstr}_b"; my $bar = 'a_'. $opstr .'_b'; print "$foo\n"; print "$bar\n";
    Also, OP probably meant: my ($opstr) = $vmstr =~ /-(\d+)$/; in the case of one digit (e.g. 0)?

      Aktchually they compile down to the same thing under the hood (because the former is just syntactic sugar for the latter more or less):

      $ perl -MO=Deparse,-p,-q -E 'my $foo = qq{a_${blah}_b}; my $bar = q{a_ +} . $blah . q{_b};' use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'postderef_qq +', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; (my $foo = 'a_' . $blah . '_b'); (my $bar = 'a_' . $blah . '_b'); -e syntax OK

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Hi,

        I got an error "Can't call method "YYYYmmddHHMMSS" on an undefined value" at $FN_OUTPUT This is my code

        my $vmstr = "zldcdyh1bdce2d14-xxx-vccpohr-correlator-0"; my ($opstr) = $vmstr =~ /-(\d\d)$/; #using the value of $vmstr + of the VM but with an added 0 my $OPinst = "/PMOSS_SIPNBVQM_5MIN_OHDR_${YYYYmmddHHMMSS$opstr}"; my $P3inst = "/PMOSS_P3_SIPNBVQM_5MIN_OHDR_${YYYYmmddHHMMSS$opstr +}"; $FN_OUTPUT = "$DST_DIR${OPinst}_${INSTANCE}_$$"; # Suff +ixes added later: .processing .completed $FN_P3_OUTPUT ="$DST_DIR${P3inst}_${INSTANCE}_$$"; # Suf +fixes added later: .processing .completed

        Is this incorrect