jdamae
jdamae

Reputation: 3909

how to execute a shell command inside of a perl sub routine?

How do I execute a shell command in perl if I call it through a sub routine?

For example, I can run this on the command line to get a particular value:

mycert-util --show test.user.myuser_cd

I want to run this command within a perl subroutine and call it. For example my routine would be get_auth and I want to print out the value of get_auth.

code:

use strict;
use warnings;

#&get_auth;  
#get_auth();

print "The value is: get_auth() \n";

sub get_auth
{
  $exec=`mycert-util --show test.user.myuser_cd`; 
}

Upvotes: 1

Views: 781

Answers (2)

TLP
TLP

Reputation: 67920

Your problem is not the sub, but how you call it. You cannot place sub calls inside quotes. Some equivalent alternative features below.

use feature qw(say);
say "The value is: ", get_auth();

sub get_auth {
   return qx(mycert-util --show test.user.myuser_cd); 
}

Upvotes: 3

JJ.
JJ.

Reputation: 5475

It looks like you probably need to return your $exec variable and then output it properly:

sub get_auth
{
  $exec=`mycert-util --show test.user.myuser_cd`; 
  return $exec;
}

print "The value is: " . get_auth() . "\n";

Upvotes: 2

Related Questions