console input in perl!
hi
i am a newbie and want to know why the following code does not take input from the console in perl. #!/usr/bin/perl -w print "Name?"; a$= ; print "Hello"; print $a; print "\n"; thanks for the help in advance. Farhan Ali |
You must read the input with <STDIN> like
$a = <STDIN>; |
Yes, you could use <STDIN> for input.
Also, you used a$, when it should've been $a. But, you should avoid using $a (or $b) in general, since these are variables that have special meaning in Perl (for sorting). A function that will come in handy is 'chomp()', which will remove the newline that is stored in your variable automatically (unless you change the input field seperator). Otherwise you might get into trouble/errors in variable context later on. For example if you type "123" and press enter ('\n'): after $a=<STDIN>; $a will store "123\n" if you then do chomp($a);, $a will now store "123". It may not seem important right now, but it's very usefull to get rid of the newline character after input. |
thanks guys!
i got it working now... Farhan Ali |
If you want to actually prompt the user you could use:
Code:
#!/usr/bin/perl |
Well david_ross , the answer is written twice already
|
:P - Half asleep - sorry ;)
|
No problem
|
All times are GMT -5. The time now is 11:10 PM. |