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

Re: [perl #123665] 'Useless use of a constant in void context' iscompile-time but not syntax warning

Thread Previous | Thread Next
From:
Abigail
Date:
September 15, 2016 09:07
Subject:
Re: [perl #123665] 'Useless use of a constant in void context' iscompile-time but not syntax warning
Message ID:
20160915091045.GA24700@almanda.fritz.box
On Wed, Sep 14, 2016 at 05:37:43AM -0700, Redfred Garett via RT wrote:
> The functions in this section can serve as terms in an expression.
>        They fall into two major categories: list operators and named unary
>        operators.  These differ in their precedence relationship with a fol-
>        lowing comma. 
> There are two issues at hand here.
> 
>     Why does use bigint; 1; warn in void context?
>     Why is the constant being executed in void context in the first place?
> 
> $ perl -c -we'1 while sub_with_side_effects();'
> -e syntax OK
> 
> $ perl -c -we'use bigint; 1 while sub_with_side_effects();'
> Useless use of a constant (1) in void context at -e line 1.
> -e syntax OK



The answer to the second question is simple: it's executed in void
context because that's how the program is written. The body of the
while loop is just '1', which is in void context.

As for the first part: Normally, perl warns if it sees a literal in
void context. The assumption is that this is likely to be a bug, as
there would not be any effect. However, exceptions are made for the
values 1 and 0 -- that allows you to write things like "1 while ..."
without perl complaining.

However, under "use bigint", what looks like a literal 1 to the reader
no longer looks like a plain literal to perl. It's an overloaded 
Math::BigInt object. It's no longer a literal, and perl doesn't know
whether fechting the value has any side effects or not. So, it won't
warn.

To see the difference:

    $ perl -MDevel::Peel -we 'Dump 1';
    SV = IV(0x1008035e8) at 0x1008035f8
      REFCNT = 1
      FLAGS = (IOK,READONLY,PROTECT,pIOK)
      IV = 1

    $ perl -MDevel::Peel -we 'use bigint; Dump 1';
    SV = IV(0x1009559d0) at 0x1009559e0
      REFCNT = 1
      FLAGS = (ROK,READONLY,PROTECT)
      RV = 0x101000450
      SV = PVHV(0x1008ac840) at 0x101000450
        REFCNT = 1
        FLAGS = (OBJECT,SHAREKEYS)
        STASH = 0x101044288 "Math::BigInt"
        ARRAY = 0x10061f870  (0:6, 1:2)
        hash quality = 125.0%
        KEYS = 2
        FILL = 2
        MAX = 7
        Elt "sign" HASH = 0xdd7ff9fd
        SV = PV(0x100805f80) at 0x10082bdf8
          REFCNT = 1
          FLAGS = (POK,IsCOW,pPOK)
          PV = 0x100714b00 "+"\0
          CUR = 1
          LEN = 10
          COW_REFCNT = 1
        Elt "value" HASH = 0x7fda65e6
        SV = IV(0x101000410) at 0x101000420
          REFCNT = 1
          FLAGS = (ROK)
          RV = 0x10082bdb0
          SV = PVAV(0x1008060c8) at 0x10082bdb0
            REFCNT = 1
            FLAGS = ()
            ARRAY = 0x1006239c0
            FILL = 0
            MAX = 0
            ARYLEN = 0x0
            FLAGS = (REAL)



Regards,


Abigail
            Elt No. 0
            SV = IV(0x10082bd88) at 0x10082bd98
              REFCNT = 1
              FLAGS = (IOK,pIOK)
              IV = 1


Thread Previous | 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