diff options
author | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2003-12-02 22:18:05 +0000 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2003-12-02 22:18:05 +0000 |
commit | 793f5136aceac628d0f8aee41ccb49204963e443 (patch) | |
tree | 1b8078840654d2d92c6f7842a8a8a69c145348b8 /pod/perlfaq5.pod | |
parent | 9c4673c112c9c6db503869f421b813d3c489a10e (diff) | |
download | perl-793f5136aceac628d0f8aee41ccb49204963e443.tar.gz |
FAQ sync.
p4raw-id: //depot/perl@21835
Diffstat (limited to 'pod/perlfaq5.pod')
-rw-r--r-- | pod/perlfaq5.pod | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/pod/perlfaq5.pod b/pod/perlfaq5.pod index cad896d71f..be10390120 100644 --- a/pod/perlfaq5.pod +++ b/pod/perlfaq5.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq5 - Files and Formats ($Revision: 1.28 $, $Date: 2003/01/26 17:45:46 $) +perlfaq5 - Files and Formats ($Revision: 1.30 $, $Date: 2003/11/23 08:07:46 $) =head1 DESCRIPTION @@ -153,8 +153,10 @@ temporary files in one process, use a counter: =head2 How can I manipulate fixed-record-length files? -The most efficient way is using pack() and unpack(). This is faster than -using substr() when taking many, many strings. It is slower for just a few. +The most efficient way is using L<pack()|perlfunc/"pack"> and +L<unpack()|perlfunc/"unpack">. This is faster than using +L<substr()|perlfunc/"substr"> when taking many, many strings. It is +slower for just a few. Here is a sample chunk of code to break up and put back together again some fixed-format input lines, in this case from the output of a normal, @@ -162,22 +164,23 @@ Berkeley-style ps: # sample input line: # 15158 p5 T 0:00 perl /home/tchrist/scripts/now-what - $PS_T = 'A6 A4 A7 A5 A*'; - open(PS, "ps|"); - print scalar <PS>; - while (<PS>) { - ($pid, $tt, $stat, $time, $command) = unpack($PS_T, $_); - for $var (qw!pid tt stat time command!) { - print "$var: <$$var>\n"; + my $PS_T = 'A6 A4 A7 A5 A*'; + open my $ps, '-|', 'ps'; + print scalar <$ps>; + my @fields = qw( pid tt stat time command ); + while (<$ps>) { + my %process; + @process{@fields} = unpack($PS_T, $_); + for my $field ( @fields ) { + print "$field: <$process{$field}>\n"; } - print 'line=', pack($PS_T, $pid, $tt, $stat, $time, $command), - "\n"; + print 'line=', pack($PS_T, @process{@fields} ), "\n"; } -We've used C<$$var> in a way that forbidden by C<use strict 'refs'>. -That is, we've promoted a string to a scalar variable reference using -symbolic references. This is okay in small programs, but doesn't scale -well. It also only works on global variables, not lexicals. +We've used a hash slice in order to easily handle the fields of each row. +Storing the keys in an array means it's easy to operate on them as a +group or loop over them with for. It also avoids polluting the program +with global variables and using symbolic references. =head2 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? |