Let the script figure it out

brian_d_foy on 2004-05-08T20:38:56

Today's exercise: let the program, which is really a module with a run() mehtod, figure out what to do based on how I call it. Not using semicolons is just a bonus feature.

sub run
	{	
	$_[0]->new->${ 
		\(do { 
			if( defined $ENV{ REQUEST_METHOD } ) { 'as_html' }
			elsif( -t STDIN )                    { 'as_text' }
			else                                 { 'as_file' }
			})
		}
	}


file

Juerd on 2004-05-08T22:43:01

I know what plain text look like and I'm familiar with HTML. But what kind of "file" is that?

Re:file

brian_d_foy on 2004-05-08T23:00:25

I don't know what kind of file it is. Right now it's a placeholder method that catches anything that falls through.

STDIN or STDOUT

bart on 2004-05-10T08:42:33

I find -t STDIN a bit of an odd criterium to decide what kind of output to get. I often run programs from the command line, that doesn't mean I want interactive output. Or on the contrary, it's not because my source comes from a file, that it means I want it to go to a file.

In short, I'd rather use -t STDOUT to decide on kind of output instead. I imagine that ls behaves this way.

If you want to override it, there's always command line switches.