First comment. Don't use DATA as your filehandle. That filehandle already exists and is used for something different.

Second, use lexical filehandles instead of globals. open my $data, '<', 'IN-accounting.data'

Third, check that the open succeeded. Especially when you're dealing with a filename that isn't an absolute path, the current working directory (cwd) could change simply because the user called your script from the wrong directory. open my $data, '<', 'IN-accounting.data' or die "Can't read IN-accounting.data: $!"

Fourth, use Text::CSV_XS to read delimited data rather than splitting. It handles edge cases for you. It also handles the first three comments, too ;-)

Fifth, use DBD::CSV to handle data stores like this. IMO, treating tables of data as, well, tables of data, generally works better. It also uses Text::CSV_XS under the covers.

Ok, now on to some meta-comments (comments on the comments). I see this is homework. Thank you for not hiding that fact ;-) . Your professor may not like comments 4 and 5 above. But the first three are still valid, even for homework. The only reason 4 and 5 might be disliked by your professor is that the goal of the excersise is not to actually accomplish anything, but to learn stuff. (Which was always one of the things I disliked about school - I prefer to learn stuff through accomplishing things.) And Text::CSV_XS will bypass learning split, and DBD::CSV will bypass learning Text::CSV_XS.

Oh, a sixth coment, I just noticed: Perl has many quoting operators so that you can avoid (or at least minimise) LTS (leaning toothpick syndrome). So, instead of die ("\"$username\" does not match any users in our database!\n");, I'd suggest using the qq operator and ending up with: die (qq["$username" does not match any users in our database!\n]);. See how I've removed two backslashes (leaning toothpicks)? Note that you can do die (qq'"$username" does not match any users in our database!\n'); as well, but don't - the use of single quotes as a double-quote delimiter will get you strangled :-)


In reply to Re: [SOLVED] Does split not work with a pipe? by Tanktalus
in thread [SOLVED] Does split not work with a pipe? by jaffinito34

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.