Explain this code please?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • über
    New Member
    • Sep 2007
    • 31

    Explain this code please?

    Code:
             for ($i = 0; $s < @FILENAME; $s++) {
                    if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
                            $PERLFILE[$ss] = $FILENAME[$s];
                            $ss++;
                    }
            }
    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?
  • über
    New Member
    • Sep 2007
    • 31

    #2
    damn i cant edit it but the script has error on the first line the $i is supposed to be $s

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      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?

      Comment

      • über
        New Member
        • Sep 2007
        • 31

        #4
        Originally posted by numberwhun
        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

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          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.

          Comment

          • über
            New Member
            • Sep 2007
            • 31

            #6
            there are probably better ways to do the same thing
            How would you do it?

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Without knowing anything else about your existing program, one possibility:

              Code:
              my @PERLFILES = grep {/\.(cgi|pl)$/} @FILENAME;

              Comment

              • über
                New Member
                • Sep 2007
                • 31

                #8
                I will post my friends whole script now
                Code:
                #!/usr/bin/perl
                ## Made by Josh ##
                        opendir(DIR, ".");
                        @FILENAME = readdir(DIR);
                        closedir(DIR);
                         for ($s = 0; $s < @FILENAME; $s++) {
                                if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
                                        $PERLFILE[$ss] = $FILENAME[$s];
                                        $ss++;
                                }
                        }
                        for ($s = 0; $s < @PERLFILE; $s++) {
                                open(FILE,$PERLFILE[$s]);
                                $fileindex = join("",<FILE>);
                                close(FILE);
                                if (index($fileindex, "## Made by Josh ##") == -1) {
                                        open(FILE,">>$PERLFILE[$s]");
                                        print FILE "## Made by Josh ##";
                                        close(FILE);
                                }
                        }
                        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
                Code:
                         for ($s = 0; $s < @FILENAME; $s++) {
                                if (substr($FILENAME[$s], length($FILENAME[$s]) - 4) eq ".cgi" || substr($FILENAME[$s], length($FILENAME[$s]) - 3) eq ".pl") {
                                        $PERLFILE[$ss] = $FILENAME[$s];
                                        $ss++;
                                }
                        }
                still would need changing and kevins code wouldnt work in this situation so any help?

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Code:
                  #!/usr/bin/perl
                  ## Made by Josh ##
                  use strict;
                  use warnings;
                  use File::Find;
                  
                  my $start = 'start_directory';
                  find({no_chdir => 1, wanted => \&wanted}, $start);
                  print "Finished";
                  
                  sub wanted {
                     if ($File::Find::name =~ /\.(cgi|pl)$/i) {
                        open (my $fh, ">>", $File::Find::name) or die "$!";
                        print $fh "\n## Made by Josh ##";
                     }
                  }
                  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_director y" and all sub directories of the "start_director y" and add your line.

                  Read the File::Find documentation for explanations of the code.

                  Comment

                  • über
                    New Member
                    • Sep 2007
                    • 31

                    #10
                    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 ##"

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      Originally posted by über
                      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.

                      Comment

                      • über
                        New Member
                        • Sep 2007
                        • 31

                        #12
                        Originally posted by KevinADC
                        I leave that up to you to figure out.
                        Code:
                        #!/usr/bin/perl
                        ## Made by Josh ##
                        use strict;
                        use warnings;
                        use File::Find;
                        
                        my $start = '.';
                        find({no_chdir => 1, wanted => \&wanted}, $start);
                        print "Finished";
                        
                        sub wanted {
                           if ($File::Find::name =~ /\.(cgi|pl)$/i) {
                           open (my $fh, ">>", $File::Find::name) or die "$!";
                           my $filee = join("",<$fh>);
                           close(FILE);
                              if (index($fh, "## Made by Josh ##") == -1) {
                              open(FILE,">>$fh")
                              print $fh "\n## Made by Josh ##";
                              close(FILE);
                        
                           }
                           }
                           }
                        well i tried but it gives syntax error and the error says that something is wrong in line 18

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          OK, you gave it a try. I give you credit for the effort if not the execution. See how this works:

                          Code:
                          #!/usr/bin/perl
                          ## Made by Josh ##
                          use strict;
                          use warnings;
                          use File::Find;
                           
                          my $start = '.';
                          find({no_chdir => 1, wanted => \&wanted}, $start);
                          print "Finished";
                           
                          sub wanted {
                             my $fh;
                             if ($File::Find::name =~ /\.(cgi|pl)$/i) {
                                open ($fh, $File::Find::name) or die "$!";
                                while (<$fh>) {
                                   if (/## Made by Josh ##/) {
                                      return(0);
                                   }
                                }
                                undef $fh;
                                open($fh, ">>", $File::Find::name) or die "$!";
                                print $fh "\n## Made by Josh ##";
                                undef ($fh);
                             }
                          }

                          Comment

                          • über
                            New Member
                            • Sep 2007
                            • 31

                            #14
                            Ok thanks very much Kevin!

                            Comment

                            • KevinADC
                              Recognized Expert Specialist
                              • Jan 2007
                              • 4092

                              #15
                              Originally posted by über
                              Ok thanks very much Kevin!
                              You're welcome

                              Comment

                              Working...