in reply to Re^2: No Substitution Happening
in thread No Substitution Happening

Hi All,

please see the below code,

#sub change_ext_if_file { #foreach my $file ($_) { # if (($file eq -f) && ($file =~ /.pdf$/)) { # $file =~ s/.pdf/.doc/; #Above, better written as below, perhaps? sub change_ext_if_file { my @files = $_; foreach my $file if ((@files eq -f) && (@files =~ /\.pdf$/)) { $file =~ s/pdf/doc/; } }

I'm guessing, going by some code suggestions above, that there is a quicker, cleaner way of achieving the end result, but, I'm just playing, to learn what things do, so I'm not looking, I guess, for shorter ways just yet. Hope that makes sense, and doesn't seem ungrateful? Cheers

coolboarderguy...

Replies are listed 'Best First'.
Re^4: No Substitution Happening
by coolboarderguy (Acolyte) on Mar 24, 2006 at 09:22 UTC
    Hi All,

    #!/usr/bin/perl use strict; use warnings; use File::Find; my $dir = shift; #dir = the argument given on the command line (./fi +lechng.pl testdir) find(\&change_ext_if_file, "$dir"); #loops through dir scooping up all + it finds sub change_ext_if_file { #called function my @files = @_; #creates array foreach my $file (@files) { ##loops through the array @files and + places each file into scalar $file if ((-f $file) && ($file =~ /pdf$/)) { #checks if file and if e +nds with pdf $file =~ s/pdf/doc/; #replaces pdf with doc } } }

    Is this closer? It runs with no errors, but, still no changes. Cheers

    EDIT: Good lord, I'm a dimwit at best. That above looks for the string inside the file, not changes the name. I now see my wrong in this...sorry for not picking that up any earlier than now. Cheers, again coolboarderguy...

      That above looks for the string inside the file, not changes the name.

      No, it looks for the string inside the filename. The substitution operator acts on strings, not files, not filenames, etc. If you want to look inside the file, you need to open it. If you want to rename the file, see perldoc -f rename.