Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Using PIped-in and invocation arguments in same script

by justkar4u (Novice)
on Apr 07, 2011 at 19:34 UTC ( #898176=perlquestion: print w/ replies, xml ) Need Help??
justkar4u has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I'm trying to handle the possibility that a script executes based on following scenarios 1> no arguments are passed to the script 2> arguments maybe passed from command line 3> piped data is passed to a Perl script from file.
if ($#ARGV == -1){..#no arguments passed.} elsif ($#ARGV == 1){..# arguments passed from command line.} elsif (-t STDIN and not @ARGV){..#piped inputs are supplied....do this +} else {default: usage error}
The first two scenarios works fine.The piped input scenario works good if i just use it as a seperate perl script nd do test.pl < input.txt But when i try to combine it with above two scenarios it always enters the first case. I read somewhere that -t option is used to check if handle is open to tty and since piped inputs are supplied through stdin i thought this works but somehow it doesnt. I also tried using (*stdin) in the third case but it messes up the other two options ..Any sugestions ? - Thanks Ram

Comment on Using PIped-in and invocation arguments in same script
Select or Download Code
Re: Using PIped-in and invocation arguments in same script
by wind (Priest) on Apr 07, 2011 at 19:44 UTC

    Change the order of your tests. Note that No Arguments and default are the same thing:

    if (@ARGV){ print "Command line args\n"; } elsif (! -t STDIN) { print "STDIN\n"; } else { print "Default\n"; }

    I don't believe your -t STDIN test will work though. Don't know what the right test is for that at the moment.

    Update: Fixed STDIN test per Anon Monk's post.

      Thank you Wind for your suggestion. I was actually wanting to display an error in case none of the three scenarios were used so used 4 conditions :) Now its working great.
Re: Using PIped-in and invocation arguments in same script
by Anonymous Monk on Apr 07, 2011 at 19:58 UTC
    Any sugestions ?

    Look into truth tables, and test -t STDIN first

    $ perl -le " warn 0 + @ARGV; warn 0 + -t STDIN" 0 at -e line 1. 1 at -e line 1. $ perl -le " warn 0 + @ARGV; warn 0 + -t STDIN" a b c d e f 6 at -e line 1. 1 at -e line 1. $ perl -le " warn 0 + @ARGV; warn 0 + -t STDIN" a b c d < NUL 4 at -e line 1. 0 at -e line 1. $ perl -le " warn 0 + @ARGV; warn 0 + -t STDIN" < NUL 0 at -e line 1. 0 at -e line 1.
      Thank you Anonymous monk for your suggestion. Learned something new !
Re: Using PIped-in and invocation arguments in same script
by Eliya (Vicar) on Apr 07, 2011 at 20:17 UTC
    ...and since piped inputs are supplied through stdin i thought this works

    Note that if you pipe input to the script (or redirect stdin), the script's stdin is no longer connected to the tty, but rather to the pipe.

    In other words, if (-t STDIN) would indicate that nothing is being piped to the script, i.e. that it would get its stdin interactively from the terminal.

      Thank you Eliya for the insight :).
Re: Using PIped-in and invocation arguments in same script
by Anonymous Monk on Jan 24, 2013 at 13:28 UTC
    -p is for pipe :)
    $ perl -le " warn 0+ -t STDIN ; warn 0+ -p STDIN " 1 at -e line 1. 0 at -e line 1. $ echo | perl -le " warn 0+ -t STDIN ; warn 0+ -p STDIN " 0 at -e line 1. 1 at -e line 1.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://898176]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (16)
As of 2014-09-16 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (24 votes), past polls