I am getting some perl compile errors trying to convert these unix commands to perl.
The use of single quotes and double quotes is throwing me off (see below: my $curlcmd).
Here’s the working unix commands executed in order:
export CERT=`/dev/bin/util --show dev.testaccnt | awk '{print $2}'`
/usr/bin/curl -c /home/foobar/cookee.txt --certify /dev/key.crt \
--header "FooBar-Util:'${CERT}'" \
https://devhost.foobar.com:4443/fs/workflow/data/source/productname?val=Summ
I want to do the same within Perl:
#Build cmd in perl
my $cookie='/home/foobar/cookee.txt';
my $certkey='/dev/key.crt';
my $fsProxyHostPort='devhost.foobar.com:4443';
my $fsPath='workflow/data/source/productname';
my $fsProxyOperation='Summ';
my $fsProxyURL="https://$fsProxyHostPort/fs/$fsPath?val=$fsProxyOperation";
#Get cert
my $cert=qx(/dev/bin/pass-util --show foobar.dev.testaccnt | awk '{print \$2}');
Here’s where I am having trouble executing it:
my $curlcmd = qx(/usr/bin/curl -c $cookie --certify $certkey --header "FooBar-Util:'${" . $cert . "}'". $fsProxyURL);
Can someone show me how to setup these commands in Perl correctly?
In the shell script, you have (in part):
This generates something like:
where the
curlcommand gets to see those single quotes. To get the same result in Perl, you will need:Changes:
${ ... }notation.In situations where you have problems seeing the argument list sent to a command, I recommend using a program analogous to the shell
echocommand, but which lists each argument on its own line, rather than as a space-separated set of arguments on a single line. I call my version of thisalfor ‘argument list’. If you test your commands (for example, the shell version) by prefixing the whole command line withal, you get to see the arguments thatcurlwould see. You can then do the same in Perl to compare the argumentscurlsees at the shell with the ones given it by Perl. Then you can fix the problems, typically much more easily.For debugging with
al:If you want to write
alin Perl:Adventures in Validating an Answer
Fortunately, I usually check what I write as answers – and what is written above is mostly accurate, except for one detail; Perl manages to invoke the shell on the command, and in doing so, the shell cleans out the single-quotes:
This yields:
Note the difference in the `FooBar lines!
At this point, you start to wonder what’s the least unclean way to work around this. If you want to use the
qx//operator, then you probably do:This yields the variant outputs (
systemthenqx//):So, the
qx//notation now gives the single quotes to the executed command, just as in the shell script. This meets the goal, so I’m going to suggest that it is ‘OK’ for you; I’m just not sure that I’d actually adopt it in my own code, but I don’t have a cleaner mechanism on hand. I’d like to be able to use thesystemplus ‘array of arguments’ mechanism, while still capturing the output, but I haven’t checked whether there’s a sensible (meaning relatively easy) way to do that.One other passing comment; if any of your arguments contained spaces, you’d have to be excruciatingly careful with what gets passed through the shell. Having the
alcommand available really pays off then. You can’t identify which spaces inechooutput are parts of one argument and which are separators provided byecho.