develooper Front page | perl.perl5.porters | Postings from January 2004

[perl #24793] Warning for logical short-circuit at compile time

From:
perlbug-followup
Date:
January 2, 2004 20:05
Subject:
[perl #24793] Warning for logical short-circuit at compile time
Message ID:
rt-3.0.7_01-24793-69377.15.8433851531183@perl.org
# New Ticket Created by  avised@kbcfp.com 
# Please include the string:  [perl #24793]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=24793 >



This is a bug report for perl from avised@kbcfp.com,
generated with the help of perlbug 1.34 running under perl v5.8.0.


-----------------------------------------------------------------
[Please enter your report here]

The logical operators like && and || have short-circuiting behaviour.
This is sometimes optimized away at compile time to a constant value
when perl can determine that the LHS is is true or false.  While this
optimization is correct, it can be a signal that the programmer made a
mistake, because normally you don't write logical tests where the
result is known at compile time.

For example, this is a mistake that caused some bother:

    my $fh = new FileHandle "< $fileName" || die;

The precedence of || means that the code is equivalent to

    my $fh = new FileHandle ("< $fileName" || die);

optimized at compile time to

    my $fh = new FileHandle "< $fileName";

It's unlikely the programmer intended to write an || expression which
could be short-circuited in this way before the program runs.
Therefore, it would be a good idea to add a warning.

There are some uses for logical expressions known at compile time, for
example when dealing with global flags:

    use constant TWIDDLE => 1;
    if (TWIDDLE || $x) { ... }

So probably the warning should be only if the constant expression on
the LHS is a string rather than a number or reference.

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=wishlist
---
Site configuration information for perl v5.8.0:

Configured by bhcompile at Sun Sep  1 23:55:07 EDT 2002.

Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.4.18-11smp, archname=i386-linux-thread-multi
    uname='linux daffy.perf.redhat.com 2.4.18-11smp #1 smp thu aug 15 06:41:59 edt 2002 i686 i686 i386 gnulinux '
    config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686 -Dmyhostname=localhost -Dperladmin=root@localhost -Dcc=gcc -Dcf_by=Red Hat, Inc. -Dinstallprefix=/usr -Dprefix=/usr -Darchname=i386-linux -Dvendorprefix=/usr -Dsiteprefix=/usr -Duseshrplib -Dusethreads -Duseithreads -Duselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Ui_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Duseperlio -Dinstallusrbinperl -Ubincompat5005 -Uversiononly -Dpager=/usr/bin/less -isr'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
    optimize='-O2 -march=i386 -mcpu=i686',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -I/usr/include/gdbm'
    ccversion='', gccversion='3.2 20020822 (Red Hat Linux Rawhide 3.2-5)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lgdbm -ldb -ldl -lm -lpthread -lc -lcrypt -lutil
    perllibs=-lnsl -ldl -lm -lpthread -lc -lcrypt -lutil
    libc=/lib/libc-2.2.92.so, so=so, useshrplib=true, libperl=libperl.so
    gnulibc_version='2.2.92'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic -Wl,-rpath,/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    

---
@INC for perl v5.8.0:
    /u/avised/lib/perl5/5.8.0/i386-linux-thread-multi
    /u/avised/lib/perl5/5.8.0
    /u/avised/lib/perl5/5.6.1
    /u/avised/lib/perl5
    /u/avised/lib/perl5/5.8.0/i386-linux-thread-multi
    /u/avised/lib/perl5/5.8.0
    /u/avised/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi
    /u/avised/lib/perl5/site_perl/5.8.0
    /u/avised/lib/perl5/site_perl/5.8.0/i386-linux
    /u/avised/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi
    /u/avised/lib/perl5/site_perl/5.8.0
    /u/avised/lib/perl5/site_perl/5.6.1
    /u/avised/lib/perl5/site_perl
    /prod/datatech/inferno/bin
    /usr/lib/perl5/5.8.0/i386-linux-thread-multi
    /usr/lib/perl5/5.8.0
    /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi
    /usr/lib/perl5/site_perl/5.8.0
    /usr/lib/perl5/site_perl/5.6.1
    /usr/lib/perl5/site_perl
    /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi
    /usr/lib/perl5/vendor_perl/5.8.0
    /usr/lib/perl5/vendor_perl
    .

---
Environment for perl v5.8.0:
    HOME=/u/avised
    LANG=C
    LANGUAGE (unset)
    LD_LIBRARY_PATH=/u/avised/lib:/usr/local/X11R6/lib:/usr/local/lib:/usr/local/oracle/lib:/usr/local/deshaw/lib:/usr/local/pkgs/systems/lib:/opt/sybase/lib:/usr/local/pkgs/sybase-12.5/OCS-12_5/lib
    LOGDIR (unset)
    PATH=/u/avised/bin:/u/avised/bin/linux:/u/avised/datatech_dev/build/bin:/u/avised/idev/build/bin:/proj/fintech/builds/private/avised-inf/cppmessagecompiler/bin/linux:/prod/datatech/inferno/bin:/usr/local/bin:/usr/local/etc:/usr/local/deshaw/bin:/usr/bin:/bin:/usr/ucb:/usr/local/sybase/OCS/bin:/usr/sbin:/sbin:/usr/local/oracle/bin:.:/usr/X11R6/bin:/usr/local/sbin:/usr/local/X/bin
    PERL5LIB=/u/avised/lib/perl5:/u/avised/lib/perl5/5.8.0:/u/avised/lib/perl5/site_perl/5.8.0:/u/avised/lib/perl5/site_perl/5.8.0/i386-linux:/u/avised/lib/perl5/site_perl:/prod/datatech/inferno/bin
    PERL_BADLANG (unset)
    SHELL=/usr/local/bin/tcsh



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