in reply to Printing an array

Looking at the code from a slightly different perspective from the other posters, I'd say that you just need to place the print "@DATA"; line inside of your readdata() routine just before the return @DATA; line.

But everything the others have said is true too. You haven't called any of these subroutines (at least in the bit of code you showed us) and you haven't declared a variable called @DATA with file scope (you did declare block-scoped variables named @DATA within each of the subroutines but, because they are lexically scoped to the subroutines, they aren't available outside of the subroutines)

Replies are listed 'Best First'.
Re: Re: Printing an array
by Ovid (Cardinal) on Nov 23, 2003 at 01:43 UTC
    I'd say that you just need to place the print "@DATA"; line inside of your readdata() routine

    The problem with this solution is that we no longer have a "readdata" routine. Instead, we have a routine that should probably be called "read_and_print_data" (or something like that). Subroutines generally should not have side effects like this without good reason. Admittedly, for such a small program it doesn't matter as much, but when the programmer wants to later create a more generalized routine, side effects will cause problems. For example, I almost changed it to this:

    sub read_data { my $file = shift; open FH, '<', $file or die "Cannot open ($file) for reading: $!"; chomp (my @data = <FH>); close FH; return wantarray ? @data : \@data; }

    This routine is a nice little black box that returns an array (or reference) to the lines contained in the file. If you wanted to print out the contents, putting this functionality in the subroutine means that this routine is tough to reuse if you want to just read a file and not print the contents (of course, some might question the use of chomp, given that argument).

    Cheers,
    Ovid

    New address of my CGI Course.