++ed by:

1 non-PAUSE user.

Jason Leane


Toolbox::Simple - Simplfy some common tasks in Perl


        use Toolbox::Simple qw(lcm is_prime send_mail);
        $num = 7;
        if(is_prime($num)) { print("$num is prime!"); } else { print("$num is not prime."); }
        @nums = (3, 8, 24); 
        $lcm = lcm(@nums);    ### $lcm = 24

        # Send an e-mail message, with the body as a string with embedded newlines.
        $msg[0] = "Hi there Dave!";
        $msg[1] = "Just saying hi.";
        $msg[2] = "See you!";
        $message = join("\n", @msg);  # Join lines with \n's
        # Do the actual sending
                        'smtp.isp.com',         # SMTP server
                        'dave@dave.com',        # Recipient
                        'me@myhost.com',        # Sender
                        'Saying hi',            # Subject
                        $message                        # Message body
        ) or die("Error sending mail!");


Descriptions for each available function follow.


Attempts to calculate a checksum of sorts for its argument, and returns it.

send_mail('server', 'recipient', 'sender', 'subject', $string)

Sends mail using the Net::SMTP module, with the info given. Addresses should be in raw "user@host.com" form, and SMTP server should accept mail from your machine. Message body ($string) is arbitrary-length, with embedded newlines. It is sent all at once to the SMTP server.

Returns 0 on failure.


Returns the hexadecimal MD5 checksum for the specified filename.

Returns checksum on success, 0 on failure.

b64_encode("filename.foo", "filename.b64")

Base64 encodes the file specified in the first argument, putting the result in a file specified by the second argument. If no second argument is given, ".b64" is appended to the input file's name.

Returns 0 on failure.

b64_decode("filename.b64", "filename.txt")

Base64-decodes the file in the first argument, saving the decoded version in the filename specified by the second argument (or the input file with ".out" appended, if no second arg is provided.)

Returns 0 on failure.


Returns your hostname as reported by Sys::Hostname.

Returns 0 on failure.


Returns your IP address ( by running name2ip on he name returned by Sys::Hostname.

Returns 0 on failure.


Returns the argument, rounded to two decimal places (as is done with money). In the example, "12345.67" would be returned.


Returns the argument, with a comma every 3 places, as is common when writing large numbers. For the example, it would return "1,000,000".


Returns the ASCII character corresponding to the given hex number. ("A" in the example.)


Returns the resolved name for the given IP address, or 0 on failure.


Returns the IP address corresponding to the given name, or 0 on failure.


Returns the (number)th number in the Fibonacci sequence.

gcd(num, num, num) / gcf(num, num, num)

Both (identical) functions return the greatest common divisor/factor for the numbers given in their arguments.

lcm(num, num, num)

Returns the lowest common multiple for the numbers in its argument.


Tests a number for primeness. If it is, returns 1. If it isn't prime, returns 0.


Converts given decimal number into hexadecimal. Result in example is '41'.


Converts given hex number into decimal. Result in example is '31'.

dec2bin(decimalnumber, bits)

Converts decimalnumber into a big-endian binary string consisting of bits bits total (bits can be between 4 and 32).


Converts given binary string into decimal. Returns "10" in example.

dec2oct() oct2dec()

Converts given decimal num to octal, and vice versa.


Returns the current time in format "16:20:00".


Returns the date / time as specified by timeformat. Examples of output with different values for timeformat:

        time                    16:20:00                                                (hh:mm:ss)
        date_short              02/22/02                                                (mm/dd/yy)
        date_lf         22/02/02                                                (dd/mm/yy)
        date_long               Friday, February 22, 2002
        weekday                 Friday
        month                   February
        year                    2002

date_short is the American way, date_lf is the rest of the world...


All functions can be exported. Specify which you want using...

        use Toolbox::Simple qw(time_english md5_file);

And only those will be imported.


None that i know about.


Add more useful things as I think of them... Send me suggestions!


Jason Leane (alphamethyl@mac.com)

Copyright 2002 Jason Leane

Thanks to LucyFerr for getting me out of a rut and renewing my enthusiasm for Perl with her own brand of persevereance as she learned Perl for the first time.

"Now quick, what's 0xDEADBEEF in octal?"