First, add use strict; and use warnings; to the top of every script you write. It's a free error-checker and it's daft to ignore it. You have lots of uninitialized variables, you are not doing error checking on your function calls, and you are using barewords in the code. You need to fix all that.

I guess system(clear); and break; are supposed to stop the terminal going wonky; you probably need to use another Term::* module. Also 'clear' and 'break' cannot be unquoted:

Unquoted string "clear" may clash with future reserved word at 1137927 +.pl line 35. Bareword "break" not allowed while "strict subs" in use at 1137927.pl +line 57. Bareword "clear" not allowed while "strict subs" in use at 1137927.pl +line 35.

But the problem you originally asked about is caused by having copied code from the docs for Term::StatusBar:

sub doSomething { $status->setItems($_[0]); for (1..$_[0]){ sleep 1; $status->update; ## Will call $status->start() if needed } }

This is a subroutine that is called earlier in the example like this:

doSomething(20);

'20' is passed to the sub as an argument. Perl creates the special array @_ to hold the arguments passed to a sub. So in the example $_[0] is the first element in the array, i.e. the first argument passed, i.e. '20'. So the code says "sleep for one second and repeat once for each number between 1 and 20, then move on (and update the status bar status)".

Your code has no subroutine and there is no value in @_ or $_[0], so that's not going to work. if you had use warnings; Perl would have told you with the warning: Use of uninitialized value in foreach loop entry at foo.pl, line 52 ... and you could have been led to the problem.

I got rid of the for loop so that part of your code looks like:

if ($i % $j == 0) { $is_prime = 0; $status->setItems($i); sleep 1; }

This makes the bar advance, but there's more to do to get your program running right.

Advice: as a beginner, do one thing at a time. If you want to learn how to use a progress bar, create a small test script that does nothing else but that. When you copy examples to try out, copy the whole example and run it as is. Only once you have really "got it" should you combine that technique with others.

Hope this helps!

Update: added answer

The way forward always starts with a minimal test.

In reply to Re: Using a progress bar... having issues by 1nickt
in thread Using a progress bar... having issues by TitoLasVegas

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.