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

I am attempting to run a program within a perl script. The problem is I am a perl newbie (3 days old). I tried exec() and open() but am clueless on the parameters/arguments. On the a command line, this is the command that I run: /home/mailman/bin/add_members -n/home/mailman/lists/minlist-news/email.out -wy minlist-news How do I run the same program within a perl script?

Replies are listed 'Best First'.
Re: running programs
by melguin (Pilgrim) on Aug 28, 2001 at 23:07 UTC
    There are arguments all around this issue, but I won't go into those.
    system('what ever you want to run');
    or you can try
    print `what ever you want to run`; ##these are backticks
    Hope that helps.

    melguin.

Re: running programs
by arturo (Vicar) on Aug 28, 2001 at 23:08 UTC

    Take a look at the man pages for system and backticks ``

    Backticks are useful if you want to capture the output of the external program.

    my $output = `/home/mailman/bin/add_members -n/home/mailman/lists/minl +ist-news/email.out -wy minlist-news` or die "Cant' add members: $!\n" +;

    The or die part is important -- it will be called if something goes wrong with the external program.

    An alternative to backticks is system, just system "command", which is more useful when you don't need to capture the output and/or when you want more security (it's dangerous to put user input into any call that's going to get executed on your system).

    HTH!

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: running programs
by jlongino (Parson) on Aug 28, 2001 at 23:40 UTC
    As already mentioned, you probably want to use the system command. I would add that you should also use the form:

    my $rc = system "some command"; if ($rc != $whatever_return_code_you_expect) { # do some debugging stuff }
    to take advantage of whatever error codes/messages that may be returned.

    If the code and the comments disagree, then both are probably wrong. -- Norm Schryer

Re: running programs
by derby (Abbot) on Aug 28, 2001 at 23:47 UTC
    Or if you wanted to use exec you need to break your command into a list:

    exec '/home/mailmain/bin/add_members', '-n/home/mailmain/lists/minlist +-news/email.out', '-wy', 'minlist-news'

    or more realistically with options:

    exec '/home/mailmain/bin/add_members', '-n', '/home/mailmain/lists/min +list-news/email.out', '-wy', 'minlist-news'

    But the thing to remember about exec, it replaces the current running process, nothing in your script after the exec call will ever be executed. That's why you normally see the fork/exec model being used. fork will create another (child) process and the exec will replace the child - leaving the parent to either wait for the child and continue or just continue.

    -derby

    Update Well it doesn't automatically replace the child, it can replace the parent. It's up to you on which one it replaces.

Re: running programs
by RayRay459 (Pilgrim) on Aug 29, 2001 at 01:12 UTC
    For a quick answer try
    system("your command or something");
    You can also look at the man page on system or do a search on cpan. (search.cpan.org). Secondly, being somewhat new to perl myself(a couple of months), you should pick up O'Reilly's book "Learning Perl". I was able to learn from it very fast and it teaches you how to start writing meaningful scripts and all of the basics. Good luck and happy hunting.
    Ray
Re: running programs
by Hofmator (Curate) on Aug 29, 2001 at 14:15 UTC

    Ok, to make the set complete, I mention the solution using open:

    open (CMD, "your command |") or die "Couldn't fork: $!"; while (my $line = <CMD>) { # read output from command linewise } # or slurp the whole output into one string # { # local $/; # $string = <CMD>; # } # or read the output into an array for easy line access # my @lines = <CMD>; close CMD;

    -- Hofmator