create copy, random filename of itself

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kuratkull
    New Member
    • Jan 2007
    • 12

    create copy, random filename of itself

    Hello,

    I am rather new at pearl, but I am a fast learner.

    So, I have been sitting up here, all night, trying to make a script that:

    Reads it's name, copies itself to the same folder, but with a random name with a .html suffix(I have my apache configure to handle .html as CGI).

    My code is here, this is the small batch I left for you to see, and It doesn't work.


    You are welcome to submit fixes and suggestion there :)

    Thank you.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Do you have Data::Random installed? I've never even heard of that module but thats no surprise, there are so many perl modules. But this is really easy to do without that module.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    print "Content-type: text/html\n\n";
    use File::Copy;
    my @length = (3..20);
    my @set = ('a'..'z','A'..'Z','0'..'9');
    my $key2 = $ENV{SCRIPT_NAME};
    my $rand_name = '';
    my $length = $length[rand @length];
    for(0..$length){
       $rand_name .= $set[rand @set];
    }
    $rand_name .= '.html';	
    print $rand_name;
    my $filetobecopied = "rando2.html";
    copy($filetobecopied, $rand_name) or die "File cannot be copied.";
    chmod 0666, $rand_name;
    ten runs produced this output:

    LTHbsX.html
    RTMvPRX.html
    obVmyxbPlp8lnye ZAVI.html
    ExYicXiGh9n03bq .html
    uEOMOhHdlv0QPF. html
    uIiF2orJK2u.htm l
    jSfdXEG06cuVQ.h tml
    A5Tfy32wt0YB.ht ml
    XokxahaECjvqEge 8SjjW.html
    5dDfE.html


    In your code this line has an error:

    Code:
    chmod $0666, '@newfile';
    $0666 should be 0666

    next time post your code here, asking people to go to another site to see that little bit of code is not necessary and just makes it harder to help you.

    Comment

    • kuratkull
      New Member
      • Jan 2007
      • 12

      #3
      wow, thx for such a detailed answer :)

      Next time, I will do as you told me.

      Thanks again.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        personally if it were me I might just use "time" for the filename instead of that random name stuff:

        Code:
        #!/usr/bin/perl
        use strict;
        use warnings;
        print "Content-type: text/html\n\n";
        use File::Copy;
        my $key2 = $ENV{SCRIPT_NAME};
        my $filetobecopied = "rando2.html";
        my $new_name = time . '.html';
        copy($filetobecopied, $new_name) or die "File cannot be copied.";
        chmod 0666, $new_name;
        might work for your purposes, might not.

        Comment

        Working...