in reply to Problem emulating built-ins like map and grep.

For what it is worth your sub works both ways using Perl 5.8. I tried it using the activestate distribution:
use strict; sub map2 (&@) { use Carp; my $code = shift; croak 'Odd number of values in list' if @_ & 1; map { local ($a, $b) = (shift,shift); $code->() } 1 .. (@_>>1); } my @array = ('A'..'J'); print 'PRINTING map2{ print "$a $b"; } @array; =>'; map2{ print "$a $b"; } @array; print "\n",'PRINTING map2{ print "$a $b"; } qw[A B C D]; =>'; map2{ print "$a $b"; } qw[A B C D E F G H I J];
resulting in
E:\>228837.pl PRINTING map2{ print "$a $b"; } @array; =>A BC DE FG HI J PRINTING map2{ print "$a $b"; } qw[A B C D]; =>A BC DE FG HI J
and also using Perl 5.8 (built from source on a SCO OSR5.5):
#!/usr/bin/perl -slw use strict; sub map2 (&@) { use Carp; my $code = shift; croak 'Odd number of values in list' if @_ & 1; map { local ($a, $b) = (shift,shift); $code->() } 1 .. (@_>>1); } my @array = ('A'..'J'); print 'PRINTING map2{ print "$a $b"; } @array; =>'; map2{ print "$a $b"; } @array; print "\n",'PRINTING map2{ print "$a $b"; } qw[A B C D]; =>'; map2{ print "$a $b"; } qw[A B C D E F G H I J];
resulting in:
{sco}>./228837.pl PRINTING map2{ print "$a $b"; } @array; => A B C D E F G H I J PRINTING map2{ print "$a $b"; } qw[A B C D]; => A B C D E F G H I J
update:I have just tried it on a RH Linux Box and as expected works one way and not the other (Perl 5.6.1).

Hope this helps.

-enlil

Replies are listed 'Best First'.
Re: Re: Problem emulating built-ins like map and grep.
by BrowserUk (Patriarch) on Jan 21, 2003 at 22:05 UTC

    Thanks Enlil. Time to upgrade I guess, but I thought I heard there were problems with the current AS distribution, and my attempts to build perl have ground to a halt so far.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.