in reply to Re: how to log a process
in thread how to log a process

I have changed it like this
#!/usr/local/bin/perl # 2592000 = 30 day n min (60 *60 * 24 *30 ) use strict; use POSIX; use warnings; # change the below to dlt - dl30 my $logfile = "/script_logs/30DyDlt.txt"; my $date = localtime(); my $dir = '/oven_web/iportal/cgi-bin/uploads'; opendir(DIR,$dir) || die "Can't open $dir : $!\n"; my @files = readdir(DIR); # you may want to grep only certain files he +re close(DIR); ### my $logmsg = $date/$dir/$file; #print $logmsg = $date/$dir/$file; open LOGFILE, ">> $logmsg " or die " cannot open logfie $logfile for a +ppend $!"; foreach my $file(@files) { my $now = time; my @stat = stat("$dir/$file"); my $logmsg = $date/$dir/$file; if ($stat[9] < ($now - 2592000)) { print "Deleting $dir/$file..."; #unlink("$dir/$file"); #print "Done.\n"; print LOGFILE $logmsg, "\n"; } } close LOGFILE;
It does not compile for me iether... the error is; /usr/bin/perl /oven_script/tst/dlt30day-oven1d-j2.pl Global symbol "$logmsg" requires explicit package name at /oven_script/tst/dlt30day-oven1d-j2.pl line 21. Execution of /oven_script/tst/dlt30day-oven1d-j2.pl aborted due to compilation errors. --- I was thinking ( kinda) that i could append to a txt file the things I see if i just do a print "Deleting $dir/$file..." if I comment that (#) and uncomment the unlink("$dir/$file"); the older files really do go away... maybe it is harder than I thought... this does work
#!/usr/local/bin/perl # 2592000 = 30 day n min (60 *60 * 24 *30 ) use strict; # change the below to dlt - my $dir = '/oven_web/iportal/cgi-bin/uploads'; opendir(DIR,$dir) || die "Can't open $dir : $!\n"; my @files = readdir(DIR); # you may want to grep only certain files he +re close(DIR); foreach my $file(@files) { my $now = time; my @stat = stat("$dir/$file"); if ($stat[9] < ($now - 2592000)) { #print "Deleting $dir/$file..."; unlink("$dir/$file"); #print "Done.\n"; } }

Replies are listed 'Best First'.
Re^3: how to log a process
by Marshall (Canon) on May 28, 2009 at 05:55 UTC
    So compile error says that it doesn't like print LOGFILE $logmsg, "\n";The actual trouble may be with this: my $logmsg = $date/$dir/$file; because that looks like a division arithmetic expression. I would suggest changing:
    my $logmsg = "$date/$dir/$file"; print LOGFILE "$logmsg \n";
    Update:
    I looked again and you've got open LOGFILE, ">> $logmsg " or die ".." I think you meant: open LOGFILE, ">>$logfile" or ... ?

    I don't see any problem with the logging idea. Opening a file for append is a very normal thing to do. If you are the only user of this file, this approach will work great. Nothing fancy needed. Get the code to compile and run, then report the run-time errors.

      splain
      Global symbol "$logmsg" requires explicit package name at /oven_script/tst/dlt30day-oven1d-j2.pl line 21. (#1) (F) You've said "use strict vars", which indicates that all variab +les must either be lexically scoped (using "my"), declared beforehand +using "our", or explicitly qualified to say which package the global var +iable is in (using "::").
        Yep! Right.
        This an error before the other errors. The first "my $logmsg" is commented out. There is more than one problem in the code. Once poster gets to the: my $logmsg = $date/$dir/$file; it will be found that is a "division by zero error". I was just looking in web browser and not in my editor so didn't see the exact line.

        I highly advise the op to not reuse $logmsg both as a file name and as a message text name.

      well it still did not compile /usr/bin/perl /oven_script/tst/dlt30day-oven1d-j3.pl Global symbol "$file" requires explicit package name at /oven_script/tst/dlt30day-oven1d-j3.pl line 14. Execution of /oven_script/tst/dlt30day-oven1d-j3.pl aborted due to compilation errors. so it hates $file
      #!/usr/local/bin/perl # 2592000 = 30 day n min (60 *60 * 24 *30 ) use strict; use POSIX; use warnings; # change the below to dlt - dl30 my $logfile = "/script_logs/30DyDlt.txt"; my $date = localtime(); my $dir = '/oven_web/iportal/cgi-bin/uploads'; opendir(DIR,$dir) || die "Can't open $dir : $!\n"; my @files = readdir(DIR); # you may want to grep only certain files he +re close(DIR); ### my $logmsg = $date/$dir/$file; my $logmsg = "$date/$dir/$file"; #print $logmsg = $date/$dir/$file; open LOGFILE, ">> $logmsg " or die " cannot open logfie $logfile for a +ppend $!"; foreach my $file(@files) { my $now = time; my @stat = stat("$dir/$file"); if ($stat[9] < ($now - 2592000)) { print "Deleting $dir/$file..."; #unlink("$dir/$file"); #print "Done.\n"; print LOGFILE "$logmsg \n"; } } close LOGFILE;
      so - i ( just for fun ) tried a my $file = "aaa" thinking explictedly declare it a value and later it gets the real fileNames -- or i was curious what wod happen...
      #!/usr/local/bin/perl # 2592000 = 30 day n min (60 *60 * 24 *30 ) use strict; use POSIX; use warnings; # change the below to dlt - dl30 my $logfile = "/script_logs/30DyDlt.txt"; my $date = localtime(); my $dir = '/oven_web/iportal/cgi-bin/uploads'; my $file = "aaa" opendir(DIR,$dir) || die "Can't open $dir : $!\n"; my @files = readdir(DIR); # you may want to grep only certain files he +re close(DIR); ### my $logmsg = $date/$dir/$file; my $logmsg = "$date/$dir/$file"; #print $logmsg = $date/$dir/$file; open LOGFILE, ">> $logmsg " or die " cannot open logfie $logfile for a +ppend $!"; foreach my $file(@files) { my $now = time; my @stat = stat("$dir/$file"); if ($stat[9] < ($now - 2592000)) { print "Deleting $dir/$file..."; #unlink("$dir/$file"); #print "Done.\n"; print LOGFILE "$logmsg \n"; } } close LOGFILE;
      well that blew up in the following manner ::: /usr/bin/perl /oven_script/tst/dlt30day-oven1d-j3.pl "my" variable $file masks earlier declaration in same scope at /oven_script/tst/dlt30day-oven1d-j3.pl line 20. syntax error at /oven_script/tst/dlt30day-oven1d-j3.pl line 11, near "opendir" Execution of /oven_script/tst/dlt30day-oven1d-j3.pl aborted due to compilation errors. hummmmm
        well it still did not compile
        /usr/bin/perl /oven_script/tst/dlt30day-oven1d-j3.pl Global symbol "$f +ile" requires explicit package name at /oven_script/tst/dlt30day-oven +1d-j3.pl line 14.

        Quite unsurprising, because you don't declare $file and you never assign a value to it before using it. You use a variable @files above that - maybe you wanted to use an element of @files?