I guess I wasn't clear enough. There are two distinct points.

One is that scalar variables are interpolated (i.e. the variable is replaced by its value, for example when printing out the string) within a string if the string is initialized within double quotes, but not if the string is initialized within simple quotes. So the change that you did from single quotes to double quotes was a good move (although there are some limitations, as mentioned below by other monks).

my $var = "foo"; print 'Variable name is $var'; # prints: Variable name is $var print "Variable is $var"; # prints: Variable is foo
Then, even when the encompassing string is within double quotes, the compiler has to be able to know where the variable name ends to be able to interpolate the variable. It will be able to do so if the variable name is followed, for example, by a space or a punctuation symbol; but it might not be able to do it if the variable is followed by characters that are valid within an variable identifier, for example by some letters. In that case, a pair of curly brackets makes it possible to know where the variable name ends and the rest of the string starts, thereby removing any ambiguity. For example:
my $var = 10; print "There are $varapples"; # error: the compiler cannot know wh +ere the variable name ends print "There are $var apples"; # OK: the space after the variable m +akes it possible to know that the variable ends just there print "There are ${var}apples"; # prints: there are 10apples. # A space is missing, but the print +statement works, because the compiler can know # where the variable name ends thank +s to the curlies;

I hope this is clearer.


In reply to Re^5: Help with script recognizing variable in string by Laurent_R
in thread Help with script recognizing variable in string by TonyNY

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.