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

I have seen similar questions posted around and have tried researching this topic prior to asking you all, but I am stuck. I am trying, through command-line argument, take a directory, search for all file extensions in that directory that end in .pl and change them to .txt. I then want to go into any of the subdirectories in that parent directory and do the same thing. I am trying to use glob, because it seems like a neat little function. I don't have to use glob. I was just trying to solve this by using glob, so I am open to any other suggestions.

I have tried changing the file extensions in one directory. I am trying to start small, but it does not work as intended. There are no compile errors, but I believe my understanding of some of the commands are incorrect.

This is what I have so far:

#!/usr/local/bin/perl use strict; use warnings; my @argv; my $dir = $ARGV[0]; my @files = glob "${dir}'*pl*'"; foreach (@files) { next if -d; (my $txt = $_) =~ s/pl$/txt/; rename($_, $txt); }

Test Example: "Insert a directory:" TestDir "Changing file formats in TestDir from .pl to .txt." "Changing file format in subdirectories of TestDir from .pl to .txt" etc.

Any help is appreciated.

Replies are listed 'Best First'.
Re: Use Of Glob On File Extensions
by GrandFather (Saint) on Nov 18, 2014 at 22:37 UTC

    I what way does it "not work as intended"?

    Why the single quotes in the string you pass to glob? I'd understand what is going on if there might be spaces in your path to be globed and you had my @files = glob "'${dir}*pl*'";, but the quotes around *pl* don't make sense to me.

    Perl is the programming world's equivalent of English
      I'm still new to the syntax on working with glob. I just made a dumb mistake in adding the extra parentheses. I did not understand the context of the quotes correctly.
Re: Use Of Glob On File Extensions (find/rule)
by Anonymous Monk on Nov 18, 2014 at 23:22 UTC
    File::Find::Rule could work, this of course untested
    use File::Find::Rule qw/ find rule /; rule( file => name => qr/\.pl$/i, exec => sub { my( $shortname, $path, $fullname ) = @_; my $newname = $fullname; $newname =~ s/\.pl$/.txt/i; use autodie qw/ rename /; rename $fullname, $newname; return !!0; # discard filename }, )->in( $dir );

      First, I would like to thank you for your help. Second, this is the first time I have heard of File::Find, so I had to study it. Is it possible to recursively go through directories with something like this?

      #!/usr/local/bin/perl use strict; use warnings; use File::Find; my @argv; my $dir = $ARGV[0]; find(\&recursiveDir, $dir); sub recursiveDir{ return if -d; my @files = glob "${dir}/*pl"; foreach (@files) { next if -d; (my $txt = $_) =~ s/pl$/txt/; rename($_, $txt); } }
        Is it possible to recursively go through directories with something like this?
        You want glob to return files with extension .pl. So you can't recursively go through directories if glob doesn't return directories. Actually, it will return them, if they also end with .pl. But really, use opendir, readdir, closedir.

        First, I would like to thank you for your help. Second, this is the first time I have heard of File::Find, so I had to study it. Is it possible to recursively go through directories with something like this?

        File::Find::Rule uses File::Find so that you don't have to -- use File::Find::Rule :) its easier

Re: Use Of Glob On File Extensions
by RonW (Parson) on Nov 18, 2014 at 21:58 UTC

    Try my @files = glob "${dir}'*.pl'";

    In yours, you would be getting any file with 'pl' anywhere in the name.

      Hmm, I just tried that and I received the same result.

        Oops - was only paying attention to the glob....

        Try my @files = glob "${dir}/*pl";