How can I determine if a script was called from the command line or as a cgi script?

后端 未结 3 1051
天命终不由人
天命终不由人 2020-12-06 12:21

I have a script that I wrote that can either be used on the command line or as a CGI script, and need to determine how the script was called so I can output a content-type h

3条回答
  •  天涯浪人
    2020-12-06 12:37

    I usually do a little trick at the beginning of my module:

    exit run(@ARGV) unless caller();   # run directly if called from command line
    
    sub run
    {
        process_options(@_);
        ...
    }
    
    sub process_options {
        @ARGV = @_;
        my  %opts;
        GetOptions(\%opts,
        ...
    }
    

    The module does not have to be named "run".

提交回复
热议问题