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

Hello All, I am having a bit of trouble trying to get desired result into a variable in perl script. I am doing api monitoring through zabbix via a script, and the endpoint returns {"messageType":"SUCCESS","message":"It's alive!"} .My scripts searches for the word SUCCESS and sends appropriate result, if not then zabbix sends errors. I want to put my curl command into a variable within my perl script, but all the ways I have tried do not work, maybe a different approach is needed, please assist. I have tried, but none work, error : Use of uninitialized value $4 in concatenation (.) or string at :

$status=`curl -s https://www.url/service/status| awk -F '"' '$0=$4' `; open(PS, "curl -s https://www.url/service/status| awk -F '"' '$0=$4'" +|) || die ("Failed: $!"); system qq{curl -s https://www.url/service/status| awk -F '"' '$0=$4'};

If I run the command at the command line , I get the desired result which is just the word SUCCESS

Replies are listed 'Best First'.
Re: variablize command in perl script
by hippo (Archbishop) on Aug 03, 2017 at 21:53 UTC
    maybe a different approach is needed

    Perhaps so. Using LWP to handle the HTTP GET request and JSON to parse the response content sounds like a better plan. Give those a go and see how you get on.

      Thank you
      #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use open qw(:std :utf8); my $res; my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } ); my $response = $ua->get("https://www.url.net/service/status"); if($response->decoded_content =~ /"messageType":"(.*?)","(.*?)"/mi ) { $res=$1; } print "$res\n";
Re: variablize command in perl script
by karlgoethebier (Abbot) on Aug 04, 2017 at 08:53 UTC
Re: variablize command in perl script
by Anonymous Monk on Aug 04, 2017 at 01:50 UTC
    a) Do what hippo says, but
    b) Your way will work if you backslash the dollar signs.
      haha , you are right, I tried that , but I only backspaced out \$0 , wish you answered long time ago ... oh well, thank you lol
Re: variablize command in perl script
by Anonymous Monk on Aug 04, 2017 at 01:39 UTC

    Hi,

    Check on cpan for curl modules.

    J.C.

      thank you