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

I am a new guy in Perl. I would like to run all my programs in Perl instead of shell script.

Currently,I have a small script called run.scr which is shown below.

#!/bin/csh
#set path and source license
source /ICT/synplicity/synplify/8.6.2/.cshrc_synplify_8.6.2

#run synplify pro
synplify_pro
When I use system function to run it in Perl.
#!/usr/bin/perl
print "start command\n";
$status1=system "/bin/csh","-fc","source /ICT/synplicity/synplify/8.6.2/.cshrc_synplify_8.6.2";
$status2=system "synplify_pro";
print "$status1,$status2, end command\n";
output from Perl show:
start command
0,65280, end command
Compared to shell script, The synplify_pro is not launched.
1.Is there a way to make it working?
2.Any good book or document in Perl will help me to catch up this kind of skill?
Please comment. Thank in advance.
  • Comment on source the license and run the program in perl

Replies are listed 'Best First'.
Re: source the license and run the program in perl
by Fletch (Bishop) on May 17, 2007 at 14:18 UTC

    This is a FAQ (search for "I {changed directory, modified my environment} in a perl script" in that page). You're running something which executes commands which change the shell's environment. In your case, you run the shell, which then immediately exits. Your Perl process' environment is untouched.

    Update: That being said, the presumption that you should run everything from Perl and ignore the shell's kind of daffy. Right tool for the right job. You don't use a chainsaw to do dental work, you don't try and paint your walls with a hedgehog.

      Ok, now you have my curiosity piqued. I can imagine what job a chainsaw is good at, and what tools are needed for dental work or painting your walls. What I can't figure out is what job, precisely, a hedgehog is the right tool for.

        Awww, come on guys, it's so simple. Maybe you need a refresher course. Hey! It's all ball bearingshedgehogs nowadays. Now you prepare that Fetzer valve with some 3-in-1 oil and some gauze pads. And I'm gonna need 'bout ten quarts of anti-freeze, preferably Prestone. No, no make that Quaker State.

        *cough* Ehrm . . . ah, yes; an explanation.

        Directly manipulating your parent process' environment is one of those "so bad it's not even wrong" things under the POSIX paradigm; similarly attempting to paint something with a small, spine-covered mammal . . .

        Update: Yeah, weak. I've got nothing . . . Oh look, defenseless babies!

Re: source the license and run the program in perl
by marto (Cardinal) on May 17, 2007 at 14:28 UTC
Re: source the license and run the program in perl
by Mr. Muskrat (Canon) on May 17, 2007 at 14:25 UTC

    Fletch gave you a great answer.

    What's wrong with using a shell script to start this program? The task you are trying to accomplish (setting up the environment for synplify pro) is best done in a shell script.

Re: source the license and run the program in perl
by ropey (Hermit) on May 17, 2007 at 14:25 UTC

    Getting your example working shouldnt be hard, your changing your environment though did you mean to ? as that is most likely why it wouldnt run... if you are starting out in perl, first thing to do is to add use strict and warnings, you should read why this is important on other nodes on perlmonks, however simply but it enforces correct programming and warns you if you have done something a dodge. That said in your example - what is the benefit of doing it in perl rather than sh ?...

    #!/usr/bin/perl -w use strict; my $status1 = qx|source /ICT/synplicity/synplify/8.6.2/.cshrc_synplify +_8.6.2|; # Define the program you are calling my $status2 = qx|synplify_pro|; print "Status 1 is $status1, status2 is $status2\n";

    This is of course not fully tested with your example but should do as you think... Seeing as you look to be dealing with unix admin tasks I think the O'Reilly book Perl for System Administration may help you..

    </code>
      My purpose to have this type of code is to run my synplify_pro program in perl script and then analyze the report in perl (not yet development now). I just need to run this perl script to do my job and no need to source license when logging to my account.
      Normally, my MIS will update me the synplify_pro new version by mailing me a the source path. I just need to change in my perl scipt without translating his setting.
      Anyway, thank all of your comment.