Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2021-09-24 13:06 GMT
Find Nodes?
    Voting Booth?

    No recent polls found