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

Hello Monks I have a program namely "retrieve" which receives one paramenter, CUSTOMERID and print the name and the status of the customer
***
./retrive 2301
CID: Jenny
STATUS: Active
***
May you please advice how i can call this program from perl and be able to assign this output to some variables

Thank you in advance

/S
  • Comment on Retrieving information from an executable program

Replies are listed 'Best First'.
Re: Retrieving information from an executable program
by Corion (Patriarch) on Jul 25, 2010 at 18:23 UTC

    The easiest way is to use backticks (or qx):

    my $number = "2301"; my @output = `./retrive $number`; chomp @output; # remove line endings

    The second step would be then to separate the keys and values into pairs. This is easiest done with split:

    use Data::Dumper; my %data; for my $line (@output) { my ($key, $value) = split /:/, $line, 2; $data{ $key } = $value; }; print Dumper \%data;
      Thanks for your quick reply. I will give it a try. Now suppose I want to set two parameters, say
      my $name=; my $status;
      Is there a way of assigning them inside a for loop, say using switch or if-else

      Sam

        No, there basically is no way to assign your variables that way. Well, you could store the names and references in a hash and then use that:

        my ($name, $status, $cid); my %vars = ( 'NAME' => \$name, 'CID' => \$cid, 'STATUS' => \$status, ); for my $line (@output) { my ($key, $val) = split /:/, $line, 2; if ($vars{ $key }) { ${ $vars{ $key } } = $val; } else { warn "Unknown key '$key', discarded"; }; };

        I'm not sure whether that's worth the trouble though.

Re: Retrieving information from an executable program
by jwkrahn (Abbot) on Jul 25, 2010 at 19:48 UTC
    my ( $name, $status ) = `./retrive 2301` =~ /(?=CID:\s*(.+))(?=STATUS: +\s*(.+))/;
      I am very grateful for your replies. I will give it a short and revert

      Sam

        I have tried both codes and all give me the output to the console. What i now want is to assign the value to my variable so that i can do more manipulation with the data, say my $newstatus=$status;
        #!/usr/bin/perl use strict; use warnings; my ( $name, $status ) = `./retrieve 2301` =~ /(?=CID:\s*(.+))(?=STATUS +:+\s*(.+))/; my $newstatus $status; //Just be able to access the variable with data print $newstatus;
Re: Retrieving information from an executable program
by ivo_ac (Acolyte) on Jul 25, 2010 at 19:43 UTC

    Hi, you can get the result by using backquotes like:

    my @output = `./retrive 2301`;

    Then, you can use a regular expression to fill your variables:

    my $cid=""; foreach (@output) { if ($_ =~/^CID: (.*)/) { $cid=$1; } }

    If performance is an issue, you can precompile this regular expression so Perl doesnt have to do this every time in the loop. Here is a complete little program:

    #!/usr/bin/perl use strict; use warnings; my @output = `./retrive 2301`; my $cid=""; my $r_regex = qr/^CID: (.*)/; foreach (@output) { if ($_ =~ $r_regex) { $cid=$1; } }