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

so what im trying to do is open up 4 files, whether they exist or not, and put in a scalar string into each. i encounter the error "Can't use string (">poo.txt") as a subroutine ref while "strict refs" in use at ./2.pl line 12. " i have tried to call on to qw() with the FILE1, FILE2, etc etc within and calling the array index to the filename but then i get the same error only instead of >poo.txt its FILE1

#!/usr/bin/perl use strict; my $scalar = "We Be Killing Zombies Tonite"; my @array = (">poo.txt", ">moo.txt", ">doo.txt". ">goo.txt"); open(FILE1, $array[0]) or die "uno problemo"; open(FILE2, $array[1]); open(FILE3, $array[2]); open(FILE4, $array[3]); foreach(@array){ print $array[$_] ("$scalar"); }

im not looking for a blunt answer to just fix the problem and move on with my programming exercises, id like to learn why this doesnt work, so i can better understand how i can fix a problem like this in the future.. thanks for all your help guys!

Replies are listed 'Best First'.
Re: Output To File with Array help
by ikegami (Patriarch) on Aug 19, 2009 at 18:06 UTC
    Re-read the docs for print. Start at the bottom.

    id like to learn why this doesnt work

    Limitations in the parser. It needs to know whether what follows print is a file handle or not in short order. This limits what can be used as a file handle.

      yeah i had it working with 4 different print statements, but i wanted to see if i could use my head and get it to run more effecient with an array running thru the array index. you telling me that ill be unable to use an array as a filehandle period? or will i have to deliminate it somehow so the print function sees that its indeed a reference to a real filehandle?
        You really need to read the docs he linked to. Starting at the bottom.
        A reply falls below the community's threshold of quality. You may see it by logging in.

        you telling me that ill be unable to use an array as a filehandle period?

        No. Please follow the instructions I gave.

        Update: Toned down my answer. My annoyance was showing.

Re: Output To File with Array help
by jwkrahn (Abbot) on Aug 19, 2009 at 18:09 UTC
    #!/usr/bin/perl use warnings; use strict; my $scalar = 'We Be Killing Zombies Tonite'; my @array = qw( poo.txt moo.txt doo.txt goo.txt ); my @handles; for my $name ( @array ) { open my $FH, '>', $name or die "Cannot open '$name' $!"; push @handles, $FH; } for my $FH ( @handles ) { print $FH $scalar; }
Re: Output To File with Array help
by markkawika (Monk) on Aug 19, 2009 at 18:24 UTC

    One of your problems is that you're attempting to print to a scalar value containing a string instead of a scalar value containing a file handle.

    The first argument to print should be a file handle, not a string.

    If you wanted to loop over an array of file handles and print the same output to each, you could try this:

    use strict; my $scalar = "We Be Killing Zombies Tonite"; my @array = (">poo.txt", ">moo.txt", ">doo.txt", ">goo.txt"); my @filehandle; open($filehandle[0], $array[0]); open($filehandle[1], $array[1]); open($filehandle[2], $array[2]); open($filehandle[3], $array[3]); for my $fh (@filehandle) { print $fh "$scalar"; }