Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

<STDIN> detects only input after perl script is run.

by tty1x (Novice)
on May 03, 2013 at 15:31 UTC ( [id://1031913]=perlquestion: print w/replies, xml ) Need Help??

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

If I enter the alphabet h in together with the perl filname.pl command in Terminal ( perl filename.pl h ), it doesn't work.
It only works if I do it separately.
perl filename.pl
h


How do I make it work if I enter both together on the same line ? Thanks :)
#!/usr/bin/perl –w chomp(my $user_value = <STDIN>); if ($user_value eq "h") { print " hi \n"; }

Replies are listed 'Best First'.
Re: <STDIN> detects only input after perl script is run.
by SuicideJunkie (Vicar) on May 03, 2013 at 15:36 UTC

    perl filename.pl his passing "h" as a command line parameter. You will find it in @ARGV

    perl filename.pl h
    is providing no command line parameters, and sending "h" to STDIN

      Ahh thanks for the @ARGV . That answers my question :)
Re: <STDIN> detects only input after perl script is run.
by blue_cowdawg (Monsignor) on May 03, 2013 at 15:41 UTC

    If you wanted to send an "h" to standard input you should invoke your script

    echo "h" | perl filename.pl
    also another thought:
    #!/usr/bin/perl -w use strict; #damn fine idea my $user_value=<STDIN>; chomp $user_vaule;


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Noted on  use strict :)
Re: <STDIN> detects only input after perl script is run.
by karlgoethebier (Abbot) on May 03, 2013 at 16:49 UTC

    Perhaps you should think about using Getopt::Long or something similar?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1031913]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found