Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Basic Input and Output

by root (Monk)
on Nov 05, 1999 at 05:53 UTC ( #955=perltutorial: print w/replies, xml ) Need Help??

Perl makes input and output extremely easy. When you know just a few simple things you'll find that you can read from the keyboard and display output to your monitor with ease. Once you get that down you'll find that reading and writing to files isn't much more difficult.

First we will show you how to read a line of text from the keyboard. It doesn't take much. All you need is the diamond, or spaceship operator which looks something like this: <>. You may also have the name of an optional stream inside of the diamond operator like: <STDIN> or <FILE>.

<> or <STDIN> amount to input from the keyboard unless you have redefined <STDIN> or redirected a file to STDIN. The diamond operator by default reads to a newline or what ever is in $/ is set to. Ok enough talking, now for some quick examples.
$a=<>; #reads one line in from the keyboard including #the newline character (\n) and puts it into $a $b=<STDIN>; #does the same thing only puts it into $b; while(<>){ #reads lines until end of file or a Control-D from t +he keyboard print "$_"; #prints lines back out }

Note in the while loop above Perl automatically put the input into the default variable: $_. Perl does this whenever a test for a loop consists of only reading input like <..>. If this is the case Perl just plops the line read into $_;
Some useful functions to use with <> are chomp and chop. Both remove things from the end of a string. In chop's case it removes the last character from a string. In chomp's case it removes the suffix of the string only if it matches the input record separator $/ which is by default the newline character(\n). Both can take a scalar or array of strings to act on as their argument. If no argument is used the default of $_ is assumed.

Output is straightforward with perl. Basically the print statement takes a string or list of strings and sends them to standard output. Here are some examples:
print "Hello World\n"; #the simplest sort of print print "Hello ","World\n"; #this prints the same thing but uses a list +of strings to do the same thing print ("1+1=",(1+1)); #prints 1+1=2 first prints the string 1+1= a +nd then what 1+1 evaluates to(2);

The printf function gives you a lot more control over your output. If you're familiar with C you'll know basically how this works. A quick example:
<BR> printf "%3d %7.2f %5s\n",$a,$b,$c;

This results in $a getting printed as an integer in a 3 space field followed by a space, then $b printed as a float in a field of size seven with 2 decimal digits, then a space and then $c as 5 character field.

You should now learn about File Input and Output

Replies are listed 'Best First'.
RE: Basic Input and Output
by Anonymous Monk on Apr 24, 2000 at 22:44 UTC
    In your example, it looks like: $b=<STDIN>; and my browser didn't substitute the <>.
RE: Basic Input and Output
by Anonymous Monk on Apr 24, 2000 at 22:46 UTC
    Let's try that one, again. In the example, the ampersand-lt-semicolon and the ampersand-gt-semicolon didn't get converted to < and > for the line: $b=<STDIN>;
      Thanks that one should be fixed now... that was written before <CODE> tags took care of all of that stuff....

      vroom | Tim Vroom |
Re: Basic Input and Output
by Anonymous Monk on Oct 20, 2001 at 20:32 UTC
    while(<>){ #reads lines until end of file or a Control-D from the keyboard .. unless you are on a windows system, in which case you press Ctrl-Z instead.
      yes .... but when I added a line after the while like this:
      while(<>){ #reads lines until end of file or a Control-D print "$_"; #prints lines back out } print "done";
      why don't I see the 'done' after Ctrl-Z?
        I think this is *x system. The "Ctrl+Z" is mapped to the suspend signal (SIGTSTP). This suspends the execution of the program. You can restart the execution of the program by : fg %1 (if this is the first task to be suspended). Actually, "Ctrl+Z" does not bring you out of the while loop. On starting it again, you will still be inside the while loop. You will have to press "Ctrl+D" to come out of the loop.
      ... or Ctrl + C or Ctrl + Break ...
Re: Basic Input and Output
by sleek (Sexton) on Sep 24, 2002 at 06:59 UTC
    What is <br> doing before printf statement? Is it just by error in html of the page or some perl code?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://955]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2023-10-02 21:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found