in reply to Printing to stdout an array of strings and scalar references

Hi Anonymous Monk,

I am not sure if this fits into what you're trying to do, but why not simplify the matter by building the output where and when you need it:

$ perl -e '#!/usr/bin/perl > use warnings; > use strict; > > my $count = 42; > my $completed = 6; > > notify_progress(); > $completed = 7; > notify_progress(); > > sub notify_progress { > my $progress_output = ["\rProcessing: ", $count, " files to proc +ess ; ", $completed, " files completed\n"]; > print @$progress_output; > }' Processing: 42 files to process ; 6 files completed Processing: 42 files to process ; 7 files completed
Or even simplify further the notify_progress subroutine:
$ perl -e '#!/usr/bin/perl > use warnings; > use strict; > > my $count = 42; > my $completed = 6; > > notify_progress(); > $completed = 7; > notify_progress(); > > sub notify_progress { > print "Processing: ", $count, " files to process ; ", $complete +d, " files completed\n"; > }' Processing: 42 files to process ; 6 files completed Processing: 42 files to process ; 7 files completed
And, BTW, do yourself and other monks a favor, take the time to register an account. ;-)

Replies are listed 'Best First'.
Re^2: Printing to stdout an array of strings and scalar references
by Anonymous Monk on Oct 25, 2017 at 02:36 UTC

    Hi Laurent_R

    Thanks for the feedback, I had considered building the entire output within the subroutine, however I have a requirement be able to modify the output based on the command line option chosen, for example on one run I may need the progress indication to be

    Processing: 42 files to process ; 6 files completed
    then on the next run the verbose switch may be selected so I might need
    Processing: 42 files to process ; 2 files excluded ; 6 files completed
    Using the array method I am currently looking at lets me just push the required reporting output into the array as I am processing the CLI options which seemed to me to be more efficient than reprocessing the CLI options every time I need to update the progress indicator.

    Regardless I value the time you have taken to review and make suggestions, Thankyou.

      You are welcome.

      OK, now I understand why you're using an arrayref with references to the counters. Although I guess a simple array with references to the counters would also work.

      An alternative approach would be to build dynamically the output subroutine depending on your input:

      #!/usr/bin/perl use warnings; use strict; my $verbose = shift // 0; my $excluded = 2; my $count = 42; my $completed = 6; my $notify_progress = build_output_subroutine(); $notify_progress->(); $completed = 7; $notify_progress->(); sub build_output_subroutine { my @out; if ($verbose) { return sub { print "Processing: ", $count, " files to proces +s ; ", $excluded, " files excluded; ", $completed, " files completed\ +n" }; } else { return sub { print "Processing: ", $count, " files to process + ; ", $completed, " files completed\n" }; } }
      But, in this context, this probably does not really make things really simpler.