Yet Another Example: Stock Price Converter

This final example uses split and patterns. The script in Listing 5.3 is a bit of an enhancement to the stock script on Day 3—the script where you typed in the beginning and ending prices of a stock, and it calculated the return on your investment. In that version of the script, you weren't allowed to enter stocks in the old-style format of fractional prices (14 5/8); we tested for that using regular expressions and printed an error.

This script isn't really a stock script; instead it's more of a price-checking and fixing script. It allows prices in plain numeric format. It checks for nondecimal characters and prints an error, as before. But if you enter in a price in a fractional format, it converts ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.