The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

# NAME

PC - A simple but feature filled command line Perl calculator

# SYNOPSIS

``  pc 11+3``

# DESCRIPTION

`pc` is a quick and dirty command line perl calculator. Pass it an expression on the command line and it will print the result in a number of different formats. You can also run `pc` with no command line parameters and it will run an interactive loop (using `Term::ReadLine` if you have it installed).

`pc` is designed to give you all the output you ever could want without having to memorize any stupid command line switches. There are none, in fact. It also doesn't overload you with redundant data. If the floating point answer is the same as the integer answer then it doesn't show it to you. Same with the fractions.

# TUTORIAL

## The basics

``````  \$ pc 11+3
14 0xe 016``````

The first number is the integer result, followed by the hex and octal representations. Simple enough.

## Order of operations

This shows that `pc` uses Perl's order of operations (operator precedence if you are in a programming mood):

``````  \$ pc 1+3*2
7 0x7 07``````

## ASCII

``````  \$ pc 1+3*20
61 0x3d 075 '='``````

Here we see an extra field was printed. In this case `pc` detected the final integer value was in the ASCII range and printed the character represented by the value 61, an equal sign.

## Bitwise Operations

We also get Perl (and C) style bitwise operators:

``````  \$ pc '1+3*20<<2 & 0xff'
244 0xf4 0364``````

Also notice that I had to quote it since (a) I put a space in the expression and (b) I used the '<' and '&' characters which mean something to the shell.

## Floating point

Of course it's not restricted to only integers, it can handle floats too:

``````  \$ pc 1+3*20/55
2 0x2 02 2.0909090909090 23/11``````

You'll notice it shows the result of the floating point math in addition to the integer math.

## Fractions

You might have noticed a fraction in the output of the previous example. `pc` uses Perl's "bigrat" library to do fractions:

``````  \$ pc 3/4+1/2
0 0x0 01 1.25 5/4``````

``````  \$ pc 1000*2000
2,000,000 2000000 0x1e,8480 0x1e8480 07502200 1.90MB``````

You'll notice that the integer and hex results are printed twice--one with commas and one without. The one with commas is so that you the human can read the output easily. The one without commas is for copying and pasting. You should also notice that `pc` helpfully told you the number was 1.90MB. If a number is bigger than 1024 (1KB) it will print it in human readable byte quantity.

## Power of 2 magnitude suffixes

It also accepts magnitude suffixes on the input:

``````  \$ pc 16m
16,777,216 16777216 0x100,0000 0x1000000 0100000000 16MB``````

The following suffixes are allowed: kmgtpezy (lower or upper case, with or without a trailing "b"). Note that the human readable output "16MB" doesn't have a decimal point. It will remove the decimal point if the number is exactly that value. So:

``````  \$ pc 16m+1
16,777,217 16777217 0x100,0001 0x1000001 0100000001 16.0MB``````

Since "16.0MB" has a decimal point, we know the value isn't exactly 16 megabytes.

## Large numbers

`pc` uses Perl's bigint so that it can handle numbers bigger than 32 bits:

``````  \$ pc '1<<40'
1,099,511,627,776 1099511627776 0x100,0000,0000 0x10000000000 020000000000000 1TB``````

## Trancendental and trignomentric functions

`pc` gives you access to all the Perl math functions:

``````  \$ pc 'sin(3.141592) ** .5'
1 0x1 0 0.0008084490047 1.772453666531229808666207156690905830337 1``````

## Random Perl code

``````  \$ pc 'ord("a")'
0x61 0141 97 'a'``````

Since `pc` uses Perl's eval you can do arbitrary perl code too. Though frankly the only thing I've ever used is ord().