[comp.lang.perl] diffs from last month's FAQ

tchrist@convex.com (Tom Christiansen) (05/02/91)

Almost nothing this time.

--tom

1c1
< [Last changed: $Date: 91/04/08 17:18:55 $ by $Author: tchrist $]
---
> [Last changed: $Date: 91/05/01 22:59:15 $ by $Author: tchrist $]
126c126
<           osu-cis!~/perl/3.0/kits@44/perl.kitXX.Z (XX=01-33)
---
>           osu-cis!~/perl/4.0/kits@0/perl.kitXX.Z (XX=01-36)
461c461
<       $found = $list{$pattern};
---
>       $found += $list{$pattern};
479c479
<       @pats = ('_get.*', 'bogus', '_read', '.*exit');
---
>       @pats = ('_get.*', 'bogus', '_read', '.*exit', '_write');
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time." 

tchrist@convex.com (Tom Christiansen) (06/03/91)

I've fixed a couple JPL references and added three questions that seems to
come up now and again.  I decided not to put in, ``Where can I get the
book's scripts?'' since that's in question 4 (although maybe folk skip over
it there.)  

--tom

51a52,54
>     30)  How can I use Perl interactively?
>     31)  How do a sort an associative array by value instead of by key?
>     32)  How can I capture STDERR from an external command?
60c63
<     A programming language, by Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
---
>     A programming language, by Larry Wall <lwall@netlabs.com>.
201,205c204,210
<     If you can't find the book in your local technical bookstore, the book may
<     be ordered directly from O'Reilly by calling 1-800-dev-nuts.  Autographed
<     copies are available from TECHbooks by calling 1-503-646-8257 or mailing
<     info@techbook.com.  Cost is ~25$US for the regular version, 35$US
<     for the special autographed one.
---
>     If you can't find the book in your local technical bookstore, the book
>     may be ordered directly from O'Reilly by calling 1-800-dev-nuts if in
>     North America (that's 1-800-338-6887 for those poor folks without
>     handy mnemonic numbers on their phones) and 1-707-829-0515.
>     Autographed copies are available from TECHbooks by calling
>     1-503-646-8257 or mailing info@techbook.com.  Cost is ~25$US for the
>     regular version, 35$US for the special autographed one.
231,232c236
<     the best place for the very latest information on Perl, unless perhaps
<     you should happen to work at JPL. 
---
>     the best place for the very latest information on Perl.
1092a1097,1194
> 
> 30) How can I use Perl interactively?
>     
>     While some folks have written little wrappers to do this, and there's
>     even a "perlsh" included in the Perl distribution, probably the
>     easiest way is simply to invoke the debugger on an empty program:
> 
> 	perl -de 0 
> 
>     And then you can type in all the Perl you want, examine the symbol
>     table, and in general do much of what you're looking for.
> 
> 31) How do I sort an associative array by value instead of by key?
> 
>     You have to declare a sort subroutine to do this.  Let's assume
>     you want an ASCII sort on the values of the associative array %ary.
>     You could do so this way:
> 
> 	foreach $key (sort by_value keys %ary) {
> 	    print $key, '=', $ary{$key}, "\n";
> 	} 
> 	sub by_value { $ary{$a} cmp $ary{$b}; }
> 
>     If you wanted a descending numeric sort, you could do this:
> 
> 	sub by_value { $ary{$b} <=> $ary{$a}; }
> 
>     If you wanted a function that didn't have the array name hard-wired
>     into it, you could so this:
> 
> 	foreach $key (&sort_by_value(*ary)) {
> 	    print $key, '=', $ary{$key}, "\n";
> 	} 
> 	sub sort_by_value {
> 	    local(*x) = @_;
> 	    sub _by_value { $x{$a} cmp $x{$b}; } 
> 	    sort _by_value keys %x;
> 	} 
> 
>     If you want neither an alphabetic nor a numeric sort, then you'll 
>     have to code in your own logic instead of relying on the built-in
>     signed comparison operators "cmp" and "<=>".
> 
>     Note that if you're sorting on just a part of the value, such as a
>     piece you might extract via split, unpack, pattern-matching, or
>     substr, then rather than performing that operation inside your sort
>     routine on each call to it, it is significantly more efficient to
>     build a parallel array of just those portions you're sorting on, sort
>     the indices of this parallel array, and then to subscript your original
>     array using the newly sorted indices.  This method works on both
>     regular and associative arrays, since both @ary[@idx] and @ary{@idx}
>     make sense.  See page 245 in the Camel Book on "Sorting an Array by a
>     Computable Field" for a simple example of this.
> 
> 
> 32) How can I capture STDERR from an external command?
> 
>     There are three basic ways of running external commands:
> 
> 	system $cmd;
> 	$output = `$cmd`;
> 	open (PIPE, "cmd |");
> 
>     In the first case, both STDOUT and STDERR will go the same place as
>     the script's versions of these, unless redirected.  You can always put
>     them where you want them and then read them back when the system
>     returns.  In the second and third case, you are reading the STDOUT
>     *only* of your command.  If you would like to have merged STDOUT and
>     STDERR, you can use shell file-descriptor redirection to dup STDERR to
>     STDOUT:
> 
> 	$output = `$cmd 2>&1`;
> 	open (PIPE, "cmd 2>&1 |");
> 
>     Another possibility is to run STDERR into a file and read the file 
>     later, as in 
> 
> 	$output = `$cmd 2>some_file`;
> 	open (PIPE, "cmd 2>some_file |");
>     
>     Here's a way to read from both of them and know which descriptor
>     you got each line from.  The trick is to pipe only STDERR through
>     sed, which then marks each of its line, and then sends that
>     back into a merged STDOUT/STDERR stream, from which your Perl program
>     then reads a line at a time:
> 
>         open (CMD, 
>           "3>&1 (cmd args 2>&1 1>&3 3>&- | sed 's/^/STDERR:/' 3>&-) 3>&- |");
> 
>         while (<CMD>) {
>           if (s/^STDERR://)  {
>               print "line from stderr: ", $_;
>           } else {
>               print "line from stdout: ", $_;
>           }
>         }
> 
> 
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
	    "Perl is to sed as C is to assembly language."  -me