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

I have a script called ask_input.pl which basically asked for an input. Below is the script.
#!/usr/bin/perl use strict; print "Enter your answer?\n"; # Line 3 <>; # Line 4 print "Done.\n";
So, when I call this script from the command line, it should ask for the input.

Now, when I call this script from another script or is run through the cron, it should bypass the lines where it asks for the inputs specifically lines 3 and 4. I was thinking of using GetOpt::Long to add an argument in the ask_input.pl script, and if that particular argument exists then it will just bypass lines 3 and 4.

Is this the efficient way to do this? What are the other ways of doing this? I read somewhere but could not recall that a Perl module can be run as both as a module and as a stand-alone script. I'm not sure if I can use this in this situation though.

Replies are listed 'Best First'.
Re: Altering Behavior of a Script
by ikegami (Patriarch) on Sep 03, 2009 at 18:15 UTC

    Some times, -t STDIN is used.

    But here, arguments sound like the way to go. If "answer" is provided as an argument, use it. If not, prompt for it.

    PS - If you're want to be interactive, use <STDIN> and not <>.

      I never noticed -t. It even works on Windows!!

Re: Altering Behavior of a Script
by ig (Vicar) on Sep 03, 2009 at 18:24 UTC
Re: Altering Behavior of a Script
by bichonfrise74 (Vicar) on Sep 03, 2009 at 20:37 UTC
    Hi ikegami,

    I was playing with the -t STDIN that you mentioned but I think I missed something. Please see below.

    Here's the ask_input.pl
    #!/usr/bin/perl print "Enter your answer?\n"; if ( -t STDIN ) { <STDIN>; print "Terminal was used.\n"; } else { print "Not a terminal.\n"; } print "Done.\n";
    Here's the caller.pl
    #!/usr/bin/perl use strict; system( "perl /tmp/ask_input.pl" );
    So, when I run caller.pl on the terminal, it will still ask for the input... but I thought it should not do this.

      system doesn't mess with any of the file handles passed to the child. If caller.pl is called interactively, so is ask_input.pl.

      But why are you using -t? You need to get your answer from somewhere. You can't test if you're going to receive an answer from a terminal, so that's the last place you should check. You should base whether you prompt or not based on the presence or absence of the answer elsewhere (e.g. argument, config file).

        Ah, I see what you mean.
Re: Altering Behavior of a Script
by Bloodnok (Vicar) on Sep 03, 2009 at 19:52 UTC
    On *NIX, the environment variable $- will contain 'i' iff the shell was invoked interactively i.e.
    if ($ENV{'-'} =~ 'i') { # interactive invocation } else { # non-interactive e.g. cron, invocation }
    A user level that continues to overstate my experience :-))
      I'm on a Linux system, but the script you provided does not seem to be working for me. In fact, if I do this,
      perl -e 'print join "\n", keys %ENV';
      I do not even see '-', the closest thing that would resemble this is '_'. But if I print the value of '_', it will give me this: /usr/bin/perl
        Bloodnok is thinking of special bash variable $-. It is not an environment variable, and it would be useless to a child anyway.