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

I must be missing something subtle... or just being a nini and not seeing the obvious.
Help would be greatly appreciated, I think I need to dereference correctly the FILEHANDLE in the print function vs the open function. ?
Particulars
ActivePerl Build 616
Perl version : v5.6.0 built for MSWin32-x86-multi-thread
Goal:
Read source file on specific content, create a new file whose name is composed of part a = 'source content' and part b = 'index'. Then print remaining content read from source to a new file, also keep the new file open in order to do other processing based on file name and/or content, then print additional processed info to the new file and finally close the new file. How is the following possible when not using the strict pragma? Yet when using strict pragma, the code fails to work !?!?

Sample code:
use diagnostics; use strict; open (FILE, "c:\\my\\dir\\src.file.txt") || die "failed to open src file"; while (<FILE>){ my $var_a = $_; my $var_b = "_INDEX"; my $var_c = $_; $var_a =~ s/(^[A-Z]{4})(.*)/$1/o; $comb = $var_a . $var_b; open ($comb, "c:\\my\\dest\\dir\\" . $comb . "found.txt") || die "failure to create" . $comb; print $comb $var_c . "content detail for file found\n"; } close FILE; #_## do some other functions that add more information to file. close $comb.
Using the strict pragma, I have found that I need to do some editing to the open function. Yet the print function alludes me.
WORKS: open ('$comb', "c:\\my\\dest\\dir\\" . $comb . "found.txt") Unable to figure out the print function. ERROR: print $comb $var_c . "content detail for file found\n";
ERROR output: Can't use string ("ACFT_INDEX") as a symbol ref while "strict refs"
Trying to correct this error, the symbolic reference, only has lead to may other but very similar errors.
If I use a fixed FILEHANDLE this works when using the strict pragma and this might be acceptable, not finished with the project yet. I just need to understand why it works when not using the strict pragma. Call it curiosity.
Any help figuring out how to correctly do this would be great.
joseph-

Replies are listed 'Best First'.
Re: How do you properly use a variable for FILEHANDLE in print vs open
by chromatic (Archbishop) on Jul 03, 2004 at 00:00 UTC

    Is there a reason you need a named filehandle? Reusing the same variable for the filehandle and the file name seems to be tripping you up. I'd write:

    while (<FILE>) { my $var_a = $_; my $var_b = "_INDEX"; my $var_c = $_; $var_a =~ s/(^[A-Z]{4})(.*)/$1/; $comb = $var_a . $var_b; open (my $fh, "c:\\my\\dest\\dir\\" . $comb . "found.txt") or die "failure to create '$comb': $!\n"; print $fh $var_c . "content detail for file found\n"; }
Re: How do you properly use a variable for FILEHANDLE in print vs open
by Zaxo (Archbishop) on Jul 03, 2004 at 00:06 UTC

    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

      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-