Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: split a line by control ^D character

by rjt (Deacon)
on Dec 02, 2012 at 11:38 UTC ( #1006708=note: print w/replies, xml ) Need Help??

in reply to split a line by control ^D character

Yes, you definitely want to split on /\x04/ . In addition to that, there are a few places where your code could be cleaned up a little.

You are declaring @LineDetails outside of your loop. Is there any reason for that? At least in your example, you only use it inside the loop, as a temp variable. You don't actually need the variable at all, to do what you need to do in this example.

You are sucking the entire file into an array. This can be very memory intensive, depending on the size of the file. It can also cause your program to appear to hang when it is started. Plus, you can eliminate another variable if you read the file line by line.

I would clean up everything after the open() line as follows:

use 5.010; # Allow say(). You can use print "$_\n" if you like. ... while (<$FILE>) { chomp; say for split /\x04/; }

One last thing, as a general security tip, you would be well advised to get used to using the 3 argument open, (i.e., open my $FILE, '<', $FileLocation in your case).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006708]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-06-23 12:46 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.