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

Dear Monks, I'm currently writing a script that streamlines the addition of print queues on multiple *nix environments. I have options setup with Getopt::Long to store and sort my <> vars. I am now trying to figure out just how I'm going to read and handle information for one of my options.
The option is theorized to parse print queue names stored on single lines in a $file. Once the -f option is called I want Perl to find the given text document and start reading. I want it to read and sort each print queue within the file and store each as an individual scalar (preferably in an array) so each queue name can be processed and added accordingly.

sub infile { open $file "< $file" or die "Cannot open \"$file\"\n$!"; while (! eof($file)) { print "Find out how to read the input file and store e +ach object on a line in an array\n"; } }

Replies are listed 'Best First'.
Re: Slurping information and storing it in an array.
by toolic (Bishop) on Jan 09, 2008 at 17:09 UTC
    If you are just trying to slurp the contents of a file into an array, where each element of the array corresponds to one line of the file, then this is one way to do it:
    #!/usr/bin/env perl use warnings; use strict; print read_file('foo.txt'); exit; sub read_file { my $file = shift; open my $fh, '<', $file or die "Can not open file '$file' $!\n"; my @all_lines = <$fh>; close $fh; return @all_lines; }

    If file "foo.txt" contains these 3 lines:

    apple banana orange

    then the script output will be:

    apple banana orange
    You can then manipulate the array contents to suit your needs.
Re: Slurping information and storing it in an array.
by alexm (Chaplain) on Jan 09, 2008 at 17:41 UTC
    use Perl6::Slurp; my $file_contents = slurp 'filename'; my @lines = slurp 'filename';
    See the details in Perl6::Slurp.