G'day ovedpo15,

Three reasons why I wouldn't recommend this approach:

Having said that, if you want to proceed with this — perhaps you just want snapshots of the log instead of continual progress updates — you could use the core module Tie::File. Here's an example (with show_log_to_date() simulating snapshots):

#!/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; }

Output from a sample run:

$ ./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.

Some other points regarding the specific code you posted:

— Ken


In reply to Re: Recommended way to display progress by kcott
in thread Recommended way to display progress by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.