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

Greetings all!

It is me again doing things that just aren't meant for the GUI world.

I have created what I call a 'site index file' creation tool...basically it follows all the links at a site (or sites) and logs the URL, Title, and the content. This info gets written to a file which can then be searched on...sort of a hacked Content Index.

Anyways...doing traversals thru a small site (less than 100 pages; dynamic & static) works fine...however, when dealing with a larger site ( 100+ pages; dynamic & static), browsers tend to time out so I can never tell if the script finished or not since the response from the script never makes it back in time. Since I don't know how long the traversal will take, it's not a simple matter of just increasing the browser wait time (if that's possible).

Thinking back to the days when I started using Perl, I figured I would just try running this script from the DOS command line (yes, Virginia, I'm running XP). It works and the debugging information that I inserted displays on each pass. This is much better than outputting to the browser, which seems to wait until the script is completed before it shows me anything...

Only problem is all my debugging text is wrapped in HTML tags...great for browser display, but one big line of crap when output to the command line.

My question: Is there an easy check to tell if a script is being invoked from within a DOS shell vs from a browser. And if so, what is a good (one-liner) way to handle outputting text to each based on that? Something like: print ( if ( dos ) { "$debugtext\n\n"; } else { "<P>$debugtext</P>"; } )

As always - TIA

======================
Sean Shrum
http://www.shrum.net

Replies are listed 'Best First'.
Re: Outputting to browser vs DOS...how can you tell?
by zjunior (Beadle) on Mar 31, 2002 at 12:43 UTC

    First, to output to the browser as soon as the information come from script, set the autoflush to on. This can be done with:

    $| = 1;

    To discover if the script is running from command line, an possible idea is testing the existance if any CGI environment variable, such as GATEWAY_INTERFACE.

    if( defined $ENV{'GATEWAY_INTERFACE'} ) { # we are under CGI } else { # we are under command line }

    Hope this helps

(crazyinsomniac) Re: Outputting to browser vs DOS...how can you tell?
by crazyinsomniac (Prior) on Mar 31, 2002 at 14:03 UTC
Re: Outputting to browser vs DOS...how can you tell?
by gt8073a (Hermit) on Mar 31, 2002 at 14:03 UTC

    Is there an easy check to tell if a script is being invoked from within a DOS shell vs from a browser

    This may be a little more than you are really looking for, but it is a quick example of how to extend your tool.

    #!/usr/bin/perl -w use strict; use Getopt::Long; use vars qw: %switches %actionTable @logActions :; %actionTable = ( 'email' => \&_logEmail, 'browser' => \&_logBrowser, 'console' => \&_logConsole, 'file' => \&_logFile, ); $|++; &GetOptions( \%switches, "email:s", ## script provides a default if none given "file=s", ## MUST have a file name if you want to log to a +file "console", ## console can be target "browser", ## or browser can be target( or both.. ) "url=s", ## url to begin from "help" ## jsut a usage thing ); ## are we just looking for help? if ( $switches{ 'help' } ) { print <<EOHELP; switches: --url <url> site you want to work on --email <address or default if blank> to save output to ema +il --file <file to open> to save output to file --console to use console format --browser to use browser format --help this page EOHELP exit(0); } ## build our @logActions so we know what we have to log to if ( defined( $switches{ 'email' } ) ) { ## we have an email, but it may be empty string, so use default ## notice class act style here! $switches{ 'email' } = 'root@localhost' if ( $switches{ 'email' } +eq "" ); ## open email here if you are going to use it ## ie open( EMAIL, "| /usr/sbin/sendmail -blah )... push( @logActions, 'email' ); } if ( defined( $switches{ 'file' } ) ) { ## open our file, do your security checking too ## open( FILE, '>' . $switches{ 'file' } ) ... push( @logActions, 'file' ); } if ( $ENV{ 'REMOTE_HOST' } ) { ## don't print console data if from browser ## this could be better, but for the sake of an example it'll do $switches{ 'console' } = 0; $switches{ 'browser' } = 1; } ## note no real error checking if no file/email/console/browser select +ed ## legal and illegal combinations are up to the reader push( @logActions, 'console' ) if $switches{ 'console' }; push( @logActions, 'browser' ) if $switches{ 'browser' }; ## do you stuff print '-' x 11, ' START ', '-' x 11, "\n"; output( 'test message a' ); output( 'test message b' ); output( 'test message c' ); print '-' x 11, ' END ', '-' x 11, "\n"; exit(0); sub output { my $text = shift; defined( $actionTable{ $_ } ) && &{$actionTable{ $_ }}( $text ) fo +reach @logActions; } sub _logConsole { print "Console: $_[0]\n"; } sub _logBrowser { print "Browser: <p>$_[0]</p>\n"; } sub _logEmail { ## print EMAIL "$_[0]\n"; print "Email: $_[0]\n"; } sub _logFile { ## print FILE "$_[0]\n"; print "File: $_[0]\n"; }

    I know I find the command line much easier to work with, so I hope this helps you. Also, remember this is just an example, there is a lot left to work out.

    Will perl for money
    JJ Knitis
    (901) 756-7693
    gt8073a@industrialmusic.com

Re: Outputting to browser vs DOS...how can you tell?
by chromatic (Archbishop) on Mar 31, 2002 at 21:20 UTC
    CGI.pm does this by checking the definedness $ENV{'REQUEST_METHOD'}. You could also go further by checking -t STDIN, though if you're not running on an interactive terminal, you'll get different results. (That may be what you want.)