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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide 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 joko 1.1 ## ---------------------------------------------------------------------------
2 joko 1.8 ## $Id: shortcuts.pm,v 1.7 2003/03/29 07:24:10 joko Exp $
3 joko 1.1 ## ---------------------------------------------------------------------------
4     ## $Log: shortcuts.pm,v $
5 joko 1.8 ## 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 joko 1.7 ## Revision 1.6 2003/03/28 06:58:06 joko
9     ## new: 'run_cmd' now asynchronous! (via IPC::Run...)
10     ##
11 joko 1.6 ## Revision 1.5 2003/02/22 17:26:13 joko
12     ## + enhanced unix compatibility fix
13     ##
14 joko 1.5 ## Revision 1.4 2003/02/22 17:19:36 joko
15     ## + unix compatibility fix
16     ##
17 joko 1.4 ## Revision 1.3 2003/02/14 14:17:04 joko
18     ## - shortened seperator
19     ##
20 joko 1.3 ## Revision 1.2 2003/02/11 05:14:28 joko
21     ## + refactored code from libp.pm
22     ##
23 joko 1.2 ## Revision 1.1 2003/02/09 04:49:45 joko
24     ## + shortcuts now refactored to this file
25     ##
26 joko 1.1 ## ---------------------------------------------------------------------------
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 joko 1.2 bool2status
42 joko 1.8 make_guid
43 joko 1.1 );
44    
45    
46     use Data::Dumper;
47 joko 1.6 use POSIX qw( strftime );
48     #use IPC::Run qw( run timeout );
49     use IPC::Run qw( start pump finish timeout ) ;
50 joko 1.8 use Carp;
51    
52    
53 joko 1.1
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 joko 1.6 my $options = shift;
72 joko 1.1 #$cmd = 'perl ' . $cmd;
73 joko 1.6
74     # report - header
75 joko 1.3 my $sep = "-" x 60;
76 joko 1.1 print $sep, "\n";
77 joko 1.6 #print " ", $cmd, "\n";
78     #print " ", " $caption", "\n" if $caption;
79     print " ", $cmd;
80     print " - ", $caption if $caption;
81     print "\n";
82 joko 1.1 print $sep, "\n";
83 joko 1.4
84 joko 1.5 # 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 joko 1.4 if (!RUNNING_IN_HELL()) {
90 joko 1.5 #if ($cmd !~ m/\//) {
91     if (-e $executable) {
92 joko 1.4 $cmd = "./$cmd";
93     }
94     }
95    
96 joko 1.6 # V1 - backticks or qq{}
97 joko 1.1 #`$cmd`;
98 joko 1.6 #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 joko 1.7 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 joko 1.6 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 joko 1.1 print "ready.", "\n";
135 joko 1.6
136 joko 1.1 }
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 joko 1.2 }
149    
150     sub bool2status {
151     my $bool = shift;
152     return ($bool ? 'ok' : 'failed');
153 joko 1.1 }
154 joko 1.4
155     sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
156    
157 joko 1.8 # 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 joko 1.1
184     1;

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