in reply to More File/Directory Trouble

Sigh. Others have answered the question and I am ( once again ) simply throwing pop bottles from the bleachers.
open(FILE, ">folder/${name}/${date}.txt");
Why are you doing this? I know, you are making sure Perl Does the Right Thing and you have been bitten by ( I am guessing here ) shell programming.

In Perl, legal variables names are must start with an alphabetic character or an underscore. After that, it can be lots of things. It cannot be, though, / or ".". Which means you can write that line as

open(FILE, ">folder/$name/$date.txt");
which is easier on my poor eyes. It flows, it makes sense.

Of course, I also fall off the end of this statement. Especially when you open it for writing. You are not checking to see if you open the file. This will annoy you later, believe me. A wise monk ( or at least a monk who is tired of being bitten by this ) would say

open(FILE, ">folder/$name/$date.txt") || die("Couldn't open file for w +riting: $!");
which will not only protect you from trying to write to an unopened file, but it will also tell you why it didn't work.

Of course, a stylish monk would take offense to all the parens in there. Actually, a stylish monk is really lazy and hates the cording necessary to make ( and ). So s/he would say instead

open FILE, ">folder/$name/$date.txt" or die "Couldn't open file for re +ading:$!";
I would take this one step further and make the error message more informative and say
my $filename = "folder/$name/$date.txt"; open FILE, ">$filename" or die "Couldn't open $filename for writing: $ +!";
This would tell me what the code was trying to open so I could figure out what I screwed up this time.

mikfire

Replies are listed 'Best First'.
RE: RE: More File/Directory Trouble
by Fastolfe (Vicar) on Jul 29, 2000 at 19:48 UTC
    While you're correct that the {brackets} around the variables aren't necessary in this case, they're still perfectly legal and necessary in cases where ambiguity exists.
    $var = "${one}two$three"; # "1two3" $var = $one . "two$three"; # "1two3"
    Certainly both of these methods achieve the same result, but there are instances when putting two variables together in a double-quoted string is more legible or needed for other reasons. In these cases, surrounding the variable name in brackets is necessary.
      Sometimes the posting code in perlmonks annoys me. Chalk it up to inexperience I guess. See the next post, which I would have preferred would replace the one I'm replying to here.

      Why can't there be a delete function? Heh.

RE: RE: More File/Directory Trouble
by Fastolfe (Vicar) on Jul 29, 2000 at 19:50 UTC
    Ignore this post -- I misread your comment as chastizing the poster for using ${shell} style variables that "weren't" supported in Perl. You are absolutely right and the explanation was good.

    While you're correct that the {brackets} around the variables aren't necessary in this case, they're still perfectly legal and necessary in cases where ambiguity exists.

    $var = "${one}two$three"; # "1two3" $var = $one . "two" . $three; # "1two3"
    Certainly both of these methods achieve the same result, but there are instances when putting two variables together in a double-quoted string is more legible or needed for other reasons. In these cases, surrounding the variable name in brackets is necessary.