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

Hi,

I just can't get my head around this and I'm sure its so simple I'm going to feel stupid having asked the question but here goes.

What I'm trying to do is use $ENV{'SCRIPT_FILENAME'} as the path to the script and then as I know the name of the script substitute that name for the names of other files in the same directory the permissions of which I will than go on and change.

Here's a small debugging script I'm playing with to try and get this to work.

#!/usr/bin/perl @filelist =("includes.cgi","connect.cgi","commit.cgi"); print "Content-type:text/html\n\n"; $path = $ENV{'SCRIPT_FILENAME'}); foreach $filelist (@filelist){ $path =~ s/permissions.cgi/$filename/gi; print "$filelist -- $path<br />"; }


I'd be very grateful if some kind Monk could show me where I'm going wrong, or suggest an alternative way of "sensing" the path to files.

Many thanks

Sid

Replies are listed 'Best First'.
Re: Substitution on string held in a variable
by tinita (Parson) on Apr 01, 2004 at 11:03 UTC
    What I'm trying to do is use $ENV{'SCRIPT_FILENAME'} as the path to the script and then as I know the name of the script substitute that name for the names of other files in the same directory the permissions of which I will than go on and change.
    it is especially that paragraph i'm simply not understanding.
    please can you add some commas or other punctuation, i'm not a native english speaker.
    but at least i can understand your code. your replacing the string 'permissions.cgi' (1) in $path three times. the first time it is substituted it will be gone! i hope that's clear. so what do you have in there before and what do you want in there after? that's something we can work on.

    (1) besides that, please note, that you're not replacing the string permissions.cgi but the dot stands for (almost) any character. you want to put a backslash before it.
      Hi,

      Sorry about my English, I have no excuse, just lazyness.

      Thanks for pointing out my two errors.

      This now works perfectly:

      #!/usr/bin/perl @filelist =("includes.cgi","connect.cgi","commit.cgi"); print "Content-type:text/html\n\n"; foreach $filelist (@filelist){ $path = $ENV{'SCRIPT_FILENAME'}; $path =~ s/filelist\.cgi/$filelist/gi; print "$filelist -- $path<br />"; }


      Thanks again

      Sid
Re: Substitution on string held in a variable
by matija (Priest) on Apr 01, 2004 at 13:22 UTC
    If you want to find where the file your script was read from is, and thus find other files which might be in the same directory (or directories close), you need cpan::FindBin.
      Hi,

      Here's the alternative using FindBin
      #!/usr/bin/perl @filelist =("includes.cgi","connect.cgi","commit.cgi"); @file =(); use FindBin qw($Bin); print "Content-type:text/html\n\n"; foreach $filelist(@filelist){ $path = "$Bin\/$filelist"; push (@file,$path); } $changed = chmod 0777,@file; foreach $file(@file){ print "$file<br />"; } print "<br />$changed files changed";


      Sid
      Hi matija,

      Thanks for the tip I'll take a look at the module.

      Meanwhile this is what I'm playing with at the moment:

      #!/usr/bin/perl @filelist =("includes.cgi","connect.cgi","commit.cgi"); @file =(); print "Content-type:text/html\n\n"; foreach $filelist (@filelist){ $path = $ENV{'SCRIPT_FILENAME'}; $path =~ s/permissions\.cgi/$filelist/gi; push (@file,$path); } $changed = chmod 0777,@file; print "@file<br /><br />"; print "$changed files changed";


      It seems to work just fine.

      Best wishes

      Sid