T
Tore Aursand
Hi!
Please excuse my English. It's only my secondary language.
Over the time as a Perl programmer, I've gathered small bits of code here
and there. The last few weeks I've tried to structure all this code and
put in a module which I - so far - call 'utils.pm'. Thus, in my scripts,
I tend to begin like this:
#!/usr/bin/perl
#
use strict;
use warnings;
use utils;
The name could - of course - have been more propriate (if possible), but
to me 'utils' seems like a good one.
The main question is: Although I know that the Perl core should stick to
a minimum, why isn't a module like this included? It covers most of the
traps that newbies encounters, and it offers experienced programmers to be
even more lazy when programming.
Information about the module's functions:
STRINGS
ltrim( $value ) - Removes leading whitespace from a string.
rtrim( $value ) - Removes trailing whitespace from a string.
trim( $value ) - Combines ltrim() and rtrim().
squish( $value ) - Removes (all the) whitespace from a string.
split_csv( $value ) - Easy CSV splitting. (*)
random_string( $length ) - Generates a random string $length
characters long.
CASTING
as_string( $value, [$default] ) - Always returns a defined value,
optionally $default if $value
isn't defined.
as_int( $value ) - Always returns $value as an integer.
as_decimal( $value, [$decimals] ) - Always returns $value as a
decimal number with $decimals
numbers after the decimal point.
as_boolean( $value ) - Always returns $value as a boolena value (ie.
TRUE/1 or FALSE/0).
as_date( $value ) - Always returns $value as a date (YYYY-MM-DD).
as_time( $value ) - Always returns $value as a time (HH:MM:SS).
as_datetime( $value ) - Always returns $value as a datetime (which
means combining as_date() and as_time()).
VALIDATION
Each of the CASTING functions also have a is_* function, which returns
TRUE/1 or FALSE/0 depending on wether the input argument conforms to
the datatype.
NUMBERS
round( $value ) - Rounds a number to the nearest integer.
random_number( $min, $max ) - Returns a random number in the range
$min to $max.
format_number( $value, $separator ) - Formats a number with a given
separator; 1234 becomes 1,234.
ARRAYS
unique( $arrayref ) - Returns only the unique elements in $array.
intersection( $arrayref1, $arrayref2 ) - Computes the intersection
of two array references.
union( $arrayref1, $arrayref2 ) - Computes the union of two array
references.
shuffle( $arrayref ) - Returns the elements shuffled randomly.
DATES
now_year()
now_month()
now_day() - These three returns current the year, month and day.
now_date() - Combines the three above.
now_hour()
now_minute()
now_seconds() - These three returns the current hour, minute and
second.
now_time() - Combines the three above.
now() - Combines now_date() and now_time(); 'YYYY-MM-DD hh:mm:ss'.
is_leap_year( $year ) - Returns TRUE/1 if $year is a leap year.
day_of_week( $date ) - Returns the day of the week for $date.
day_of_year( $date ) - Returns the day of the year for $date.
days_in_month( $year, $month ) - Returns the number of days in the
given year/month.
days_in_year( $year ) - Returns 365 or 366 depending on wether the
year is a leap year or not.
(*) These methods could possibly generate warnings/info about standalone
modules which one should preferrably use.
Well. These are the majority of the functions I've gathered. There are a
few more, but never mind them for now. All the functions are written in
pure Perl, so no modules - of course - or "anything else" is required.
I find these functions very valuable when programming. Whenever I need to
check if a date from the user is valid, I just:
unless ( is_date($date) ) {
# Error
}
I could also combine them. Let's say that the user has misunderstood the
"input the current date" question and also entered the current time;
my $date = '2004-02-11 12:34:56'; # example input
$date = as_date( $date ); # $date is now '2004-02-11'
unless ( is_date($date) ) {
# Error
}
Stupid examples, but...
This is a RFC - Request For Comment - so I want to hear what you all
thing? Should this be a "swiss army" module in the Perl core? I think
so.
Just my 0.5 cents.
Please excuse my English. It's only my secondary language.
Over the time as a Perl programmer, I've gathered small bits of code here
and there. The last few weeks I've tried to structure all this code and
put in a module which I - so far - call 'utils.pm'. Thus, in my scripts,
I tend to begin like this:
#!/usr/bin/perl
#
use strict;
use warnings;
use utils;
The name could - of course - have been more propriate (if possible), but
to me 'utils' seems like a good one.
The main question is: Although I know that the Perl core should stick to
a minimum, why isn't a module like this included? It covers most of the
traps that newbies encounters, and it offers experienced programmers to be
even more lazy when programming.
Information about the module's functions:
STRINGS
ltrim( $value ) - Removes leading whitespace from a string.
rtrim( $value ) - Removes trailing whitespace from a string.
trim( $value ) - Combines ltrim() and rtrim().
squish( $value ) - Removes (all the) whitespace from a string.
split_csv( $value ) - Easy CSV splitting. (*)
random_string( $length ) - Generates a random string $length
characters long.
CASTING
as_string( $value, [$default] ) - Always returns a defined value,
optionally $default if $value
isn't defined.
as_int( $value ) - Always returns $value as an integer.
as_decimal( $value, [$decimals] ) - Always returns $value as a
decimal number with $decimals
numbers after the decimal point.
as_boolean( $value ) - Always returns $value as a boolena value (ie.
TRUE/1 or FALSE/0).
as_date( $value ) - Always returns $value as a date (YYYY-MM-DD).
as_time( $value ) - Always returns $value as a time (HH:MM:SS).
as_datetime( $value ) - Always returns $value as a datetime (which
means combining as_date() and as_time()).
VALIDATION
Each of the CASTING functions also have a is_* function, which returns
TRUE/1 or FALSE/0 depending on wether the input argument conforms to
the datatype.
NUMBERS
round( $value ) - Rounds a number to the nearest integer.
random_number( $min, $max ) - Returns a random number in the range
$min to $max.
format_number( $value, $separator ) - Formats a number with a given
separator; 1234 becomes 1,234.
ARRAYS
unique( $arrayref ) - Returns only the unique elements in $array.
intersection( $arrayref1, $arrayref2 ) - Computes the intersection
of two array references.
union( $arrayref1, $arrayref2 ) - Computes the union of two array
references.
shuffle( $arrayref ) - Returns the elements shuffled randomly.
DATES
now_year()
now_month()
now_day() - These three returns current the year, month and day.
now_date() - Combines the three above.
now_hour()
now_minute()
now_seconds() - These three returns the current hour, minute and
second.
now_time() - Combines the three above.
now() - Combines now_date() and now_time(); 'YYYY-MM-DD hh:mm:ss'.
is_leap_year( $year ) - Returns TRUE/1 if $year is a leap year.
day_of_week( $date ) - Returns the day of the week for $date.
day_of_year( $date ) - Returns the day of the year for $date.
days_in_month( $year, $month ) - Returns the number of days in the
given year/month.
days_in_year( $year ) - Returns 365 or 366 depending on wether the
year is a leap year or not.
(*) These methods could possibly generate warnings/info about standalone
modules which one should preferrably use.
Well. These are the majority of the functions I've gathered. There are a
few more, but never mind them for now. All the functions are written in
pure Perl, so no modules - of course - or "anything else" is required.
I find these functions very valuable when programming. Whenever I need to
check if a date from the user is valid, I just:
unless ( is_date($date) ) {
# Error
}
I could also combine them. Let's say that the user has misunderstood the
"input the current date" question and also entered the current time;
my $date = '2004-02-11 12:34:56'; # example input
$date = as_date( $date ); # $date is now '2004-02-11'
unless ( is_date($date) ) {
# Error
}
Stupid examples, but...
This is a RFC - Request For Comment - so I want to hear what you all
thing? Should this be a "swiss army" module in the Perl core? I think
so.
Just my 0.5 cents.