
EasyTool - The Library of Perl Functions in Common Usage

use EasyTool;
if(defined(&EasyTool::foo)){
print "lib is included";
}else{
print "lib is not included";
}
print EasyTool::is_int(2147483647); #true
print EasyTool::is_int(-2147483648); #true
print EasyTool::is_int(2147483648); #false
print EasyTool::is_id(4294967295); #true
print EasyTool::is_id(4294967296); #false
print EasyTool::is_email("xxx.abc@test.com"); #true
print EasyTool::trim(" \t test\n "); #test
print EasyTool::in('a', {'a' => 1, 'b' => 2}); #true
print EasyTool::in(undef, 1, undef); #true
print EasyTool::ifnull(undef, 1); #1
print EasyTool::read_file("file.in");
print EasyTool::write_file("file.out", "string");
print EasyTool::append_file("file.out", "string");
print EasyTool::delete_file("file.out");
$ra_array = EasyTool::csv_2_array("a.csv");
print EasyTool::array_2_csv("a.csv", $ra_array);
print EasyTool::md5_hex("test"); #'098f6bcd4621d373cade4e832627b4f6'
print EasyTool::crc32("test"); #3632233996
$str = &EasyTool::encode_hex("hello");
print EasyTool::decode_hex($str); #hello
$str = &EasyTool::encode_base64("hello");
print EasyTool::decode_base64($str); #hello
$str = &EasyTool::url_encode('<&%$/ \|=+_]{>@^');
print EasyTool::url_decode($str); #<&%$/ \|=+_]{>@^
$str = &EasyTool::html_encode(";<><\"");
print EasyTool::html_decode($str); #;<><"
print EasyTool::qquote('\n'); #"\\n"
print EasyTool::qquote_bin('\n'); #"\\n"
print EasyTool::dump(['1', {'a' => '1', 'b' => '2'}, undef]); #[1, {"a" => 1, "b" => 2}, ()]
print EasyTool::test_var(); #undef
print EasyTool::test_var(1); #1
print EasyTool::test_var(); #1
print EasyTool::time_2_str('1983-03-07 01:02:03','%yyyy-%MM-%%dd');
print EasyTool::time_2_str('1983-03-07 01:02:03');
print EasyTool::time_2_str('1983-03-07');
print EasyTool::time_2_str('2004-08-28T08:06:00');
print EasyTool::time_2_str('946656000');
print EasyTool::time_2_str(' 1983-03-07 ');
print EasyTool::time_2_str('1983-03-07T01:02:03');
print EasyTool::is_time('1983-03-07 01:02:03');
print EasyTool::time_2_timestamp('1983-03-07 01:02:03');
print EasyTool::hash_2_timestamp({year=>1983,month=>3,day=>7,hour=>1,min=>2,sec=>3});
$rh_time=EasyTool::time_2_hash('1983-03-07 01:02:03'); #{year=>1983,month=>3,day=>7,hour=>1,min=>2,sec=>3}
print EasyTool::now();
print EasyTool::time();
print EasyTool::datetime_now();
print EasyTool::date_now();
print EasyTool::day_of_month(2000,2); #29
print EasyTool::day_of_week('2006-07-02'); #0
print EasyTool::timestamp_set('1983-03-07 01:02:03',{year=>1984,month=>5,day=>10,hour=>5,min=>7,sec=>9});#maybe 453013629
print EasyTool::datetime_set('1983-03-07 01:02:03',{year=>1984,day=>10,min=>7});#'1984-03-10 01:07:03'
print EasyTool::date_set('1983-03-07 01:02:03'',{month=>5,hour=>5,sec=>9});#'1983-05-07'
print EasyTool::timestamp_add('1983-03-07 01:02:03',{year=>1,month=>2,day=>3,hour=>4,min=>5,sec=>6});#maybe 453013629
$datetime=EasyTool::datetime_add('1983-03-07 01:02:03',{year=>1,day=>3,min=>5});#'1984-03-10 01:07:03'
$date=EasyTool::date_add('1983-03-07 01:02:03',{month=>2,hour=>4,sec=>6});#'1983-05-07'
The synopsis above only lists the major methods and parameters.

The EasyTool module aims to provide a easy to use, easy to port function set
you can copy and paste some function to embed into your code as easy as possiable youc can also make some modification on function as you need
support time from 1971 to 2037 if you want more function,please use EasyDateTime the time zone used in these function is server local time zone
the 'time' in function name means time_str, please read the description of $time_str
$str: $str is a string
$email: $email is a string as be accept as a email address
$file_path: $file_path is the path of file you want to operate
$bytes: $bytes is the content to write or append
$byte_count: $byte_count is the length of $bytes
$delete_num: $delete_num is the number of files be deleted
$ra_array: an array represent the csv content
$time_str: $time_str is the string as be accept as a time
Samples can be accepted
'2004-08-28 08:06:00' ' 2004-08-28 08:06:00 '
'2004-08-28T08:06:00' '2004/08/28 08:06:00'
'2004.08.28 08:06:00' '2004-08-28 08.06.00'
'04-8-28 8:6:0' '2004-08-28' '08:06:00'
'946656000'
Which string can be accepted?
rule 0: Unix Timestamp, an int represent seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) can be accepted
rule 1: there can be some blank in the begin or end of string e.g. ' 2004-08-28 08:06:00 '
rule 2: date can be separate by . / or - e.g. '2004/08/28 08:06:00'
rule 3: time can be separate by . or : e.g. '2004-08-28 08.06.00'
rule 4: date and time can be join by white space or 'T' e.g. '2004-08-28T08:06:00'
rule 5: can be (date and time) or (only date) or (only time) e.g. '2004-08-28' or '08:06:00'
rule 6: year can be 2 digits or 4 digits,other field can be 2 digits or 1 digit e.g. '04-8-28 8:6:0'
rule 7: if only the date be set then the time will be set to 00:00:00
if only the time be set then the date will be set to 2000-01-01
$timestamp : unix timestamp, an integer like 946656000
$datetime : date time string, a string, like '2004-08-28 08:06:00'
$date : date string, a string like, like '2004-08-28'
$rh_time : a hash represent a time
$rh_time is a struct like {year=>2000,month=>1,day=>1,hour=>0,min=>0,sec=>0}
if some item in $rh_time is not set ,use default value instead
default values: year=>2000,month=>1,day=>1,hour=>0,min=>0,sec=>0
$rh_offset : a hash represent the offset in two times
$rh_offset is a struct like {year=>0,month=>0,day=>0,hour=>0,min=>0,sec=>0}
if some item in $rh_offset is not set ,use zero instead, integer can be negative
one month: {month=>1}
one day : {day=>1}
one month and one day: {month=>1,day=>1}
when you add a time with $rh_offset such as {year=>0,month=>0,day=>0,hour=>0,min=>0,sec=>0}, it will add second first,then
miniute, hour, day, month, year
$template option:
#===FORMAT
#%datetime return string like '2004-08-28 08:06:00'
#%date return string like '2004-08-28'
#%timestamp return unix timestamp
#===YEAR
#%yyyy A full numeric representation of a year, 4 digits(2004)
#%yy A two digit representation of a year(04)
#===MONTH
#%MM Numeric representation of a month, with leading zeros (01..12)
#%M Numeric representation of a month, without leading zeros (1..12)
#===DAY
#%dd Day of the month, 2 digits with leading zeros (01..31)
#%d Day of the month without leading zeros (1..31)
#===HOUR
#%h12 12-hour format of an hour without leading zeros (1..12)
#%h 24-hour format of an hour without leading zeros (0..23)
#%hh12 12-hour format of an hour with leading zeros (01..12)
#%hh 24-hour format of an hour with leading zeros (00..23)
#%ap a Lowercase Ante meridiem and Post meridiem (am or pm)
#%AP Uppercase Ante meridiem and Post meridiem (AM or PM)
#===MINUTE
#%mm Minutes with leading zeros (00..59)
#%m Minutes without leading zeros (0..59)
#===SECOND
#%ss Seconds, with leading zeros (00..59)
#%s Seconds, without leading zeros (0..59)
$bool: 1 for true and '' for false
AM and PM - What is Noon and Midnight? AM and PM start immediately after Midnight and Noon (Midday) respectively. This means that 00:00 AM or 00:00 PM (or 12:00 AM and 12:00 PM) have no meaning. Every day starts precisely at midnight and AM starts immediately after that point in time e.g. 00:00:01 AM (see also leap seconds) To avoid confusion timetables, when scheduling around midnight, prefer to use either 23:59 or 00:01 to avoid confusion as to which day is being referred to. It is after Noon that PM starts e.g. 00:00:01 PM (12:00:01)

if(defined(&EasyTool::foo)){
print "lib is included";
}else{
print "lib is not included";
}
&EasyTool::is_int($str); &EasyTool::is_int($str, $min); &EasyTool::is_int($str, $min, $max); default $max is 2147483648, default min is -2147483648
&EasyTool::is_id($id);
&EasyTool::is_email($email);
$str = &EasyTool::trim($str);
&EasyTool::in($word,$word1,$word2,..); &EasyTool::in($word,$rh); $word can be undef
&EasyTool::ifnull($scalar1,$scalar2)
$bytes=&EasyTool::read_file($file_path)
$byte_count=&EasyTool::write_file($file_path,$bytes)
$byte_count=&EasyTool::append_file($file_path,$bytes)
$delete_num=&EasyTool::delete_file($file_path)
$ra_array=&EasyTool::csv_2_array($file_path)
&EasyTool::array_2_csv($file_path, $ra_array)
$md5 = &EasyTool::md5_hex($str)
$crc = &EasyTool::crc32($str)
$str = &EasyTool::encode_hex($str) $str = &EasyTool::decode_hex($str)
Every 3*8bit will be convert into 4*6bit, then add 2bit 0 before every 6bit
11010101 11000101 00110011 -> 110101 011100 010100 110011
-> 00110101 00011100 00010100 00110011
then every byte convert with the table below:
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 0 | A | 8 | I |16 | Q |24 | Y |32 | g |40 | o |48 | w |56 | 4 |pad| = |
| 1 | B | 9 | J |17 | R |25 | Z |33 | h |41 | p |49 | x |57 | 5 | | |
| 2 | C |10 | K |18 | S |26 | a |34 | i |42 | q |50 | y |58 | 6 | | |
| 3 | D |11 | L |19 | T |27 | b |35 | j |43 | r |51 | z |59 | 7 | | |
| 4 | E |12 | M |20 | U |28 | c |36 | k |44 | s |52 | 0 |60 | 8 | | |
| 5 | F |13 | N |21 | V |29 | d |37 | l |45 | t |53 | 1 |61 | 9 | | |
| 6 | G |14 | O |22 | W |30 | e |38 | m |46 | u |54 | 2 |62 | + | | |
| 7 | H |15 | P |23 | X |31 | f |39 | n |47 | v |55 | 3 |63 | / | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
$str = &EasyTool::encode_base64($str)
$str = &EasyTool::decode_base64($str)
+------------------------+---------------+ | 0-9 A-Z a-z _.!~*'()- | Not changed | | space | + | | others | %xx(xx is HEX)| +------------------------+---------------+ $str = &EasyTool::url_encode($str); $str = &EasyTool::url_decode($str);
+---+-------+ | > | > | | < | < | | " | "| | & | & | | | | +---+-------+ $str = &EasyTool::html_encode($str); $str = &EasyTool::html_decode($str);
$text =~ s/\&/\&/g;
$text =~ s/"/\"/g;
$text =~ s/ / \ /g;
$text =~ s/</\</g;
$text =~ s/>/\>/g;
$text =~ s/[\a\f\e\0\r]//isg;
$text =~ s/document.cookie/documents\&\#46\;cookie/isg;
$text =~ s/'/\&\#039\;/g;
$text =~ s/\$/\&\#36;/isg;
$text =~ s|\n\n|<p></p>|g;
$text =~ s|\n|<br />|g;
$text =~ s/\t/ \ \ /g;
+---------------+--------------------+
| & | & |
| " | " |
| space | |
| < | < |
| > | > |
| \a\f\e\0\r | |
|document.cookie|documents.cookie|
| ' | ' |
| $ | $ |
| \n\n | <p></p> |
| \n | <br /> |
| \t | \ \ |
+---------------+--------------------+
$html = &EasyTool::text_2_html($str);
$str = &EasyTool::qquote($str); $str = &EasyTool::qquote_bin($str);
&EasyTool::dump($data);
$var = &EasyTool::test_var(); &EasyTool::test_var($var);
$format_str=EasyTool::time_2_str($time_str[,$template]) time_2_str($time_str) return str such as '2000-01-01 00:00:00' time_2_str($time_str,'%yyyy-%MM-%dd') return str such as '2000-01-01'
$bool=EasyTool::is_time($time_str)
$timestamp=EasyTool::time_2_timestamp($time_str)
$timestamp=EasyTool::hash_2_timestamp($rh_time)
$rh_time=EasyTool::time_2_hash($time_str)
$timestamp=EasyTool::now(); $timestamp=EasyTool::time(); $datetime=EasyTool::datetime_now(); $date=EasyTool::date_now();
$day_count=day_of_month($year,$month)
$day_count=day_of_week($time_str)
$timestamp=EasyTool::timestamp_set($time_str,$rh_time); $date=EasyTool::date_set($time_str,$rh_time); $datetime=EasyTool::datetime_set($time_str,$rh_time)
$timestamp=EasyTool::timestamp_add($time_str,$rh_offset); $datetime=EasyTool::datetime_add($time_str,$rh_offset); $date=EasyTool::date_add($time_str,$rh_offset)

The EasyTool module is Copyright (c) 2003-2005 QIAN YU. All rights reserved.
You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.