Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: unlink files

by Anonymous Monk
on Aug 07, 2024 at 17:32 UTC ( [id://11160919]=note: print w/replies, xml ) Need Help??


in reply to unlink files

Did you actually try to run the script you exhibited? When I try I get Global symbol "$bp" requires explicit package name (did you forget to declare "my $bp"?) at clean-sql.pl line 13.. Fixing that gives me Can't use string ("backup/") as an ARRAY ref while "strict refs" in use at clean-sql.pl line 15.. The latter is caused by dereferencing a scalar that does not contain an array reference. I have no idea what the intent of that line is.

A pure-Perl implementation of the loop would go something like this:

    my $thirty_minutes_ago = time - 30 * 60;
    for my $file ( glob( "$folder*.sql" ) ) {
        if ( stat( $file )->mtime() < $thirty_minutes_ago ) {
            unlink $file or die "Failed to unlink $file: $!";
        }
    }
 

The above takes advantage of the fact that you used File::stat.

Replies are listed 'Best First'.
Re^2: unlink files
by frank1 (Scribe) on Aug 07, 2024 at 18:26 UTC

    thanks for this example, thanks so much

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11160919]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-09-07 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.