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

Dear Masters,
I have a code (mycode.pl) that need to open files that exist in subdirectory (subdir). The subdirectory contains two files that need to be opened, (data.pri and data.sec)
~/MyPerl/ | |__ mycode.pl |__ subdir |____ data.pri |____ data.sec
I fail to open these two files with this snippet.
# mycode.pl my $file = "/files/data.pri"; my $name = (split(/[\/\.]/,$file))[2]; my $file_sec = "/files/$name.sec"; my $result = some_function($file,$file_sec);
What's wrong with it? How can I go about this problem? Thanks so much beforehand.

Update: Some amendment based on bart's comment.

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: How to Open Files from Subdirectory
by bart (Canon) on Aug 03, 2005 at 08:06 UTC
    Just print out $name and you'll see what's wrong with it.

    I'm spilling the beans: it will contain "/files/data"".

    But your mechanism is very unsafe. What if the file path contains more than one dot?

      Dear bart,
      Thanks for your correction. I've made update based on your suggestion. Now $name is printed as "data". I've also added function to clarify my intention. But it still gives me:
      Can't open file /files/data.pri:
      Is there anything wrong with this syntax?
      my $file = "/files/test1.fasta";
      If not, please kindly advice what's the correct one.

      ---
      neversaint and everlastingly indebted.......
Re: How to Open Files from Subdirectory
by inman (Curate) on Aug 03, 2005 at 09:23 UTC
    You could use Cwd, File::Basename and File::Spec to find the current directory and perform a number of file path operations in an OS independant manner.
Re: How to Open Files from Subdirectory
by holli (Abbot) on Aug 03, 2005 at 08:16 UTC
    "/files/$name.sec" is an absolute path. You want a path relative to the actual directory ("files/$name.sec") or to your homedir ("~/files/$name.sec").


    holli, /regexed monk/
Re: How to Open Files from Subdirectory
by anonymized user 468275 (Curate) on Aug 03, 2005 at 08:51 UTC
    All the references to '/files/' make me feel like a fashion jounalist in an 'Emperor's new clothes' scenario.

    Is it me or are you really trying to use the /files device instead of '~/Myperl/subdir/' which is likely to be on the /home device?

    Just in case it's the real problem, the following code would not work:

    open my $fh, '<~/Myperl/files/data.pri' or die "$! - unix specific";
    But this might
    open my $fh, "<$ENV{HOME}/Myperl/files/data.pri" or die "$! - one laye +r of problem removed";

    One world, one people

Re: How to Open Files from Subdirectory
by critter (Acolyte) on Aug 03, 2005 at 08:17 UTC
    Is it not your path to the files? If I look at your file structure and the location of the files the path should be different. Also if you print your scalars you would notice that the $file_sec comes back different then expected. In your case it would result in:

    $file: "/files/data.pri"
    $file_sec: "/files/files/data.sec"

    This should do what you want:

    # mycode.pl my $file = "files/data.pri"; my $name = (split(/\./,$file))[0]; my $file_sec = "$name.sec";
    prints now:

    $file: "file/data.pri"
    $file_sec: "file/data.sec"

    hope this helps,

    Critter

Re: How to Open Files from Subdirectory
by blazar (Canon) on Aug 03, 2005 at 10:04 UTC
    I fail to open these two files with this snippet.
    # mycode.pl my $file = "/files/data.pri"; my $name = (split(/[\/\.]/,$file))[2]; my $file_sec = "/files/$name.sec"; my $result = some_function($file,$file_sec);
    I beg your pardon, since I know that my reply doesn't add anything to the good advices you already got, but it seems to me that "this snippet" does nothing to open any file except possibly in some_function, in which case it could be worth to show the code for it too. Or else the problem may not be an actual open, in which case your title may end up being confusing.