One-Liners: Simple Calculator

Ever needed to do a little math really quick, the sort of thing that's too complex to do in your head? I normally keep a calculator around for just these reasons, but my desk is really messy (one of the occupational hazards of being a writer), and I'm lucky if I can find stuff I really need. My computer, like all computers, comes with an online calculator, but because I'm often sitting working with Perl at a command line I can type faster than I can pull down a menu.

With one-liners, Perl enables your computer to become the most complicated basic calculator in the world.

% perl
						-e 'print 154/7'
22

Note

Don't forget, on Windows, use double quotes, not single quotes after the -e.

This isn't a very elegant one-liner. ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.