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

Hi All,

$dir doesn't need to be interpolated into a string, it is already.

Could you elaborate a little on that, please? Are you referring to the foreach of $dir?

$_ doesn't have any useful contents inside the sub and it certinally isn't an array, or even a reference to and array, so foreach doesn't help anything.

Does the below make it into an array?

sub change_ext_if_file { @files = $_; #continuing code left off for this post

Cheers

coolboarderguy...

Replies are listed 'Best First'.
Re^3: No Substitution Happening
by GrandFather (Saint) on Mar 24, 2006 at 08:27 UTC

    When you use a variable inside a string like "This is a string with $var interpolated into it" Perl replaces the variable ($var in this example) with the contents of the variable. However, if you write "$var" you can omit the quotes because the variable will generally be treated as a string in any case.

    A variable that starts with $ is a scalar variable. They can only store one thing, a number or a string or a reference to something. $_ is just a special scalar variable that is used by default for some things. At the start of a subroutine you generally should not depend on $_ being set to some expected value. You need to ask yourself "Where is $_ getting a value from and what sort of value is it?". To answer that you need to look at the documentation for, in this case File::Find, and see if it says that $_ is getting set to some value for you. If it doesn't (it doesn't you know), then it ain't.

    It may be that you are getting confused with @_, which is the special array variable used to pass parameters into a subroutine. However, again you need to consult the documentation to see what is passed into the subroutine by File::Find. From the documentation you will find that "The wanted function takes no arguments but rather does its work through a collection of variables.". So that doesn't help you either.

    Take another look at the code that I wrote and search the File::Find documentation for anything that looks File::Find related that you don't understand. Everything else should be pretty clear. :)


    DWIM is Perl's answer to Gödel
      Hi All,

      yes, I was getting confused between the two, $_ and @_. Cheers.

      P.S. I don't know how this started as a new thread, as I thought I was replying in the original thread

      coolboarderguy...

Re^3: No Substitution Happening
by coolboarderguy (Acolyte) on Mar 24, 2006 at 09:04 UTC
    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...

      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.