#!/usr/bin/env perl use strict; use warnings; use Tie::File; my $DEBUG_FILE = 'pm_11134904_log_progress.log'; tie my(@DEBUG_RECORDS), 'Tie::File', $DEBUG_FILE or die "Can't tie $DEBUG_FILE: $!"; debug("$0 started."); for my $step ('A' .. 'C') { debug("Start step $step"); if ($step eq 'B') { my @paths = 1 .. 5; my $overwrite_last = 0; for my $path (@paths) { sleep 1; my $progress = 100 * $path / @paths; my $msg = "Processing step $step Progress: $progress%"; debug($msg, $overwrite_last++); show_log_to_date(); } } else { debug("Processing step $step ..."); } debug("End step $step"); } debug("$0 completed."); untie @DEBUG_RECORDS; show_log_to_date(); sub debug { my ($msg, $overwrite_last) = @_; my $log_entry = join ' ', "".localtime, $msg; pop @DEBUG_RECORDS if $overwrite_last; push @DEBUG_RECORDS, $log_entry; return; } sub show_log_to_date { print '-' x 40, "\n"; system cat => $DEBUG_FILE; } #### $ ./pm_11134904_log_progress.pl ---------------------------------------- Sun Jul 11 10:20:39 2021 ./pm_11134904_log_progress.pl started. Sun Jul 11 10:20:39 2021 Start step A Sun Jul 11 10:20:39 2021 Processing step A ... Sun Jul 11 10:20:39 2021 End step A Sun Jul 11 10:20:39 2021 Start step B Sun Jul 11 10:20:40 2021 Processing step B Progress: 20% ---------------------------------------- Sun Jul 11 10:20:39 2021 ./pm_11134904_log_progress.pl started. Sun Jul 11 10:20:39 2021 Start step A Sun Jul 11 10:20:39 2021 Processing step A ... Sun Jul 11 10:20:39 2021 End step A Sun Jul 11 10:20:39 2021 Start step B Sun Jul 11 10:20:41 2021 Processing step B Progress: 40% ---------------------------------------- Sun Jul 11 10:20:39 2021 ./pm_11134904_log_progress.pl started. Sun Jul 11 10:20:39 2021 Start step A Sun Jul 11 10:20:39 2021 Processing step A ... Sun Jul 11 10:20:39 2021 End step A Sun Jul 11 10:20:39 2021 Start step B Sun Jul 11 10:20:42 2021 Processing step B Progress: 60% ---------------------------------------- Sun Jul 11 10:20:39 2021 ./pm_11134904_log_progress.pl started. Sun Jul 11 10:20:39 2021 Start step A Sun Jul 11 10:20:39 2021 Processing step A ... Sun Jul 11 10:20:39 2021 End step A Sun Jul 11 10:20:39 2021 Start step B Sun Jul 11 10:20:43 2021 Processing step B Progress: 80% ---------------------------------------- Sun Jul 11 10:20:39 2021 ./pm_11134904_log_progress.pl started. Sun Jul 11 10:20:39 2021 Start step A Sun Jul 11 10:20:39 2021 Processing step A ... Sun Jul 11 10:20:39 2021 End step A Sun Jul 11 10:20:39 2021 Start step B Sun Jul 11 10:20:44 2021 Processing step B Progress: 100% ---------------------------------------- Sun Jul 11 10:20:39 2021 ./pm_11134904_log_progress.pl started. Sun Jul 11 10:20:39 2021 Start step A Sun Jul 11 10:20:39 2021 Processing step A ... Sun Jul 11 10:20:39 2021 End step A Sun Jul 11 10:20:39 2021 Start step B Sun Jul 11 10:20:44 2021 Processing step B Progress: 100% Sun Jul 11 10:20:44 2021 End step B Sun Jul 11 10:20:44 2021 Start step C Sun Jul 11 10:20:44 2021 Processing step C ... Sun Jul 11 10:20:44 2021 End step C Sun Jul 11 10:20:44 2021 ./pm_11134904_log_progress.pl completed.