Help - Search - Member List - Calendar
Full Version: Invocation environment detection
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Peter Rabbitson
How would I implement a simple detection mechanism, to be able to run the
same script from he commnd line and from cgi, having the script behave
differently depending on the invocation? (e.g. change the content type and
other formatting stuff)

Thanks

Peter

John Doe
Am Samstag, 5. Mrz 2005 19.48 schrieb Peter Rabbitson:
QUOTE
How would I implement a simple detection mechanism, to be able to run the
same script from he commnd line and from cgi, having the script behave
differently depending on the invocation? (e.g. change the content type and
other formatting stuff)

A) (not really what you want)

sub interactive {
return -t STDIN && -t STDOUT
}

The file test operator -t tells you if the filehandle is a tty or not.
But, if you use it in a program whose STDIN/STDOUT are redirected, even if
called from cmdline, the sub returns false. Called by cron, it returns also
false.


B) Maybe what you want, if you use a POSIX compliant system
(there may be better solutions or even a module):

# has the prog control over its tty?
# returns true even if STDIN/STDOUT are redirected;
# returns false if called by cron
# (see man POSIX)
# [tested]
#
use POSIX qw/getpgrp tcgetpgrp/;
sub controls_tty {
local *TTY;
open TTY, "/dev/tty" or die "error opening tty: $!";
my $tpgrp=tcgetpgrp(fileno(TTY));
my $pgrp =getpgrp();
close TTY or warn "error closing tty: $!";
return ($tpgrp==$pgrp);
}

# returns true if run under CGI
# since the server defines GATEWAY_INTERFACE
# [untested]
#
sub from_cgi {
return $ENV{GATEWAY_INTERFACE}=~/^CGI/o ? 1 : 0;
}

# check between three cases:
# [untested]
#
if (from_cgi()) {
print "called by CGI";
}
elsif (controls_tty) {
print "called from cli";
}
else {
print "called from cron (e.g.?)";
}


corrections from others are welcome :-)

greetings joe

Peter Rabbitson
What if I want it to be able to run under everything:

* CGI (html output)
* Linux tty (text only output)
* ActivePerl (text only output + some workarounds for braindead OS)

(Sorry, forgot to CC)

John Doe
Hello Peter

Am Samstag, 5. Mrz 2005 23.41 schrieb Peter Rabbitson:
QUOTE
What if I want it to be able to run under everything:

* CGI (html output)
* Linux tty (text only output)
* ActivePerl (text only output + some workarounds for braindead OS)

Concerning ActivePerl under Windows I don't know
(I suppose the POSIX-Code didn't run under Windows? - maybe there is a Module
from Activestate or on CPAN (http://search.cpan.org) ?)

Apart from that, you have to check in your output producing code the current
invocation "kind" (sorry for my bad english / see my previous answer) and
produce the output accordingly.

Eventually, you could produce the output in html and then use a module wich
strips the text from the html for text output - not the most efficient way
though. I think the best way depends also on the complexity of the html and on
layout requirements of the plain text output.

hope this helps a bit, otherwise somebody else could eventually provide some
more specific help?

grettings joe

P.S.: I must admit that I'm not a specialist in platform independent code :-(

Graeme St. Clair
You could detect OS by use of something like this:-

if (($^O eq 'MSWin32') or ($^O =~ /cygwin/i)) { ... # works for me

And perhaps context by this:-

if defined $ENV{GATEWAY_INTERFACE} { ... # untested, in my case "CGI/1.1"
in CGI, else undefined

And there's a variable $^V that gives you your revision etc of Perl.

I guess you could lash them all together somehow...

HTH, rgds, GStC.



-----Original Message-----
From: Peter Rabbitson [mailto:[Email Removed]]
Sent: Saturday, March 05, 2005 5:42 PM
To: [Email Removed]
Subject: Re: Invocation environment detection

What if I want it to be able to run under everything:

* CGI (html output)
* Linux tty (text only output)
* ActivePerl (text only output + some workarounds for braindead OS)

(Sorry, forgot to CC)

--
To unsubscribe, e-mail: [Email Removed] For additional
commands, e-mail: [Email Removed] <http://learn.perl.org/>
<http://learn.perl.org/first-response>


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.