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

Hi, I am very new to perl, just started this week :) so my question might sound a bit dumb but I am lost and need some help. I am trying to write a simple program which will read a number input by the user and print it. 10x

Replies are listed 'Best First'.
Re: Read from Input
by virtualsue (Vicar) on May 07, 2004 at 10:59 UTC
    Hi, I am very new to perl, just started this week :) so my question might sound a bit dumb but I am lost and need some help. I am trying to write a simple program which will read a number input by the user and print it. 10x

    You can get input from the user by reading from "STDIN", the standard input device, which defaults to your keyboard.

    my $input = <STDIN>;
    You can print that input, which is now in the variable $input, with the print statement.
    print $input;
    Did you want it printed 10 times, or was that 10 kisses? ;-)
    print $input x 10;
    When you get input from the keyboard, you also get the newline character at the end. If you don't want that, use the 'chomp' function to get rid of it. I suggest you check out the excellent Tutorials on this site, and don't forget to use Super Search to look up more examples that you can learn from.
      10x => ten X => thanks
Re: Read from Input
by aquarium (Curate) on May 07, 2004 at 10:42 UTC
Re: Read from Input
by hmerrill (Friar) on May 07, 2004 at 13:43 UTC
    Virtualsue gave you a nice answer to your question. But since you are a Perl newbie I thought I'd take the opportunity to tell you (if you don't know already) about the excellent documentation that comes with perl, and is also available online. The documentation is called the Perldocs. The perldocs are installed when Perl itself is installed. At a command prompt you can do
    perldoc perl
    to get started. In the "Overview" section you can see these listed:
    perl Perl overview (this section) perlintro Perl introduction for beginners perltoc Perl documentation table of contents
    So then to view the perldocs for "perlintro" you would do
    perldoc perlintro
    at a command prompt. You get the idea.

    Perl is a very mature language with excellent documentation available. You can also find the perldocs online at www.perldoc.com.

    HTH.

Re: Read from Input
by ysth (Canon) on May 07, 2004 at 16:06 UTC
    To expand a little, <> is the line input operator. You can use it with or without a filehandle (a filehandle is an entity that holds information about a file you have opened). (<> also has a secondary use if you put something inside it that doesn't look like a filehandle; to always make read a line, use the readline(optional-filehandle) function instead.)

    When your script starts, it has the filehandle STDIN already available, for reading input from the keyboard (or other source, if the command interpreter you used to start the script specified a different source). So you can say <STDIN> or readline(STDIN) to read one or more lines of input. To tell it whether to read one or more lines, you put the call in a context: saying $x = <STDIN>; or print scalar(<STDIN>) is using it in scalar context, so only one line is read. Saying @x = <STDIN>; (which assigns lines to an array) or print <STDIN> is using it in list context so all available lines are read until end of file is reached (on the keyboard often specified by Control-Z or Control-D).

    The notion of context is a difficult one for many perl beginners; basically, any operator or built-in function or subroutine will apply a specific context to each of its arguments/operands, sometimes based on what context the operator/built-in itself is in. This is mostly documented in perldoc perlop or perldoc perlfunc.

    If you leave the filehandle out, the magical filehandle ARGV is used. It will read through any filenames specified as command line arguments to your script, or read from STDIN if no filenames were given.

Re: Read from Input
by pbeckingham (Parson) on May 07, 2004 at 12:04 UTC

    perl -e "print <>"

      Actually in your program <> is interpreted in list context, because this is standard context for print. This means that nothing is outputted until the user enters ^Z (or whatever else end of input is). You might want to go with perl -e "for (1..10) { print scalar <> }".

        You are absolutely right. I was, though, trying to hint towards the use of <> and $_, without providing the entire two-line program.

        As for the 10x - I just #ifgnored it.