Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: [SOLVED] Does split not work with a pipe?

by Tanktalus (Canon)
on Nov 10, 2012 at 14:37 UTC ( [id://1003264]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1003264]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found