[SOLVED] passing perl script argument to an outside program
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
passing perl script argument to an outside program
I have a program called checkuser, called from the shell with this syntax:
checkuser <user>@<domain> dsl
I am attempting to write a perl script that takes a username as an argument, and gets info from checkuser. The output is <100 lines, so I just want to pull it as a list of lines, straight to memory.
I hope the following will further suggest what I am attempting (though it still seems incorrect..):
my @checkuserpull = `checkuser $1@domain dsl`
You are doing it methodologically wrong - regardless of language. Your fundamental mistake is the assumption you'll make no mistake.
If you change that assumption,. you'll start writing your code in the manner which allows you to easily check inputs and outputs of intermediate steps.
But it appears you haven't used
Code:
use strict;
use warnings;
in the beginning of your script in the first place - which would have pointed you to the mistake immediately.
Now, back to the wrong assumption - you have to write your code using the following idiom:
Code:
my $cmd = "checkuser ${ARGV[0]}@domain dsl"; # this line is actually wrong
warn "about to get stdout from $cmd command line"; # comment this line out when your code works
my @checkuserpull = `$cmd`;
You assume that I made an assumption about my infallibility. In fact, I normally expect to mess it up, and find mistakes. I did not use
Code:
use strict;
use warnings;
because I am very new, and unaware of such options. They will be added, for sure, since I still have quite a bit to do.
Birei's alterations to my example made it possible to successfully get the information I need. If I have chosen a poor method, perhaps you could suggest a better way? It would be appreciated
You assume that I made an assumption about my infallibility. In fact, I normally expect to mess it up, and find mistakes. I did not use
Code:
use strict;
use warnings;
because I am very new, and unaware of such options. They will be added, for sure, since I still have quite a bit to do.
Birei's alterations to my example made it possible to successfully get the information I need. If I have chosen a poor method, perhaps you could suggest a better way? It would be appreciated
The method of getting the needed is OK, non-checking of actual command line to be executed is not.
My assumption of me screwing up command lines is carved in stone, code code that is:
Code:
cat -n system_wrapper.prl
1 ###############################
2 # written by Sergei Steshenko #
3 ###############################
4
5 use strict;
6
7 sub
8 {
9 my ($info_marker, $command)=@_;
10
11 warn "$info_marker executing ==> ",$command,"\n";
12 # ^
13 # | - it should be to STDERR, not STDOUT, UNIX(R) traditions should be respected.
14 system($command);
15 };
cat -n backticks_wrapper.prl
1 ###############################
2 # written by Sergei Steshenko #
3 ###############################
4
5 use strict;
6
7 sub
8 {
9 my ($info_marker, $warning_marker, $command)=@_;
10
11 warn "$info_marker going to grab STDOUT from '$command' command\n";
12 my $stdout=`$command`;
13 if($?)
14 {
15 warn "$warning_marker '$command' command returned non-zero status: \$?=$?";
16 }
17
18 $stdout; # the returned value
19 };
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.