Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: appending a variable

by Marshall (Canon)
on Apr 28, 2022 at 20:17 UTC ( [id://11143400]=note: print w/replies, xml ) Need Help??


in reply to Re: appending a variable
in thread appending a variable

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)?

Replies are listed 'Best First'.
Re^3: appending a variable
by Fletch (Bishop) on Apr 28, 2022 at 20:24 UTC

    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

        Yes, it is incorrect. You now have a $ inside braces - why have you done that?

        Fixing these 8 lines in isolation gives:

        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

        Note however that $INSTANCE is undefined. It is always best to provide an SSCCE.


        🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11143400]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-28 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found