On Mon, Feb 09, 2009 at 12:41:07PM -0000, Taylor, Andrew (ASPIRE) wrote:
> Hello
> 
> I'm processing a file of test data that looks something like:
> 
> FH1,data1,data2,data3,...etc
> FH2,data1,data2,data3,...etc
> FH1,data1,data2,data3,...etc
> 
> Each line split into an array and processed.
> 
> The first element (FH1, FH2, etc) is the name of the filehandle the
> output should be printed to.
> 
> I'm trying to do something like:
> 
> use strict;
> use warnings;
> 
> open (FH1, ">file1");
> open (FH2, ">file2");
> 
> open (INPUT, "<inputfile");
> 
> while (<INPUT>)
> {
> ...split line
> ...process data
> ...create output
> 
>   my $FH = $split_line[0]
> 
>   print $FH "$output\n";
> 
> }
> 
> But I get the error:
> "Can't use string ("FH1") as a symbol ref while "strict refs" in use at
> my_script.pl line 387, <INPUT> line 1."

You are using "strict refs", which is a good idea in general.  But then
you are trying to use a symbolic ref, which you explicitly said you
wouldn't do.

The solution therefore, is to allow symbolic refs where you are trying
to use them.  This can be done by placing <no strict "refs";> just above
your print statement.

> The closest thing I've found is the following from the perldocs website:
> 
>       $fh =   SOME_FH;       # bareword is strict-subs hostile
>       $fh =  "SOME_FH";      # strict-refs hostile; same package only
>       $fh =  *SOME_FH;       # typeglob
>       $fh = \*SOME_FH;       # ref to typeglob (bless-able)
>       $fh =  *SOME_FH{IO};   # blessed IO::Handle from *SOME_FH
> typeglob
> 
> But I don't think that answers my question (or if it does, I'm too dumb
> to see that answer...)

The clue is on the second line.

The whole thing does seem a little fragile though, with such a tight
connection between the internals of your program and the data in your
file.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to