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

I can’t ingest an html file under mac 10.6.4 The first line is all that is read;

*!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"*

Essentially, I am reading in an ARG[0] of where the file is (an option in terminal), but the scalar variable can not hold the whole page in memory.

#!/usr/bin/perl use strict; use warnings; use diagnostics; my $fileloc = $ARGV[0]; chomp $fileloc; open(HANDLE,"$fileloc") or die "cannot open file: $!"; my $content = <HANDLE>; close(HANDLE); print "$content\n";

Replies are listed 'Best First'.
Re: mac file problem
by Kenosis (Priest) on Mar 16, 2013 at 23:37 UTC

    That's because you're only asking Perl to read one line with my $content = <HANDLE>;. If you preceeded the open with local $/; it would work, i.e., you'd slurp all the file's contents into $content, because you've set a local copy of Perl's record seperator ($/) to undef.

    However, try the following idiomatic way, too:

    #!/usr/bin/perl use strict; use warnings; use diagnostics; my $content = do { local $/; <> }; print "$content\n";

    When you've passed a file to a script on the command line for reading, you don't need to explicitely open it, as using only <> will read it.

    Hope this helps!

    BTW - For your future reference, always use the three-argument open, e.g., open my $fh, '<', $fileloc or die "Cannot open file: $!";

Re: mac file problem
by 7stud (Deacon) on Mar 17, 2013 at 06:22 UTC
    1. You are not opening files properly.
    2. You don't seem to know how to read from a file.
    3. Why would you write a program that takes a location as input, when you can't even write a program to work with a hard coded location? Hint: you should NEVER write a program that takes ANY user input until you can get the program to work WITHOUT user input.
    4. If you posted what you intended to do with the file, you would get better answers. The most common thing to do with a file is to read it line by line, but since you are reading an html file, maybe you intend to use HTML::Parser to find certain tags, and therefore you need the whole file in one string--but then HTML::Parser can be given a file name as an argument, so you wouldn't need to read the file by hand.

    Read a file line by line:

    use strict; use warnings; use 5.016; my $fname = 'data.txt'; open my $INFILE, '<', $fname or die "Couldn't open $fname: $!"; while (my $line = <$INFILE>) { chomp $line; #do something to $line; } close $INFILE or die "Couldn't close $fname: $!";

    Read the whole file into an array:

    use strict; use warnings; use 5.016; my $fname = 'data.txt'; open my $INFILE, '<', $fname or die "Couldn't open $fname: $!"; chomp(my @lines = <$INFILE>); close $INFILE or die "Couldn't close $fname: $!";

    Read the whole file into a string:

    use strict; use warnings; use 5.016; my $fname = 'data.txt'; open my $INFILE, '<', $fname or die "Couldn't open $fname: $!"; my $file; { local $/ = undef; #No line terminator, so a line #is everyting in the file $file = <$INFILE>; #Read one line. } # $/ is restored to its default value of "\n" here close $INFILE or die "Couldn't close $fname: $!";