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

I am writing code using LWP module ( to send the HTTP request) I am sending HTTP request with following content:
$req->content('{"type":"mcc","expression":"716*","order":1,"match-type +":"regex-match","category"->'$location_id'}')
I want to pass the variable to the "category" part, how Can I pass the variable "$location_id" ( variable is already defined and having some value.)

Replies are listed 'Best First'.
Re: How to pass variable in the LWP module
by 1nickt (Canon) on Jun 30, 2015 at 11:59 UTC

    Hello Bhushan,

    1. "Learn about strings and interpolating double quotes."
    2. Don't continue until you have completed #1.
    3. "Build your string outside the call to ->content()."
    4. Don't continue until you have completed #3.
    5. Use Data::Dumper to see what is in the variables. It may not be what you think. Like:
      use strict; use warnings; use Data::Dumper; use feature qw/ say /; my $string; # your code here to build a complex string say Dumper( $string );
    6. Don't continue until you have completed #5.

    In general, it looks like you are rushing too much to try to make your scripts accomplish your ultimate goal, without learning the steps necessary to get there. I suggest that you try to learn some basics about Perl programming before trying to do relatively complex tasks like speaking JSON to a remote server. Have you worked through any Perl tutorials?

    There are no shortcuts to becoming an effective programmer. You might be able to find out how to do what you want with a question on PerlMonks or Stack Exchange, but if you don't understand what it is that you are doing and why it works, you will not be able to fix it when it breaks or extend it when $boss wants a new feature.

    The requirements of your ($boss, $job) might mean that you just have to get It working Now ... we've all been there. But then you have to spend many many hours practicing the basic techniques in an environment that is _not_ your live project, in order to really learn what is going on.

    One effective technique is to get a script or a piece of code that works, from here or from somewhere else, then "reverse engineer" it to see how it works. Start commenting out lines and reducing the complexity of the code, all the way down so it simply compiles, with no function. Then start adding the lines back in one by one, not advancing until you understand. Reduce the data to the simplest it can be (eg just one element in an array) ... just make the simplest script you can. Gradually you can build the complexity back in.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to pass variable in the LWP module
by Corion (Patriarch) on Jun 30, 2015 at 09:13 UTC

    Build your string outside the call to ->content().

    Also, let me recommend that you use a module to build your JSON string, for example JSON::XS.

Re: How to pass variable in the LWP module
by Anonymous Monk on Jun 30, 2015 at 09:13 UTC
Re: How to pass variable in the LWP module
by AnomalousMonk (Archbishop) on Jun 30, 2015 at 19:27 UTC

    I agree with others that your basic problem seems to be a lack of understanding of string interpolation. In the OPed code, you seem to be trying to include a single-quote literal character in a single-quoted string, and also to interpolate a variable into a single-quoted string (such strings do not interpolate).

    Here are two possible approaches to what seem to be your problems. The first, which I prefer, uses double-quote interpolation along with properly chosen quote delimiters. The second uses explicit string concatenation.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $location_id = 'Kansas'; ;; my $string = qq{{\"category\"->'$location_id'}}; print qq{:$string:}; ;; $location_id = 'OverTheMoon'; ;; $string = '{\"category\"->\'' . $location_id . '\'}'; print qq{:$string:}; " :{"category"->'Kansas'}: :{"category"->'OverTheMoon'}:

    Note that because I am giving you a Windows command-line code example, I have to escape all the double-quote literal characters in the code. You do not have to bother with this annoyance. Also note that the second code example, of explicit concatenation, has some single-quote characters that are escaped. You do need the escapes in these cases.

    Please see Quote and Quote-like Operators. Also see the  . (concatenation) operator in Additive Operators in perlop. (Update: Actually, the latter link isn't terribly informative!)


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