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: $!";

In reply to Re: mac file problem by 7stud
in thread mac file problem by ritz0

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.