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

i am using a script say script1.pl . in this script i am caaling other script say scriptp2. when the script1.pl is called for the first time the code for caaling script2.pl will be called, but from next time when ever script1.pl is called the code for caaling script2.pl should not be executed.

the code looks like this:

in script1.pl .......... system("perl script2.pl"); ..........
what i want is
in script1.pl ..... if ( some condition) { system("perl script2.pl"); }
here by condition i mean that only for the first time when i execute script1.pl script2.pl should be called and then after it should noy be called , every time if i call script1.pl

Edited by Chady -- code tags and minor formatting

Replies are listed 'Best First'.
Re: conditional calling of script from other script
by jpk236 (Monk) on Feb 04, 2005 at 05:05 UTC
    can we have some more information to properly diagnose a solution?

    1. is script1.pl going to be run once, then just loop; and you just want script2.pl to run the first iteration?

    2. is there some external factor you could check for to see if script2.pl needs to be run? for instance, if it creates or modifies a file

    if its only being run once at the start, perhaps just running it manually would be more efficient

    - Justin
      1. i want my script1.pl to be executed any number of times. by executing the command # perl $PATH/script1.pl -qx . when this is executed for first time i will then execute script2.pl .now when i execute my script script1.pl second time or more number of times from same command as above. 2. i have to use some external factor to see if the script2.pl needs to be executed but i am unable to decide and that the main i am asking you all. i think this may give you some good idea of what i am asking.

        First of all, to answer your question directly, just have script1 check for the existence of a particular file; if that file doesn't exist, create it and run script2:

        my $LOCKFILE = 'script1.lock'; if (!-e $LOCKFILE) { open FH, ">$LOCKFILE" or die $!; close FH; system("perl script2.pl"); }

        However, if they're both perl scripts, it seems silly to do a fork and invoke another perl interpreter to run it. Can't you just use or require it and call whatever function(s) you need directly?

        -b

        sachin,

        Can you provide us with some code as to what these two scripts do? Thanks.

        Justin
Re: conditional calling of script from other script
by chanakya (Friar) on Feb 04, 2005 at 05:06 UTC
Re: conditional calling of script from other script
by deibyz (Hermit) on Feb 04, 2005 at 11:41 UTC
    What about a "lock" file?

    For example, in script1.pl:

    ... my $lockfile = ".called"; unless( -e $lockfile ){ sytem("perl script2.pl"); open OUTFILE, ">$lockfile" or die "Couldn't create lock file: $!\n +"; }

    Just a first approach, code untested.