Perl Programming/Exercise 2

From Wikibooks, open books for an open world
Jump to navigation Jump to search

A. Input from the keyboard[edit | edit source]

  1. Write a program which asks for a line of text to be entered on the keyboard, and then displays that text on screen.
  2. Modify the program to ask for 3 lines of text, and display them on one line with a | symbol between each input.

Input from the command-line[edit | edit source]

Write a program which you can call like so:

perl myscript.pl "Argument 1" "Argument 2" "Argument 3"

The program should take as many command-line arguments as are available, and display them on screen.

Input from a text file[edit | edit source]

Create a text file containing the following 3 lines:

This is line one
This is line two
This is line threes

Write a program which reads the file, and displays all the lines of text. Modify it so that the whole file is output on one line with | characters between each input. use strict; use warnings;

open MYFILE, '3lines.txt'; foreach(<MYFILE>){ chomp; print $_," |"; }

output:

This is line one | This is line two | This is line threes |Press any key to continue . . .

Putting it all together[edit | edit source]

Using the program from exercise 1, write a program which takes three numbers as command-line arguments, and solves the quadratic equation where those numbers are the coefficients (a, b, c). Print the roots.

If no numbers are available from the command-line, prompt the user to type each number at the keyboard.

If the first command-line argument is -h or --help, display some instructions on how to use the program.

Answers

Previous exercise | Next exercise