Home / code / cgi

Standard input

You don't have to "hard code" the variables. You can get your variables from outside the script. If the user enters variables, it is called Standard Input.

Standard in put uses <STDIN>.

print "Please tell me your name:";

$name=<STDIN>;

print "Thanks for making me happy $name! \n";

If you execute the script you'll see that the !-mark is placed on another line. This is because, when you enter the variable it also stores the Enter. You can remove the enter is you use the chop function.

print "Please tell me your name:";

$name=<STDIN>;

chop $name;

print "Thanks for making me happy $name! \n";
The chop function removes the last character from the variable. You can use a shorter version.
print "Please tell me your name:";

chop ($name=<STDIN>);

print "Thanks for making me happy $name! \n";
When a enter must be removed, chop is seldom used. The chomp removes only the last function if the last character is a Enter.
print "Please tell me your name:";

chomp ($name=<STDIN>);

print "Thanks for making me happy $name! \n";

 

TOP

Latest script:

 

Books: