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

Hi,

I'm most likely not even asking this question correctly so let me try to explain as best I can.

I'm running the following relevance query using curl and need for the $bfquery variable to recognize $bfcomputer as the value of the argument when I run the script. Sort of nesting a variable inside of a variable if you will.

$bfquery='query?relevance=%28names%20of%20it%2C%20ip%20addresses%20of% +20it%2C%20root%20server%20of%20it%2C%20operating%20systems%20of%20it% +2C%20 last%20report%20time%20of%20it%2C%20agent%20versions%20of%20it%2C%20va +lues%20of%20results%20from%20%28BES%20Property%20%22_SupportGroup%22% +29%20of%20it%29%20of%20bes%20 computers%20whose%20%28%20name%20of%20it%20as%20lowercase%20starts%20w +ith%20%22$bfcomputer%22%29';
tried putting {} around $bfcomputer but that did not help.

Kind Regards,

Tony

Replies are listed 'Best First'.
Re: Help with script recognizing variable in string
by Anonymous Monk on Jun 20, 2018 at 01:49 UTC
      Actually using double quotes instead of single quotes worked...loving perl more and more!
        This works if the compiler can unambiguously recognize the end of the embedded variable name, for example if the variable name is followed by a space or a punctuation symbol.

        If it is difficult to know where the variable name ends, you can use the following syntax: ${bfcomputer} to clarify.

        Hmmm, watch out double quotes could lead to problems if you had other variable-looking items, e.g. those prepended by a sigill ($,%,@,&,etc.) in your string and perl would want to interpolate them too! If they happen to exist, perl would be eager to replace them, if they do not exist perl will replace them with empty unless you used use strict; use warnings;

        Why don't you build your $bfquery as a sequence of string concatenations using single quotes or variable contents, for example:

        $bfquery= # this is a string in single quotes so nothing is interpolated 'query?relevance=%28names%20of%20it%2C%20ip%20addresses%20of%20it +%2C%20root%20server%20of%20it%2C%20operating%20systems%20of%20it%2C%2 +0 last%20report%20time%20of%20it%2C%20agent%20versions%20of%20it%2C%20va +lues%20of%20results%20from%20%28BES%20Property%20%22_SupportGroup%22% +29%20of%20it%29%20of%20bes%20 computers%20whose%20%28%20name%20of%20it%20as%20lowercase%20starts%20w +ith%20%22' # add to this the value of this perl variable . $bfcomputer # and also this fixed string . '%22%29' # end of concatenation ;
        loving perl more and more!

        in that case use some more and make it easier to see the query

        use strict; use URI::Escape; my $values = join ', ',( 'names of it', 'ip addresses of it', 'root server of it', 'operating systems of it', 'last report time of it', 'agent versions of it', 'values of results from (BES Property "_SupportGroup") of it' ); my $bfcomputer = 'ABC123'; my $who = 'name of it as lowercase starts with "'.$bfcomputer.'"'; my $relevance = "($values) of bes computers whose ( $who)"; my $bfquery = 'query?relevance='.uri_escape($relevance);
        poj
      Thanks!