1 |
## --------------------------------------------------------------------------- |
2 |
## $Id: shortcuts.pm,v 1.5 2003/02/22 17:26:13 joko Exp $ |
3 |
## --------------------------------------------------------------------------- |
4 |
## $Log: shortcuts.pm,v $ |
5 |
## Revision 1.5 2003/02/22 17:26:13 joko |
6 |
## + enhanced unix compatibility fix |
7 |
## |
8 |
## Revision 1.4 2003/02/22 17:19:36 joko |
9 |
## + unix compatibility fix |
10 |
## |
11 |
## Revision 1.3 2003/02/14 14:17:04 joko |
12 |
## - shortened seperator |
13 |
## |
14 |
## Revision 1.2 2003/02/11 05:14:28 joko |
15 |
## + refactored code from libp.pm |
16 |
## |
17 |
## Revision 1.1 2003/02/09 04:49:45 joko |
18 |
## + shortcuts now refactored to this file |
19 |
## |
20 |
## --------------------------------------------------------------------------- |
21 |
|
22 |
|
23 |
package shortcuts; |
24 |
|
25 |
use strict; |
26 |
use warnings; |
27 |
|
28 |
require Exporter; |
29 |
our @ISA = qw( Exporter ); |
30 |
our @EXPORT_OK = qw( |
31 |
strftime |
32 |
now today |
33 |
run_cmd run_cmds |
34 |
get_chomped |
35 |
bool2status |
36 |
); |
37 |
|
38 |
|
39 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main |
40 |
|
41 |
use Data::Dumper; |
42 |
use POSIX qw( strftime ); |
43 |
#use IPC::Run qw( run timeout ); |
44 |
use IPC::Run qw( start pump finish timeout ) ; |
45 |
|
46 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
47 |
# see "perldoc -f localtime" |
48 |
|
49 |
sub now { |
50 |
my $options = shift; |
51 |
my $pattern = "%Y-%m-%d %H:%M:%S"; |
52 |
$pattern = "%Y-%m-%d_%H-%M-%S" if $options->{fs}; |
53 |
my $result = strftime($pattern, localtime); |
54 |
return $result; |
55 |
} |
56 |
|
57 |
sub today { |
58 |
return strftime("%Y-%m-%d", localtime); |
59 |
} |
60 |
|
61 |
sub run_cmd { |
62 |
my $cmd = shift; |
63 |
my $caption = shift; |
64 |
my $options = shift; |
65 |
#$cmd = 'perl ' . $cmd; |
66 |
|
67 |
# report - header |
68 |
my $sep = "-" x 60; |
69 |
print $sep, "\n"; |
70 |
#print " ", $cmd, "\n"; |
71 |
#print " ", " $caption", "\n" if $caption; |
72 |
print " ", $cmd; |
73 |
print " - ", $caption if $caption; |
74 |
print "\n"; |
75 |
print $sep, "\n"; |
76 |
|
77 |
# strip name of executable from full command string |
78 |
$cmd =~ m/(.+?)\s/; |
79 |
my $executable = $1; |
80 |
|
81 |
# for unix: check if executable is in local directory, if so - prefix with './' |
82 |
if (!RUNNING_IN_HELL()) { |
83 |
#if ($cmd !~ m/\//) { |
84 |
if (-e $executable) { |
85 |
$cmd = "./$cmd"; |
86 |
} |
87 |
} |
88 |
|
89 |
# V1 - backticks or qq{} |
90 |
#`$cmd`; |
91 |
#qq{$cmd}; |
92 |
|
93 |
# V2 - via 'system' |
94 |
#system($cmd); |
95 |
|
96 |
# V3 - using IPC::Run (optional) |
97 |
if ($options->{async}) { |
98 |
# FIXME: detect type of program and run with proper application/interpreter |
99 |
# using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here! |
100 |
# => better use absolute path-names only?! |
101 |
$cmd = "perl $cmd"; |
102 |
print "IPC::Run: $cmd", "\n"; |
103 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
104 |
|
105 |
my @cmd = split(' ', $cmd); |
106 |
|
107 |
my $in; my $out; my $err; |
108 |
start \@cmd, timeout(0) or die("IPC::Run could not start '$cmd'."); |
109 |
|
110 |
#$IPC::Run::Timer::timeout = 2000; |
111 |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
112 |
|
113 |
} else { |
114 |
system($cmd); |
115 |
} |
116 |
|
117 |
print "ready.", "\n"; |
118 |
|
119 |
} |
120 |
|
121 |
sub run_cmds { |
122 |
foreach (@_) { |
123 |
run_cmd($_); |
124 |
} |
125 |
} |
126 |
|
127 |
sub get_chomped { |
128 |
my $str = shift; |
129 |
chomp($str); |
130 |
return $str; |
131 |
} |
132 |
|
133 |
sub bool2status { |
134 |
my $bool = shift; |
135 |
return ($bool ? 'ok' : 'failed'); |
136 |
} |
137 |
|
138 |
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
139 |
|
140 |
|
141 |
1; |