http://www.perlmonks.org?node_id=1003264


in reply to [SOLVED] Does split not work with a pipe?

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 :-)