CarlosN has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
This is my first post here.
I am trying to create a simple perl script to delete session files automatically using regex,
but my simple tiny regex won't find the file. The file is located in the same dir where the script
executes.

Script to find a file
#!"c:/perl/perl/bin/perl.exe" # Script name = use warnings; use diagnostics; use strict; my $file = qr/carlos\.txt/; if (defined($file) && ($file =~ /carlos\.txt/)){ print "file found"; } else { print "file not found"; }

Any suggestions?

Thanks!
Carlos

Replies are listed 'Best First'.
Re: Perl script to delete files using regex
by hippo (Archbishop) on Nov 04, 2020 at 17:31 UTC

    The lack of any filesystem interaction notwithstanding, your mistake is in comparing 2 regexen rather than matching a string. Here's an amended version of your script showing the match actually happening.

    #!/usr/bin/env perl use strict; use warnings; use diagnostics; my $regex = qr/carlos\.txt/; my $file = 'juan_carlos.txt'; if (defined ($file) && ($file =~ $regex)) { print "File $file matched\n"; } else { print "File $file not matched\n"; }

    I've named the variables appropriately. Note that there's nothing in this script to say that $file is anything to do with the filesystem (yet) - the variable just has a semantic name.


    🦛

Re: Perl script to delete files using regex
by LanX (Saint) on Nov 04, 2020 at 18:36 UTC
    Hi

    you might be interested in the module File::Find , it's core and has plenty of examples in the documentation.

    HTH :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Perl script to delete files using regex
by Fletch (Bishop) on Nov 04, 2020 at 17:22 UTC

    Nothing in your script does any sort of operation on the filesystem. Not to mention you're comparing the string representation of a regular expression against the same regular expression (which isn't really going to do anything useful; or again, do anything against the filesystem).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Perl script to delete files using regex
by Bod (Parson) on Nov 24, 2020 at 00:24 UTC

    If, as it appears, you are trying to find a file with a fixed name rather than a group of files with a common part to their name, no regexp is required. Just perform a string comparison on the filename.

     if ($file eq 'carlos.txt')

    This script will list everything in the directory where the script is located and point out the file named 'carlos.txt'

    use strict; my $file; # Read the current directory into an array opendir(my $dir, '.'); my @files = readdir($dir); closedir ($dir); # Iterate over array to file file foreach $file(@files) { print "$file"; if ($file eq 'carlos.txt') { print " <------------- Found File"; } print "\n"; }

      For filtering lists grep is often used:

      use warnings; use strict; opendir(my $dir, '.'); my ($file) = grep {$_ eq 'carlos.txt'} readdir($dir); closedir ($dir); print "$file <------------- Found File\n" if $file;
      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

        Yes indeed - much nicer and much more Perl-ish!

        I deliberately didn't write it that way though so that CarlosN could (hopefully) understand what is going on.