in reply to How do you properly use a variable for FILEHANDLE in print vs open

Your $comb is not declared to satisfy strict,

# . . . my $comb = $var_a . $var_b;
Then you try to use the value of $comb as the filehandle name as well as part of the file name. Why not use a lexical file handle? You open the file to read, not write, so your print will fail in any case.
open (my $fh, '>', "c:\\my\\dest\\dir\\" . $comb . "found.txt") || die "failure to create" . $comb; print $fh $var_c, "content detail for file found\n";

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: How do you properly use a variable for FILEHANDLE in print vs open
by Anonymous Monk on Jul 07, 2004 at 17:52 UTC
    First off, Thanks, for the feedback.

    To answer Chromatics question:

    I should preface that I am an Architecture graduate, who dabbles in code. I have had no formal training in coding... so if the following sounds, well just wrong, it just may be.
    I want, to create then manipulate and finalize as one continuos process on several files at once. Logic being If I can access multiple files at the same time I can process things faster. Therefore using a fixed filehandle, seems limiting.
    Figured since I could create the file and name it based on a predetermined content, then I could edit based on the file name. To allow other scripts to manipulate files I would need to use a unique FILEHANDLE to call on, a at-runtime-variable-filehandle keyed to the content would keep each file unique, baring duplicates.

    My initial test script to test the at-runtime-variable-filehandle idea worked, yet with the strict pragma invoked the test script failed. Thus my confusion... and the need for help.

    After some thinking time and some Perlref reading, I think I figured things out...
    Using $::{'comb'} instead of $comb or '$comb' for the OPEN function.
    Using { $::{'comb'} } instead of $comb for the PRINT function.
    If I understand correctly, the use of $::{'comb'} derefences the content of the $comb variable.
    My previous use of the $comb variable, in the OPEN function worked since the string was accepted, and a filehandle was created. In the PRINT function, which expects a FILEHANDLE but will accept a { block }, the string caused a compile error when using the STRICT pragma.
    Now the following code works w/ the STRICT pragma:
    As Zaxo pointed out my previous posting had some errors, I incorrectly copied the code. I tried to trim the code to just what is needed... for it to work. I still have a long way to go...
    require("hlpsymbgen.config"); # general variables use diagnostics; use strict; open (SCINDEX, "$::inputDir\\SymbolCategories.txt") || die "Failed to Open SymbolCategories.txt. \n"; my $comb; while (<SCINDEX>){ my $cat_title = $_; my $var_c = $_; my $sufix = "_INDEX"; if ($cat_title ne 'NULL' && $var_c =~ m/::/o == 0){ $cat_title =~ s/(^[A-Z]{4})(.*)/$1/o; $cat_title =~ s/\n|\r//g; $cat_title =~ s/\s//g; $comb = $cat_title . $sufix; open ($::{'comb'}, ">$::outputDir\\" . $cat_title . "index.html" +) || die "Failure to create" . $cat_title . "\n"; print { $::{'comb'} } $var_c . "content detail for file found\n"; close $::{'comb'}; } } close SCINDEX;
    I don't know if the initial idea will work and I may end up having to use the fixed FILEHANDLE name. In the end whatever I end up with will be faster than doing by hand.
    Thanks for the help.
    joseph-