Jason Leane > Toolbox > Toolbox::Simple

Download:
Toolbox-0.52.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.52   Source  

NAME ^

Toolbox::Simple - Simplfy some common tasks in Perl

SYNOPSIS ^

        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
        send_mail(
        
                        'smtp.isp.com',         # SMTP server
                        'dave@dave.com',        # Recipient
                        'me@myhost.com',        # Sender
                        'Saying hi',            # Subject
                        $message                        # Message body
                                
        ) or die("Error sending mail!");

DESCRIPTION ^

Descriptions for each available function follow.

c32('string')

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.

md5_file("filename.foo")

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.

my_hostname()

Returns your hostname as reported by Sys::Hostname.

Returns 0 on failure.

my_ip()

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

Returns 0 on failure.

round_money('12345.678')

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

commify_number('1000000')

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".

hex2ascii('41')

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

ip2name('24.82.17.121')

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

name2ip('h24-82-17-121.vc.shawcable.net')

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

fibo(number)

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.

is_prime(num)

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

dec2hex(65)

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

hex2dec(1A)

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).

bin2dec(1010)

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

dec2oct() oct2dec()

Converts given decimal num to octal, and vice versa.

time_now()

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

time_english('timeformat')

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...

EXPORTABLE FUNCTIONS ^

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

        use Toolbox::Simple qw(time_english md5_file);

And only those will be imported.

BUGS ^

None that i know about.

TO DO ^

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

AUTHOR ^

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?"

syntax highlighting: