Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: 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

In reply to Basic Input and Output by root

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (10)
    As of 2014-10-22 07:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (114 votes), past polls