If I'm understanding you correctly, you want to execute some code outside of the loop while the songs are playing.  As the playing itself already appears to happen in the background, the most simple approach would be to call some other routine in your program every time you get around to calling sleep at the end of the loop, and when that routine returns, just sleep for the remaining time only (i.e. song length minus time the routine took). That would mean your other code executes synchronously with the loop iterations, so a single call of the routine may not take more time than the length of the current song allows. — In other words, make that while loop the main loop of your program and arrange the other code around it.

If you need asynchronous execution, you could use threads or fork a separate process. Which technique to use depends on a number of factors, for example how you want to share data. The latter is somewhat simpler with threads, because different processes (as created with fork) would require interprocess communication (IPC), such as pipes, sockets and signals, to explicitly exchange information.


In reply to Re: Looping in the background of your main program by almut
in thread Looping in the background of your main program by Baphi

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.