bashperlmktemp

random file name in perl generated with unusual characters


Using this perl code below, I try to output some names in a random generated file. But the files are created with weird characters like this:

"snp-list-boo.dwjEUq5Wu^J.txt"

And, obviously when my code looks for these files it says not such file. Also, when I try open the files using "vi", they open like this

vi 'temporary-files/snp-list-boo.dwjEUq5Wu
.txt'

i.e. with a "new line" in the file name. Someone please help me understand and solve this weird issue. Thanks much!

code:

my $tfile = `mktemp boo.XXXXXXXXX`;
my $fh = "";
     foreach my $keys (keys %POS_HASH){
       open ($fh, '>>', "temporary-files/snp-list-$tfile.txt");
        print $fh "$keys $POS_HASH{$keys}\n";
        close $fh;
         }

Solution

  • mktemp returns a line feed character in its output that you need to chop() or chomp() first.

    Instead of using the external mktemp program, why don't you go with File::Temp instead?