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

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.

Replies are listed 'Best First'.
Re^3: Printing to stdout an array of strings and scalar references
by Laurent_R (Canon) on Oct 25, 2017 at 07:45 UTC
    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.