Running and Debugging Perl
By default, -a splits on spaces – although you can change it by specifying another switch, -F, which will take your chosen delimiter as an argument. For instance, the fields in a UNIX password file are (as we saw in Chapter 5), delimited by colons. We can extract the home directory from the file by looking at the fifth element of the array. If our passwd file contains the line:
We'll get the following result:
>perl -F: -ane 'print $F,"\n" if /^simon/' passwd /home/simon >
l and –0
It's rather annoying to have to specify "\n" on the end of everything we print, just to get a new line, especially if we're doing things on the command line. The -l switch sets the output record separator $\ equal to the current value of the input record separator $/. The former is added on automatically at the end of every print statement. Since the latter is usually \n, the newline character, -l adds a newline to everything we print. Additionally, if used with the -n or -p switches, it will automatically chomp any input.
We can cut the above program down by a few more keystrokes like this:
>perl -F: -lane 'print $F if /^simon/' passwd /home/simon >
If –l is followed by a valid octal number, then the character with that ASCII value (see Appendix F) is used as the output record separator instead of new line. However, this is relatively rare.
Alternatively, you can set the input record separator using the -0 switch. Likewise, if this is followed by an octal number, $/ will be set to that character. For instance, -0100 will effectively execute $/="A"; at the beginning of the program. -0 on its own or followed by something that isn't an octal number will cause $/ to be set to the undefined value, causing the entire file to be read in at once:
While you can conceivably use -l on the shebang line to save printing newlines in your program, it's actually a bad idea – many people will probably miss it and wonder where all the new lines are coming from. It will also get you into trouble if you want a print statement that doesn't cause a newline.
When you're dealing with data that's being downloaded from an unreliable source from the outside world, you'll probably want to be careful what you do with it. If you're asking the user for a filename, which you pass directly to open, you're potentially allowing the user to do all kinds of horrible things. Say, for instance, you were given the filename rm -rf /| and used it as it was (DON'T!). You may well find afterwards that several of the files on your disk had disappeared...