How do i read input from the user in perl?



  • Link: http://perldoc.perl.org/functions/chomp.html

    If you require input from the user, you this will be done using standard input, which is usually the computer keyboard. In the perl you use the line input operator <STDIN> see the example code.

    #!/usr/bin/perl
    print "Who is this? ";
    $readline = <STDIN>;
    chomp($readline);
    if ($readline eq "Eva") {
      print "Big Up from $readline!\n";
    } else {
      print "This is not Eva, it's $readline.\n";
    }
    

    The chomp keyword/operator removes any trailing string from the $readline variable.
    Run the program

    $ ./myperl3
    Who is this? Eva
    Big Up from Eva!
    $ ./myperl3
    Who is this? Hillary
    This is not Eva, it's Hillary.
    

Log in to reply
 

© Lightnetics 2024