I have a script which is set to run 24/7. Every day, it will create a new logfile of actions performed that day. This is for reporting and tuning purposes, to make sure the script is running at maximum efficiency. The question I have is outlined in the code below:
use strict; use warnings; use Class::Date qw(:errors date localdate gmdate now -DateParse -EnvC) +; my $start = date now; #start date of the program running in format YYY +Y-MM-DD HH:MM:SS my @split = split(/ /, $start); #get just the Year/Month/Day open (FILE, ">>/my/directory/logfile-$split[0]"); #open our daily logf +ile print FILE "$start\n"; sub dailyTask{ my $now = date now; #Get our current time my @split = split(/ /, $now); close FILE; #close the existing daily file, in production this wil +l zip it into a running archive of daily files open (FILE, ">>/my/directory/logfile-$split[0]"); #open our new da +ily file } while(1){ my $test = date now; #date when we start the loop print "Start: $start\n"; #print our values, so I can see print "Test: $test\n"; my $soon = $start + '1D'; #Adds 1 day onto the start date print "Soon: $soon\n"; print FILE "$test\n"; if($soon < $test){#Check if I've passed our start + 1 day print "It's soon, now!\n"; #If so, print our value $start = $test; #update the new start time dailyTask(); } sleep(86400); #sleep until tomorrow }
Essentially, what I'm not sure if is whether or not the <FILE> file handle will (a) be passed to the called subroutine, and (b) whether the newly defined file handle will persist outside the scope of the subroutine. What is marked above is the essence of what I'm trying to accomplish (obviously with all the additional logic stripped out, for readability). Does anyone have any suggestions, or should the code work as written? Thank you.

In reply to Scope of Filehandle inside Subroutine? by SituationSoap

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.