develooper Front page | perl.perl5.porters | Postings from September 2012

[perl #114732] extension to for

Thread Next
From:
perl-mail @ duboulder . com
Date:
September 4, 2012 18:59
Subject:
[perl #114732] extension to for
Message ID:
rt-3.6.HEAD-11172-1346719685-894.114732-75-0@perl.org
# New Ticket Created by  perl-mail@duboulder.com 
# Please include the string:  [perl #114732]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org:443/rt3/Ticket/Display.html?id=114732 >



This is a bug report for perl from perl-mail@duboulder.com,
generated with the help of perlbug 1.39 running under perl 5.12.4.


Is it reasonable to enhance the for loop to include multiple variables:
    for my ($var1, $var2, $var3) (@list) {
    }

Where the iteration is quivalent to the block:
    {
        my ($var1, $var2, $var3);
        my @temp = @list;
        while (@temp) {
            my $var1 = shift @temp; 
            my $var2 = shift @temp; 
            my $var3 = shift @temp;
        }
    }

A related item would be having something like
    sub mapnatatime(&$@) {
        my @list;
        for (my $i=2: i<@_; $i+=$_[1]) {
            push @list, $_[0]->(@_[$i..($_[1]-1)]);
        }
        return @list;
    }
as a standard function to have map capabilities be 
similar to for.

A use case where the loop is useful is:
    our @data;
    BEGIN {
        @data = (
            $value1a, $value1b, $value1c,
            $value2a, $value2b, $value2c,
            qw( const1 const2 const3 )
        );
    }
    
    sub x {

        ...

        for (my $i=0; $i<@data; $i+=3) {
            my ($var1, $var2, $var3) = @data[$i..($i+2)];
            some_function ($var1+1, $var3, $some_expr)
                unless $_[0] eq $var2;
        }

    }

This would become:
    sub x {

        ...

        for my ($var1, $var2, $var3) (
            $value1a, $value1b, $value1c,
            $value2a, $value2b, $value2c,
            qw( const1 const2 const3 )
        ) {
            some_function ($var1+1, $var3, $some_expr)
                unless $_[0] eq $var2;
        }
    }

I think it makes some data-driven coding-implementations easier and clearer
by eliminating boilerplate that isn't important to understanding the code.

---
Flags:
    category=core
    severity=wishlist
---
Site configuration information for perl 5.12.4:

Configured by Gentoo at Sat Mar 17 23:22:44 MDT 2012.

Summary of my perl5 (revision 5 version 12 subversion 4) configuration:
   
  Platform:
    osname=linux, osvers=3.2.11-gentoo, archname=x86_64-linux
    uname='linux www.duboulder.com 3.2.11-gentoo #2 smp sat mar 17 22:32:34 mdt 2012 x86_64 intel(r) celeron(r) cpu e3400 @ 2.60ghz genuineintel gnulinux '
    config_args='-des -Duseshrplib -Darchname=x86_64-linux -Dcc=x86_64-pc-linux-gnu-gcc -Doptimize=-O2 -march=native -pipe -fomit-frame-pointer -Dldflags=-Wl,-O1 -Wl,--as-needed -Dprefix=/usr -Dsiteprefix=/usr -Dvendorprefix=/usr -Dscriptdir=/usr/bin -Dprivlib=/usr/lib64/perl5/5.12.4 -Darchlib=/usr/lib64/perl5/5.12.4/x86_64-linux -Dsitelib=/usr/lib64/perl5/site_perl/5.12.4 -Dsitearch=/usr/lib64/perl5/site_perl/5.12.4/x86_64-linux -Dvendorlib=/usr/lib64/perl5/vendor_perl/5.12.4 -Dvendorarch=/usr/lib64/perl5/vendor_perl/5.12.4/x86_64-linux -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/share/man/man1 -Dsiteman3dir=/usr/share/man/man3 -Dvendorman1dir=/usr/share/man/man1 -Dvendorman3dir=/usr/share/man/man3 -Dman1ext=1 -Dman3ext=3pm -Dlibperl=libperl.so.5.12.4 -Dlocincpth=  -Duselargefiles -Dd_semctl_semun -Dcf_by=Gentoo -Dmyhostname=localhost -Dperladmin=perl@localhost -Dinstallusrbinperl=n -Ud_csh -Uusenm -Di_ndbm -Di_gdbm -Ui_db -DDEBUGGING=none 
 -Dinc_version_list=5.12.3/x86_64-linux 5.12.3 5.12.2/x86_64-linux 5.12.2 5.12.1/x86_64-linux 5.12.1 5.12.0/x86_64-linux 5.12.0  -Dlibpth=/usr/local/lib64 /lib64 /usr/lib64'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=undef, usemultiplicity=undef
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='x86_64-pc-linux-gnu-gcc', ccflags ='-fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2 -march=native -pipe -fomit-frame-pointer',
    cppflags='-fno-strict-aliasing -pipe'
    ccversion='', gccversion='4.5.3', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='x86_64-pc-linux-gnu-gcc', ldflags ='-Wl,-O1 -Wl,--as-needed'
    libpth=/usr/local/lib64 /lib64 /usr/lib64
    libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
    libc=/lib/libc-2.14.so, so=so, useshrplib=true, libperl=libperl.so.5.12.4
    gnulibc_version='2.14'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -march=native -pipe -fomit-frame-pointer -Wl,-O1 -Wl,--as-needed'

Locally applied patches:
    0001-gentoo_MakeMaker-RUNPATH.diff
    0002-gentoo_config_over.diff
    0003-gentoo_cpan_definstalldirs.diff
    0004-gentoo_cpanplus_definstalldirs.diff
    0005-gentoo_create-libperl-soname.diff
    0006-gentoo_MakeMaker-delete_packlist.diff
    0007-fixes_8d66b3f9_h2hp_fix.diff
    0008-fixes_f178b03b_h2ph_using_deprecated_goto.diff
    0009-gentoo_mod-paths.diff
    0010-gentoo_enc2xs.diff
    0011-gentoo_IO-Compress_AutoLoader_dropped_from_Compress-Zlib.diff
    0012-gentoo_drop-fstack-protector.diff

---
@INC for perl 5.12.4:
    /etc/perl
    /usr/lib64/perl5/site_perl/5.12.4/x86_64-linux
    /usr/lib64/perl5/site_perl/5.12.4
    /usr/lib64/perl5/vendor_perl/5.12.4/x86_64-linux
    /usr/lib64/perl5/vendor_perl/5.12.4
    /usr/lib64/perl5/site_perl
    /usr/lib64/perl5/vendor_perl
    /usr/lib64/perl5/5.12.4/x86_64-linux
    /usr/lib64/perl5/5.12.4
    /usr/local/lib/site_perl
    .

---
Environment for perl 5.12.4:
    HOME=/home/mail
    LANG=en_US.UTF-8
    LANGUAGE (unset)
    LC_COLLATE=C
    LC_CTYPE=en_US.UTF-8
    LC_MESSAGES=en_US.UTF-8
    LC_MONETARY=en_US.UTF-8
    LC_NUMERIC=en_US.UTF-8
    LC_PAPER=en_US.UTF-8
    LC_TIME=en_US.UTF-8
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.5.4
    PERL_BADLANG (unset)
    SHELL=/bin/bash


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About