Files
scripts/Perl Skript template/functions.pm
2024-10-14 00:08:40 +02:00

192 lines
3.9 KiB
Perl

use v5.10;
package functions;
require Exporter;
require Term::ANSITable;
require IO::Prompter;
use vars qw($VERSION @ISA @EXPORT);
$VERSION = 1.00;
@ISA = qw(Exporter);
###############################################################################
@EXPORT = qw/
exit_on_error
p
clrscr
discard_error_msg
remove_doubles
debug
table
table_short
%conf
/;
our %conf;
read_config("config");
# Conf Format
#Skalar
#Variable:Wert
#Array
#@Variable:Wert1;Wert2;Wert3
#Hash
#%Variable:Key1#Wert1;Key2#Wert2;Key3#Wert3
#print $conf{'DB_TYPE'} ."\n";
#print $conf{'DB_HOST'} ."\n";
#foreach ($conf{'@TAGE'}) {
# print "$_\n";
#}
#foreach my $k (keys $conf{'%TAGE'}) {
# print "k $k v $conf{'%TAGE'}{$k}\n";
#}
sub read_config {
my $configfile=shift;
my $vd=':'; # trennt var von wert
my $ad=';'; # trennt werte im array bzw. wertpaare im hash
my $hd='#'; # trennt wert von key im hash
open CF,"$configfile";
foreach (<CF>) {
chomp;
if ($_ =~ /require/) {
my ($f) = $_ =~ /require "(.*)"/;
read_config($f);
}
else {
my ($k,$v) = split /$vd/,$_; # trennung zwischen var-name und werten
if ($k =~ /^@/) { # array variable
my @val=split /$ad/,$v;
foreach (@val) {
push @{$conf{"$k"}},$_;
}
}
if ($k =~ /^%/) { # hash variable
my @val=split /$ad/,$v;
foreach my $vp (@val) {
my ($k1,$v1) = split /$hd/,$vp;
$conf{"$k"}{"$k1"}=$v1;
}
}
else {
$conf{"$k"} = $v;
}
}
}
}
close CF;
######## Allgemeine Funktionen und Beispielnutzung
sub exit_on_error {
# call: exit_on_error("text");
my $m=shift;
print "\n\n ### $m\n\n\n";
exit 1;
}
sub p {
# call p("text");
my $m = shift;
printf "$m\n";
}
sub clrscr {
# call clrscr(); Leert Bildschirminhalt
for (0..50) {
print "\n";
}
}
sub discard_error_msg {
my $d=shift;
if ($d==1) {
open STDERR, '>/dev/null';
}
else {
close STDERR;
}
}
sub remove_doubles {
# call my @arr=qw /a2 a2 a2 a2 a2 a2 a2 b3 b3 b3 b3/;
# @arr=remove_doubles(\@arr);
my $o=shift;
my @o=@{$o};
my %h;
foreach (@o) {
next if ($h{$_});
$h{$_}=1;
}
@o=();
foreach (keys %h) {
push @o, $_;
}
return @o;
}
sub debug {
printf "%-10s:%s\n", $_[0], $_[1] if ($_[2] == 1);
}
########
## my (@header,@rows,@row);
## foreach my $line (@res) {
## my $status = ""; #
## $status = "I" if (${$line}[1] == 1);
## @row = [ "${$line}[0]", "$status", "${$line}[2]", "${$line}[3]" ];
## push @rows, @row;
## }
## @header=[ "ID", "A", "Name", "Beschreibung" ];
## table_short (\@header, \@rows);
sub table (@) {
my ($head, $rows) = @_;
my $at = Term::ANSITable->new( columns => @{$head} );
foreach my $row(@{$rows}) {
$at = $at->add_row($row);
}
#$at->{_table}->{cell_vpad} = 0;
$at->{_table}->{show_row_separator} = 1;
$at->refresh_table->draw();
}
sub table_short (@) {
my ($head, $rows) = @_;
my $at = Term::ANSITable->new( columns => @{$head} );
foreach my $row(@{$rows}) {
$at = $at->add_row($row);
}
#$at->{_table}->{cell_vpad} = 0;
$at->{_table}->{show_row_separator} = 0;
$at->refresh_table->draw();
}
# if ( $in =~ /^s/ and $in =~ /[tps]$/ ) {
# given (prompt -k1, "[A]ctive, (I)nactive, or al(L):", -keyletters) {
# when (/I/i) { $act3="inactive"; }
# when (/L/i) { $act3="all"; }
# default { $act3="active"; }
# }
# }
# my $anz = prompt ('Anzahl der Aktivitäten (0 für alle):', -integer, -must => { 'be in range' => [0..999] });
# $in = prompt("\nAktivität angeben:", -guarantee=>[@pars1]);
# $pw = prompt('Password:', -echo => '*');
# $animal = prompt -guarantee=>['cat','dog','cow'];
# $ok = prompt('Fertig:', -yn1s);
1;