in reply to List-to-Scalar Function?

use List::Util qw(reduce); # from the CPAN my $max = reduce { $a > $b ? $a : $b } @list; my $string = reduce {$a . $b } @list;
Your third one is problematic. "null" in what sense? And yes, $~ does already have a meaning. Everything means something. That's why they used up almost all the control-character names and are now started on the multicharacter names in 5.6.1.

-- Randal L. Schwartz, Perl hacker


update: That third one is:
my $count = reduce { $a + $b =~ /the/ } 0, <FILE>;

Replies are listed 'Best First'.
Re (tilly) 2: List-to-Scalar Function?
by tilly (Archbishop) on May 09, 2001 at 03:27 UTC
    You can also implement reduce in native Perl:
    sub reduce (&@) { my $op = shift; no strict 'refs'; my $pkg = caller; local *A = local *{"$pkg\::a"}; $A = shift; local *B = local *{"$pkg\::b"}; foreach $B (@_) { $A = $op->(); } $A; }
    (Stolen shamelessly from here, with full permission of the author. :-)

    UPDATE
    D'oh. I should have read the source-code. They don't present that in the documentation... And I wrote it originally because someone (jcwren I think) was having trouble running my code at Re (tilly) 1: An exercise in "That's not the way I'd do it" (TNTWIDI) because List::Util wasn't compiling.

      Somewhat similar to the pure-Perl implementation in List::Util itself...
      use vars qw($a $b); sub reduce (&@) { my $code = shift; return shift unless @_ > 1; my $caller = caller; local(*{$caller."::a"}) = \my $a; local(*{$caller."::b"}) = \my $b; $a = shift; foreach (@_) { $b = $_; $a = &{$code}(); } $a; }
      Although if the XS compiles and loads, this code is not used.

      -- Randal L. Schwartz, Perl hacker

Re: Re: List-to-Scalar Function?
by MrNobo1024 (Hermit) on May 09, 2001 at 02:36 UTC
    One of the old-style variable names is still unused... It's $}