Greetings, Monks!
I'm attempting to create temporary links (prevent "hot linking"). So what I'm attempting, is to create a symlink on initiation, and set a timer (sleep / while) that when reached, unlinks the symlink. It all works just fine, except, that it sort of "hangs" the script/web page, while the timer is counting down. I first attempted this with the sleep function. But am now trying with a while(). Here's what I have now

#!/usr/bin/perl -w use strict; use feature qw(say); use POSIX qw(strftime); my $ctime= strftime "%H-%M", localtime; my $dlfile= "../PATH_TO/SOME_FILE"; my $dlname= "SOMENAME"; my $newname= "./ANOTHER_PATH/$ctime-$dlname"; my $maxtime= 30; my $args = ('-s'); print "content-type:text/html; charset=utf-8\n\n"; say qq~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" + /> <title>Download $dlname</title> </head> <body <h1>Download $dlname</h1> ~; system("/bin/ln $args $dlfile $newname"); while ($maxtime > 0) { $maxtime--; sleep(1); say qq~<a href="$newname">download</a>~; if ($maxtime == 0) { unlink $newname; say qq~<h3>Sorry! $newname no longer exists!</h3>~; } } say qq~<p> <br /> </p> </body></html> ~; exit
I'm sure this is really simple. But not for me, today. :/
Thanks, for any pointers!

--Chris

EDIT: fix a couple of typos. Move a block.

¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH


In reply to How to sleep(), or use a while() in the background? by taint

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.