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

Greetings from a lowly altar boy,

I have been asked by my manager to write a number of database querying, timestamping, and error reporting scripts and modules.

I have an email script that generates an email with a timestamp and I would like it to report who called the script. Something similar to $0 but for the parent script. I'm quite new to Perl so I don't know all the technical terms.

For example the call would be:

#args(script to be called, subject, message body) my @args = ('sendmail.pl', 'An error has occurred!', Error!'); system(@args) == 0 or die "system @args failed: $?";

And then I would like sendmail.pl to know which script called it without passing additional arguments. Is this possible?

Right now sendmail.pl is just a script but would converting it into a module make things easier (obviously renaming it in the process)? All it does is take arguments and shoots out an email.

Thanks a ton.

Greg

Replies are listed 'Best First'.
Re: Filename of parent script available?
by Fletch (Bishop) on Jul 15, 2005 at 15:43 UTC

    There's nothing off the shelf, but you could always use getppid and one of the ps-ish modules (Proc::ProcessTable or GTop for example) to find it out.

    Update: of course with the caveat that if the OS allows it the process name may have been changed; or someone nefarious could use symlinks and an intermediary exec even if it doesn't.

    --
    We're looking for people in ATL

      Hmmm... Seems like the easiest thing is to just pass $0 as the first argument. Thanks for the tip though, I will have to check those out when I have more time.

      Greg

Re: Filename of parent script available?
by izut (Chaplain) on Jul 15, 2005 at 16:52 UTC
    You can also use %ENV to send/retrieve information to the child script (example follows)

    script1.pl

    #!/usr/bin/env perl # script1.pl $ENV{'MY_NAME'} = 'script1'; print system('./script2.pl', 'arg'), "\n";

    script2.pl

    #!/usr/bin/env perl # script2.pl print $ENV{'MY_NAME'}, "\n";

    surrender to perl. your code, your rules.

Re: Filename of parent script available?
by radiantmatrix (Parson) on Jul 15, 2005 at 17:23 UTC

    You could "sorta" modularize your sendmail.pl. Wrap the main logic of that script into a function (say, sendmail_main()), and add a line like this at the top of that function:

    my @ARGV = @_;

    Then, in your calling script, you can:

    require 'sendmail.pl'; sendmail_main(@args);

    Inside of Sendmail, you can use caller to discover the name of the calling file.

    I highly suggest, however, that you fully modularize your script; you could always use create a new sendmail.pl that did something like:

    use Sendmail; Sendmail::send_mail(@ARGV);

    if you still need to call the script separately.

    <-radiant.matrix->
    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code