/[cvs]/nfo/perl/libs/shortcuts.pm
ViewVC logotype

Contents of /nfo/perl/libs/shortcuts.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations)
Fri Apr 4 17:31:59 2003 UTC (21 years, 1 month ago) by joko
Branch: MAIN
Changes since 1.7: +34 -4 lines
+ sub make_guid

1 ## ---------------------------------------------------------------------------
2 ## $Id: shortcuts.pm,v 1.7 2003/03/29 07:24:10 joko Exp $
3 ## ---------------------------------------------------------------------------
4 ## $Log: shortcuts.pm,v $
5 ## Revision 1.7 2003/03/29 07:24:10 joko
6 ## enhanced 'run_cmd': now tries to execute program with appropriate application (e.g. 'cmd.exe' or 'perl')
7 ##
8 ## Revision 1.6 2003/03/28 06:58:06 joko
9 ## new: 'run_cmd' now asynchronous! (via IPC::Run...)
10 ##
11 ## Revision 1.5 2003/02/22 17:26:13 joko
12 ## + enhanced unix compatibility fix
13 ##
14 ## Revision 1.4 2003/02/22 17:19:36 joko
15 ## + unix compatibility fix
16 ##
17 ## Revision 1.3 2003/02/14 14:17:04 joko
18 ## - shortened seperator
19 ##
20 ## Revision 1.2 2003/02/11 05:14:28 joko
21 ## + refactored code from libp.pm
22 ##
23 ## Revision 1.1 2003/02/09 04:49:45 joko
24 ## + shortcuts now refactored to this file
25 ##
26 ## ---------------------------------------------------------------------------
27
28
29 package shortcuts;
30
31 use strict;
32 use warnings;
33
34 require Exporter;
35 our @ISA = qw( Exporter );
36 our @EXPORT_OK = qw(
37 strftime
38 now today
39 run_cmd run_cmds
40 get_chomped
41 bool2status
42 make_guid
43 );
44
45
46 use Data::Dumper;
47 use POSIX qw( strftime );
48 #use IPC::Run qw( run timeout );
49 use IPC::Run qw( start pump finish timeout ) ;
50 use Carp;
51
52
53
54 # $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
55 # see "perldoc -f localtime"
56 sub now {
57 my $options = shift;
58 my $pattern = "%Y-%m-%d %H:%M:%S";
59 $pattern = "%Y-%m-%d_%H-%M-%S" if $options->{fs};
60 my $result = strftime($pattern, localtime);
61 return $result;
62 }
63
64 sub today {
65 return strftime("%Y-%m-%d", localtime);
66 }
67
68 sub run_cmd {
69 my $cmd = shift;
70 my $caption = shift;
71 my $options = shift;
72 #$cmd = 'perl ' . $cmd;
73
74 # report - header
75 my $sep = "-" x 60;
76 print $sep, "\n";
77 #print " ", $cmd, "\n";
78 #print " ", " $caption", "\n" if $caption;
79 print " ", $cmd;
80 print " - ", $caption if $caption;
81 print "\n";
82 print $sep, "\n";
83
84 # strip name of executable from full command string
85 $cmd =~ m/(.+?)\s/;
86 my $executable = $1;
87
88 # for unix: check if executable is in local directory, if so - prefix with './'
89 if (!RUNNING_IN_HELL()) {
90 #if ($cmd !~ m/\//) {
91 if (-e $executable) {
92 $cmd = "./$cmd";
93 }
94 }
95
96 # V1 - backticks or qq{}
97 #`$cmd`;
98 #qq{$cmd};
99
100 # V2 - via 'system'
101 #system($cmd);
102
103 # V3 - using IPC::Run (optional)
104 if ($options->{async}) {
105 # FIXME: detect type of program and run with proper application/interpreter
106 # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!
107 # => better use absolute path-names only?!
108 my $application = '';
109 if (RUNNING_IN_HELL()) {
110 $application = 'cmd.exe /C';
111 }
112
113 if ($cmd =~ m/\w+\.pl\s*.*/) {
114 $application = 'perl';
115 }
116
117 $cmd = "$application $cmd" if $application;
118
119 print "IPC::Run: $cmd", "\n";
120 #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
121
122 my @cmd = split(' ', $cmd);
123
124 my $in; my $out; my $err;
125 start \@cmd, timeout(0) or die("IPC::Run could not start '$cmd'.");
126
127 #$IPC::Run::Timer::timeout = 2000;
128 #start $cmd or die("IPC::Run could not start '$cmd'.");
129
130 } else {
131 system($cmd);
132 }
133
134 print "ready.", "\n";
135
136 }
137
138 sub run_cmds {
139 foreach (@_) {
140 run_cmd($_);
141 }
142 }
143
144 sub get_chomped {
145 my $str = shift;
146 chomp($str);
147 return $str;
148 }
149
150 sub bool2status {
151 my $bool = shift;
152 return ($bool ? 'ok' : 'failed');
153 }
154
155 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
156
157 # create global unique identifers using Data::UUID
158 # if updating this code, please also modify Tangram::Storage::make_guid
159 sub make_guid
160 {
161 my $self = shift;
162
163 my $guid;
164
165 # try to use Data::UUID first ...
166 eval("use Data::UUID;");
167 if (!$@) {
168 my $ug = Data::UUID->new();
169 $guid = $ug->create_str();
170
171 # ... if this fails, try to fallback to Data::UUID::PurePerl instead ...
172 } else {
173 eval("use Data::UUID::PurePerl;");
174 if (!$@) {
175 $guid = Data::UUID::PurePerl::generate_id();
176 } else {
177 croak "couldn't create globally unique identifier";
178 }
179 }
180
181 return $guid;
182 }
183
184 1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed