Pausing function with Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pausing function with Perl
# 1  
Old 11-26-2008
Pausing function with Perl

Hi all,

I got the request to make a Perl script with the requirements as follow:

1. Make a program that just shows the time, a clock on the format 12:01:24.
2. When someone press key combination Ctrl -p the program should pause, ie the clock will stand still. When the Ctrl - p is pressed the second time the clock will start again.

I am a newbie in Perl & programming, so it's kind of a big job for me. Highly appreciate your support/advice.

Thanks alot.
Milo
# 2  
Old 11-26-2008
Hi Milo,

1) date '+TIME: %H:%M:%S'
2) I think you should try using signals

Regards,
Subin
# 3  
Old 11-26-2008
Hi subin,

Thanks for your reply.

yes, I know signal should be used in this case. For the first presskey to stop clock, but resuming clock after second presskey is still outstanding.

Milo
# 4  
Old 11-27-2008
Below is what I've tried. However, I use ^C to interrupt (don't know how to use ^P) and a select menu to resume couting time.

Code:
#!/usr/bin/perl
# The script is to couting time (show clock) and support Ctrl-C to pause clock
# then choose a key to continue couting or exit.

use strict 'vars';

$SIG{'INT'} = 'Pause';	# use signal handling, if press ^C, will interrupt and call sub Pause.

print "Press Ctrl-C to pause couting time \n";

our ($hour,$min,$sec) = ();
($hour,$min,$sec) = clock (0,0,0); # start couting with at 00:00:00

sub Pause {
  print "\nCaught ^C\n";
  print "Press \"c\" to continue, \"e\" to exit: ";
  while (1) {
    my $input = lc(getc());
    chomp ($input);
    if ($input eq 'c') {
      clock($hour,$min,$sec);
    }
    elsif ($input eq 'e') {
      exit 1;
    }
  }
}

# sub clock is to count time
sub clock {
  our ($hour, $min, $sec) = @_;	# declare as the global variables
  while (1) {
      sleep 1;	# get clock of system for each one second
      $sec++;
      if ($sec == 60) {
	$sec = 0;
	$min++;
	if ($min == 60) {
          $min = 0;
	  $hour++;
	  $hour = 0 if ($hour == 24)
	}
      }
      printf ("%2d:%2d:%2d\n",$hour,$min,$sec);
  }    
  return ($hour,$min,$sec);
}

The output looks like:

Code:
C:\Program Files\Perl\mine>pause_func.pl
Press Ctrl-C to pause couting time
 0: 0: 1
 0: 0: 2
 0: 0: 3

Caught ^C
Press "c" to continue, "e" to exit: c
 0: 0: 4
 0: 0: 5

Caught ^C
Press "c" to continue, "e" to exit: e

C:\Program Files\Perl\mine>

If you know how to have ^P to interrupt counting and other method to get this done, that would be great.

Thanks,
Milo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Pausing a cron job

Hi All, I have a cronjob which runs a script every 5 mins. My problem is before the script is completed the cron job starts again after 5 mins. How can I put this second cron job to wait unless the first script has completed. Could you please help me on that (7 Replies)
Discussion started by: prats_7678
7 Replies

3. Shell Programming and Scripting

Read not prompting/ pausing for input

Heya people, So my script is one of those make-user-scripts, making a bunch of directories in the home folder and copying some files over into the new directories. However, part of the thing my script need to do is check the home folder if a username matches with a directory which already exists.... (6 Replies)
Discussion started by: Cloudy_Cat
6 Replies

4. UNIX for Dummies Questions & Answers

Pausing and resuming process on laptop

Hi, I'm wondering if it's possible to pause a process I'm running in the background, close my laptop (I need to leave the office), and continue the process (when I get home). I've been running a process for a really long time and I don't want to have to start it over. Thanks in advance! (9 Replies)
Discussion started by: ShiGua
9 Replies

5. Shell Programming and Scripting

perl function enquery

Dear all, I find a perl script that contains the following codes. Does anybody know the meaning of codes highlight. ..... @field = parse_csv($file); chomp(@field); ........ ........ sub parse_csv { my $text = shift; my @new = (); push( @new, $+ ) while $text =~ m{... (9 Replies)
Discussion started by: eldonlck
9 Replies

6. Shell Programming and Scripting

Help !! perl open function

Help Please perl Gurus, I am trying to add ungrouped passengers in a group and I creating a script however it fails on first step only I tried all the options it returns following error. syntax error at junki line 4, near "open " Execution of junki aborted due to compilation errors. ... (2 Replies)
Discussion started by: dynamax
2 Replies

7. Shell Programming and Scripting

Pausing between two executions in a shell script

HI, Please help me on this. I have to execute 100 scripts which i have redirected in to a file . I want to pause the script after first execution and once i say enter key word it has to go to next execution. My looks like for RUNFILES in `cat ${SOURCEFILES}/scripts` do echo $RUNFILES; ... (1 Reply)
Discussion started by: srichunduru
1 Replies

8. Shell Programming and Scripting

PERL function problem

I have perl script as follow. ------------------------------------------------------------------------ #! /usr/bin/env perl use strict; sub printLines { print "Inside the function.............\n"; my (@file , $count , $key ) = $_; print $count , $ key ; #... (2 Replies)
Discussion started by: avadhani
2 Replies

9. Shell Programming and Scripting

Pausing a Grep output

I am writing a small script which allows users to grep multiple log files across multiple directories, and often the output produced by the grep statements is quite lengthy. It would be nice if the output to the screen could be "paused" when it reaches a certain length (say, the length of the... (5 Replies)
Discussion started by: mharley
5 Replies

10. Shell Programming and Scripting

pausing a script

hello all, I have a script (Solaris v8 ksh) monitoring an inbound directory for reports. The script will then capture the report based on a set of circumstances. However I am unable to capture a report larger then 2-Gig in size due to program limitations. What I need to do is pause the monitor... (6 Replies)
Discussion started by: gozer13
6 Replies
Login or Register to Ask a Question