in reply to Re^3: Help with script recognizing variable in string
in thread Help with script recognizing variable in string

Hi Laurent, The return I get when using ${bfcomputer} yields all of the computers of the site not the computer supplied as the command line argument, but again for some reason when I use double quotes it works. Could this have something to do with using the system command on a Solaris box?
  • Comment on Re^4: Help with script recognizing variable in string

Replies are listed 'Best First'.
Re^5: Help with script recognizing variable in string
by Laurent_R (Canon) on Jun 20, 2018 at 20:38 UTC
    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.

Re^5: Help with script recognizing variable in string
by AnomalousMonk (Archbishop) on Jun 21, 2018 at 06:11 UTC

    Further to Laurent_R's post:   Here are some more variants for scalar syntax; these generalize to all variable types:

    c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; my $var = 10; my $hard_ref = \$var; sub func { return $var; } sub func_ref { return \ $var; } ;; print qq{arbitrary whitespace within the curlies: ${ var }apples \n}, qq{dereference of hard reference: ${ $hard_ref }apples \n}, qq{deref of ref to function return value: ${ \ func() }apples \n} +, qq{deref of function returning ref: ${ func_ref() }apples \n}, qq{deref of ref to expression result: ${ \(11 + func()) }apples \n} +, ; ;; no strict 'refs'; ;; our $name = 'road'; my $soft_ref = 'name'; ;; print qq{dereference of symbolic reference: ${ $soft_ref }apples \n}, qq{quoted literal symbolic identifier: ${ 'name' }apples \n}, ; " arbitrary whitespace within the curlies: 10apples dereference of hard reference: 10apples deref of ref to function return value: 10apples deref of function returning ref: 10apples deref of ref to expression result: 21apples dereference of symbolic reference: roadapples quoted literal symbolic identifier: roadapples
    This looks to be mostly a bunch of examples of references, but I think of an expression like  ${ symbol_name } as being a dereference of a literal symbol name, so there's a natural transition to a broader consideration of references. See perldata and perlref.

    Some questions for consideration:

    • What happens if the  no strict 'refs'; statement before the symbolic reference examples is removed?
    • If the package-global  our $name ... ; variable is made a lexical (my) variable, e.g.,
          my $name = 'road';
      what happens?
    • Why are symbolic references Officially Frowned Upon? (hint)


    Give a man a fish:  <%-{-{-{-<