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

hello Monks
#!/usr/bin/perl 
use LWP::Simple;
my $url  = "http://www.hprd.org/edownload/HPRD_FLAT_FILES_090107";
my $file1 = "HPRD_FLAT_FILES_090107.tar";
# get the data
is_success(getstore($url, $file1))
    or die "Mark probably broke the server again!\n";
 use Archive::Tar;
    my $tar = Archive::Tar->new;
$tar->read('HPRD_FLAT_FILES_090107.tar',1);
    $tar->extract(); 
$output = "arch.txt";
# open file
open(FILE, "<\HPRD_FLAT_FILES_090107\PROTEIN_Architecture.txt") or die("Unable to open file");
open(OUTPUT1, ">$output") or die("Unable to open file");
# read file into an array
@hprdArchi = <FILE>;
foreach $line (@hprdArchi){
if ($line =~ s/^/'/ and $line =~ s/\t/','/g and $line =~ s/$/'/){
print OUTPUT1 " $line ";
}
}
the error i am getting is
Unable to open file at C:\Perl\hprd.pl line 15.
is there any problem opening file from a subfolder
or if you feel there is any other better way to do this
please give me your advise and suggestions
Thanks

Replies are listed 'Best First'.
Re: problem opening file
by moritz (Cardinal) on Jan 10, 2008 at 17:39 UTC
    Perl provides a (usually helpful) error message in the variables $!.

    So change your code to read

    open (...) or die "Can't ope file: $!";

    And you'll know more.

      And it's also good practice to include the filename you're attempting to open in the message as well so you know what you were trying to monkey with as well as why exactly it failed.

      (And a hint as to your specific problem: backslashes, "\", are special characters in double quoted strings; Perl will allow you to use plain slashes, "/", in filenames that you pass to system calls to avoid just this kind of problem.)

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        And if you had warnings enabled you would have got warnings about unrecognized escapes. Putting them in I got these messages:

        Unrecognized escape \H passed through at Perl-1.pl line 22. Unrecognized escape \P passed through at Perl-1.pl line 22.
        It is considered good practice to start each script with
        use warnings; use strict;
        which will pick up a lot of these types of problems.

        hello monks
        I tried using // because single / will be an escape character
        but it did not work even after doing that
        Unable to open file:No such file or directory at C:\Perl\hprd.pl line 14.
        is there any way to open files from a subdirectory
Re: problem opening file
by graff (Chancellor) on Jan 11, 2008 at 02:59 UTC
    The problem is one of these two lines, most likely the first one:
    open(FILE, "<\HPRD_FLAT_FILES_090107\PROTEIN_Architecture.txt") or die +("Unable to open file"); open(OUTPUT1, ">$output") or die("Unable to open file");
    I believe that when perl is installed on a windows machine, it's smart enough to do the right thing when you use forward slashes in a file path rather than backward slashes, so the first of those two lines ought to work if changed to:
    $infile = "/HPRD_FLAT_FILES_090107/PROTEIN_Architecture.txt"; open(FILE, "<", $infile) or die("Open failed for reading $infile: $!");
    In case the error came from the second line, you'll find it easier to track down if you craft the error messages to be more clear about where the problem occurs:
    open(OUTPUT1, ">", $output) or die "Open failed for writing $output: $ +!");
    BTW, since the file you are trying to open for input comes from a tar file that you have already loaded into memory, why not just get the txt file from the tar object? Take a closer look at Archive::Tar (and it might not be too shameless for me to point out an article about this module in the Reviews section). You'll find a way to get the file content directly from the tar object -- you can still use "extract" to unpack the content in your disk directory if you want, but the file you are looking for is already in memory, so you could do something like:
    @hprdArchi = split /\n/,$tar->get_content("HPRD_FLAT_FILES_090107/PROT +EIN_Architecture.txt");