By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Explain this code please?

P: 31
Expand|Select|Wrap|Line Numbers
  1.          for ($i = 0; $s < @FILENAME; $s++) {
  2.                 if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
  3.                         $PERLFILE[$ss] = $FILENAME[$s];
  4.                         $ss++;
  5.                 }
  6.         }
I would say that this scirpt idenflies some file that is it perl script file or other, but can someone explain this line by line?
Oct 12 '08 #1
Share this Question
Share on Google+
14 Replies


P: 31
damn i cant edit it but the script has error on the first line the $i is supposed to be $s
Oct 12 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
Um, where did you copy this code from? Either the $i needs to be a $s or the other way around. The for loop, as used, will never increment $i, so why use it?
Oct 12 '08 #3

P: 31
Um, where did you copy this code from? Either the $i needs to be a $s or the other way around. The for loop, as used, will never increment $i, so why use it?
yeah i already fixed this in my first post and my friend gave it to me but he has gone to abroad so i cant contact him
Oct 12 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
Well, it loops through @FILENAME array, it uses the sunstr() function to get the last 4 or last 3 characters from each filename in the array and uses the "eq" operator to see what that value is (.cgi or .pl). It appears to assign that value to another array @PERLFILE if it does equal .cgi or .pl.

Looks like code written by a person that only knows the very basics or perl coding. It will work but there are probably better ways to do the same thing.
Oct 12 '08 #5

P: 31
there are probably better ways to do the same thing
How would you do it?
Oct 12 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
Without knowing anything else about your existing program, one possibility:

Expand|Select|Wrap|Line Numbers
  1. my @PERLFILES = grep {/\.(cgi|pl)$/} @FILENAME;
Oct 12 '08 #7

P: 31
I will post my friends whole script now
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ## Made by Josh ##
  3.         opendir(DIR, ".");
  4.         @FILENAME = readdir(DIR);
  5.         closedir(DIR);
  6.          for ($s = 0; $s < @FILENAME; $s++) {
  7.                 if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
  8.                         $PERLFILE[$ss] = $FILENAME[$s];
  9.                         $ss++;
  10.                 }
  11.         }
  12.         for ($s = 0; $s < @PERLFILE; $s++) {
  13.                 open(FILE,$PERLFILE[$s]);
  14.                 $fileindex = join("",<FILE>);
  15.                 close(FILE);
  16.                 if (index($fileindex, "## Made by Josh ##") == -1) {
  17.                         open(FILE,">>$PERLFILE[$s]");
  18.                         print FILE "## Made by Josh ##";
  19.                         close(FILE);
  20.                 }
  21.         }
  22.         closedir(DIR);
this script writes made by Josh in every script that havent got one yet in current directory. I would like to improve it by also writing to files that are in subdirectory. Now i know how to change the "readdir" part to read in all subdirs but the
Expand|Select|Wrap|Line Numbers
  1.          for ($s = 0; $s < @FILENAME; $s++) {
  2.                 if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
  3.                         $PERLFILE[$ss] = $FILENAME[$s];
  4.                         $ss++;
  5.                 }
  6.         }
still would need changing and kevins code wouldnt work in this situation so any help?
Oct 12 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ## Made by Josh ##
  3. use strict;
  4. use warnings;
  5. use File::Find;
  6.  
  7. my $start = 'start_directory';
  8. find({no_chdir => 1, wanted => \&wanted}, $start);
  9. print "Finished";
  10.  
  11. sub wanted {
  12.    if ($File::Find::name =~ /\.(cgi|pl)$/i) {
  13.       open (my $fh, ">>", $File::Find::name) or die "$!";
  14.       print $fh "\n## Made by Josh ##";
  15.    }
  16. }
Make sure to test it on some directory and files first to make sure it works OK. Will find all .cgi and .pl files in the "start_directory" and all sub directories of the "start_directory" and add your line.

Read the File::Find documentation for explanations of the code.
Oct 12 '08 #9

P: 31
Thanks Kevin it works very well but i would also like to know how to check if the file already got the "## Made by Josh ##"
Oct 13 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
Thanks Kevin it works very well but i would also like to know how to check if the file already got the "## Made by Josh ##"
I leave that up to you to figure out.
Oct 13 '08 #11

P: 31
I leave that up to you to figure out.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ## Made by Josh ##
  3. use strict;
  4. use warnings;
  5. use File::Find;
  6.  
  7. my $start = '.';
  8. find({no_chdir => 1, wanted => \&wanted}, $start);
  9. print "Finished";
  10.  
  11. sub wanted {
  12.    if ($File::Find::name =~ /\.(cgi|pl)$/i) {
  13.    open (my $fh, ">>", $File::Find::name) or die "$!";
  14.    my $filee = join("",<$fh>);
  15.    close(FILE);
  16.       if (index($fh, "## Made by Josh ##") == -1) {
  17.       open(FILE,">>$fh")
  18.       print $fh "\n## Made by Josh ##";
  19.       close(FILE);
  20.  
  21.    }
  22.    }
  23.    }
well i tried but it gives syntax error and the error says that something is wrong in line 18
Oct 13 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
OK, you gave it a try. I give you credit for the effort if not the execution. See how this works:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ## Made by Josh ##
  3. use strict;
  4. use warnings;
  5. use File::Find;
  6.  
  7. my $start = '.';
  8. find({no_chdir => 1, wanted => \&wanted}, $start);
  9. print "Finished";
  10.  
  11. sub wanted {
  12.    my $fh;
  13.    if ($File::Find::name =~ /\.(cgi|pl)$/i) {
  14.       open ($fh, $File::Find::name) or die "$!";
  15.       while (<$fh>) {
  16.          if (/## Made by Josh ##/) {
  17.             return(0);
  18.          }
  19.       }
  20.       undef $fh;
  21.       open($fh, ">>", $File::Find::name) or die "$!";
  22.       print $fh "\n## Made by Josh ##";
  23.       undef ($fh);
  24.    }
  25. }
Oct 13 '08 #13

P: 31
Ok thanks very much Kevin!
Oct 13 '08 #14

KevinADC
Expert 2.5K+
P: 4,059
Ok thanks very much Kevin!
You're welcome
Oct 13 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.