Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

A simple question that can't be asked

by Wiggins (Hermit)
on Oct 27, 2017 at 16:25 UTC ( #1202163=perlquestion: print w/replies, xml ) Need Help??

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

In old age, things of the mind ,left unused, will just slip away.
I know there is a style of variable usage that allows the juxtaposition of the variable and a literal character that could be part of the name:
my $v1 = 'A'; my $v2 = 'B' my answer is "_$v1_$v2_";
This will not compile, as I want; a string of the 2 variables surrounded by underscores "_A_B_".
I do not want the underscores as separate strings with concatenates between.
I think I remember something like "_{$v1}_" and I have no idea how to phrase a net search.

It is always better to have seen your target for yourself, rather than depend upon someone else's description.

Replies are listed 'Best First'.
Re: A simple question that can't be asked
by golux (Chaplain) on Oct 27, 2017 at 16:35 UTC
    Hi Wiggins,

    You were close. It's actually:

    my $v1 = 'A'; my $v2 = 'B'; say qq{ my answer is "_${v1}_${v2}_" };

    (And don't forget the semicolon after 'B')
    say  substr+lc crypt(qw $i3 SI$),4,5
Re: A simple question that can't be asked
by Laurent_R (Canon) on Oct 27, 2017 at 17:38 UTC
    When you use a variable name within a (doubly-quoted) string, you have to look at what the next letter after the variable name is. If it is, say, a space or a punctuation sign, then the compiler can know that the variable name ended and will interpolate the variabe correctly. If the next letter if a letter, a digit or an underscore (_), then the compiler has no way of knowing that the variable name just stopped on the previous letter. Then, you need to use curly braces to tell the compiler where the variable name stops.
    my $foo = 4; print "baz$foo bar"; # works OK, the compiler recognizes $foo, thanks +to the space print "$foobar"; # won't work, the compiler will not recognize $fo +o, but look for a variable named $foobar, which does not exist print "${foo}bar"; # now fixed, the compiler can recognize $foo than +ks to the curlies
Re: A simple question that can't be asked
by Paladin (Vicar) on Oct 27, 2017 at 16:36 UTC
    You are close, it's "_${var}_".
Re: A simple question that can't be asked
by kcott (Bishop) on Oct 27, 2017 at 21:58 UTC

    G'day Wiggins,

    As you've already seen, "_{$v1}_" was close, and just needed a small adjustment: "_${v1}_".

    Another option is to use a formatted string with sprintf or printf. Here's an example:

    $ perl -e 'my ($v1, $v2) = qw{A B}; printf "_%s_%s_\n", $v1, $v2' _A_B_

    Here's a further option. It's a bit unwieldy; probably not what you'd want to reach for first; but this type of solution (using join) can occasionally be useful:

    $ perl -le 'my ($v1, $v2) = qw{A B}; print join "_", "", $v1, $v2, ""' _A_B_

    A more appropriate context for using that join solution might be something closer to this:

    $ perl -le 'my @x = "A" .. "Z"; print join "_", "", @x, ""' _A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P_Q_R_S_T_U_V_W_X_Y_Z_

    — Ken

Re: A simple question that can't be asked
by karlgoethebier (Abbot) on Oct 27, 2017 at 20:08 UTC

    This should work as well:

    #!/usr/bin/env perl use strict; use warnings; use feature qw(say); my ( $v1, $v2 ) = qw( A B ); say qq(my answer is "_$v1\_$v2\_"); __END__

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1202163]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2021-07-25 04:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found