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

I am a newbie. Would you guys explain what the following code does. Thank you very much.
sub load_array($$) <br> { <br> my ($a_ref, $file) = @_;<br> open (FILE, $file) or die "Can't open $file:$!\n";<br> @{$a_ref} = <FILE>; <br> close FILE; <br. chomp @{$a_ref}; }

Replies are listed 'Best First'.
(Ovid) Re: Explaining
by Ovid (Cardinal) on Aug 31, 2000 at 19:30 UTC
    Looks like you cut and paste this from some HTML. Here's the cleaned up version:
    sub load_array($$) { my ($a_ref, $file) = @_; open (FILE, $file) or die "Can't open $file:$!\n"; @{$a_ref} = <FILE>; close FILE; chomp @{$a_ref}; }
    What this does is pass and array reference and a filename to a sub. The file is opened and all lines of the file are read into the array. The final chomp removes the newlines from the array.

    Cheers,
    Ovid

    Update: I missed the ($$) on the sub call. In recent versions of Perl, this creates what is called a prototype and specifies the types of arguments to pass. This should allow you to call the subroutine just as you would call in a built-in function and it will behave like a built-in function. I've not had much use for them (but then, I've not had much use for map until recently).

Re: Explaining
by Shendal (Hermit) on Aug 31, 2000 at 19:34 UTC
    # declare a subroutine # takes two strings as arguments sub load_array($$) { # assign the two strings to two local variables, $a_ref and $file # so, if called as &load_array('foo','bar'), $a_ref eq 'foo' and # $file eq 'bar'. my ($a_ref, $file) = @_; # opens the file for read-only access # if the open fails, the program exits with the supplied message open (FILE, $file) or die "Can't open $file:$!\n"; # the array pointed to by $a_ref is assigned the contents # of the file @{$a_ref} = <FILE>; # the file is closed close FILE; # newlines are removed from each element of the array chomp @{$a_ref}; }
    Looks to me that this code is designed to be used like so:
    my($ref) = \@array; my($file) = '/usr/dict/words'; &load_array($ref,$file); # $ref now contains a reference to an array which contains # all the words in /usr/dict/words, with the newlines # removed.

    Cheers,
    Shendal
Re: Explaining
by gaspodethewonderdog (Monk) on Aug 31, 2000 at 19:46 UTC
    well the code loads a file into an array line by line. However it certainly isn't the most optimal way to do this. At least I certainly wouldn't do it this way.
    sub load_array($$) { #the first element we pass to this function is a #array reference, the second is the name of a file my ($a_ref, $file) = @_; #attempt to open the file or quit executing our program open (FILE, $file) or die "Can't open $file:$!\n"; #take all the lines in the file, and put them into the #array that $a_ref points to. @{$a_ref} = <FILE>; # close the file... duh I guess close FILE; # remove all of the carriage returns from the end of # each element in the array chomp @{$a_ref}; } # to call this routine my @array; &load_array(\@array, # pass in the reference to the array "test.txt"); # the name of our file
    Personally I think I would rewrite this to do something like:
    sub load_array { my $file = shift; # the first parameter is our file my @array; open(IN, $file) or die "Can't open $file: $!\n"; # for every line in the file... while(<IN>) { chomp; # remove the trailing carriage return # could also be chomp $_; push @array; # put it into our array # could also be push @array, $_; } # while close(IN); return @array; } # load_array # to run this routine... my @array = &load_array("test.txt");
Re: Explaining
by ncw (Friar) on Aug 31, 2000 at 19:32 UTC
    Short answer - it loads a file into an array.

    Long answer, call it like this

    my @lines; load_array(\@lines, "file_name"); # now do something with @lines.
    As a point of principle the close FILE should have an or die clause attached...
Re: Explaining
by Anonymous Monk on Aug 31, 2000 at 19:42 UTC
    Thank you all. This site is great!
RE: Explaining
by Anonymous Monk on Aug 31, 2000 at 19:30 UTC
    Please ignore
    part.Thank you. Sorry
Re: Explaining
by agoth (Chaplain) on Aug 31, 2000 at 19:36 UTC
    dies horribly whilst complaining?