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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.8

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