Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

CSH script that calls a perl script

by mark4444az (Sexton)
on Oct 16, 2012 at 21:46 UTC ( #999430=perlquestion: print w/ replies, xml ) Need Help??
mark4444az has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a csh file that calls a perl script. The perl script brings in a value that I type in when I run the csh file. I sometimes have to alter that value in the perl script, is there some way to...export that value back to the csh progam that was initially run? If not, is there some way to stop the csh program from within perl? In the perl script the value is brought into the perl file from the csh script like this:
$|=1; $stapl = shift; $serial = shift;

Replies are listed 'Best First'.
Re: CSH script that calls a perl script
by hippo (Prior) on Oct 16, 2012 at 22:07 UTC

    No, there is no way for a called (forked or spawned) program to export values back up the calling chain. You have only two options for passing any sort of data back up the chain. The first is the return value, but you really ought not to use that for arbitrary data values. Its use is so ingrained as a status flag for the completion of the child that to use it for anything else would just cause confusion and other problems in the long term. So, instead you might want to use the second option which is IPC or general I/O, neither of which I'm guessing is trivial in csh.

    Which brings me neatly to the next point. You have read csh programming considered harmful haven't you? It's over 15 years old now and is as true today as it was back then. I would therefore humbly suggest that you would be much better off in both the short term and the long term if you were to bite the bullet and rewrite your csh script as perl for all the reasons mentioned in Tom's paper as well as to make the passing of data between your pieces of perl code so much simpler.

    Of course, that's probably not what you wanted to hear.

      Both no's on your questions I'm afraid. Actually, if I could evaluate a return value as a 1 or 0 I think that would work. That way I could shut the action down from the csh. I'm not sure how exactly to accomplish that but I think that would get me through my current dilemma. I tried using perl only and I may not have time to do that. The main problem there is that it produces a log file from another exe and so far perl does not seem to be working for that.

        Would you be happy with your Perl script returning 0 for success and 255 for failure? If so, take note...

        $ perl -e'die'; echo "GOT $?" Died at -e line 1. GOT 255 $ perl -e'exit'; echo "GOT $?" GOT 0
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: CSH script that calls a perl script
by ig (Vicar) on Oct 17, 2012 at 04:58 UTC

    There are many ways to return data from your perl script to your csh script.

    If the perl script is not otherwise writing to STDOUT, you could output the new value to STDOUT. Your csh script could execute the perl script and capture its STDOUT and use what it receives to update its variable.

    You could have the perl script write a file with the new value in it and have your csh script read the file after it runs the script. If you might have more than one instance of the csh and perl script running at a time, you will have to make sure the filenames are unique. The easiest way might be to generate a unique filename in the csh script (e.g. use the pid in the filename) and pass the filename to the perl script, as you do stapl and serial.

    There are other options for IPC, including named and unnamed pipes and sockets, but these would be a bit more complicated to use. I wouldn't bother unless I had already ruled out the simple options.


      just an addition to that what ig said: Let the perl script generate shell variable assignments like SOME=thing, capture the output of the perl script and evaluate it in the context of the csh script. Than you have the return values in the variables of the csh script. But be careful with variable interpolation, shell meta characters and all that stuff.

      But the core of the idea is: Generate something which can be directly interpreted with csh. A kind of serialize to something that can be deserialized by csh.

      Best regards

        As an example on this method, ssh-agent does this. eval `ssh-agent PARAMS`. ssh-agent generates the commands that the shell will then evaluate.

        OT: In a previous life I also used this method to set up user accounts at a university to have the proper environment for their classwork (see modules). A user's account, on login, would read configuration files that would generate the appropriate variables, aliases, path settings, and other schtuff specific to their coursework or other projects that they were participating in. The shell (independent of which one they used) would then evaluate these shell commands that were generated. It made my life as an admin much easier, the user accounts much more consistent and predictable, and allowed the professors the ability to request applications and configurations particular (or peculiar) to their classes. Not certain if I would use this particular implementation again, but it did work well in the mid 1990's.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999430]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2016-08-26 04:59 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (366 votes). Check out past polls.