diff options
Diffstat (limited to 'MDK/Common/Various.pm')
-rw-r--r-- | MDK/Common/Various.pm | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/MDK/Common/Various.pm b/MDK/Common/Various.pm new file mode 100644 index 0000000..b012343 --- /dev/null +++ b/MDK/Common/Various.pm @@ -0,0 +1,32 @@ +package MDK::Common::Various; + +use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK); +@ISA = qw(Exporter); +@EXPORT_OK = qw(first second top bool to_int to_float bool2text bool2yesno text2bool chomp_ backtrace); +%EXPORT_TAGS = (all => [ @EXPORT_OK ]); + + +sub first { $_[0] } +sub second { $_[1] } +sub top { $_[-1] } + +sub bool { $_[0] ? 1 : 0 } +sub to_int { $_[0] =~ /(\d*)/; $1 } +sub to_float { $_[0] =~ /(\d*(\.\d*)?)/; $1 } +sub bool2text { $_[0] ? "true" : "false" } +sub bool2yesno { $_[0] ? "yes" : "no" } +sub text2bool { my $t = lc($_[0]); $t eq "true" || $t eq "yes" ? 1 : 0 } + +sub chomp_ { my @l = map { my $l = $_; chomp $l; $l } @_; wantarray ? @l : $l[0] } + +sub backtrace { + my $s; + for (my $i = 1; caller($i); $i++) { + my ($package, $file, $line, $func) = caller($i); + $s .= "$func() called from $file:$line\n"; + } + $s; +} + +1; + |