Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.99k stars 559 forks source link

Please improve parsing #19067

Open KES777 opened 3 years ago

KES777 commented 3 years ago

Description

Correct perl code is not parsed properly

Steps to Reproduce

perl -wE'say( $v /3 )'
Search pattern not terminated at -e line 1.

Expected behavior I expect result of division. It must work same as:

     perl -wE'say( $v / 3 )'  # this parsed OK. Notice space after /

Perl configuration

$ perl -V
Summary of my perl5 (revision 5 version 30 subversion 3) configuration:

  Platform:
    osname=linux
    osvers=5.4.0-37-generic
    archname=x86_64-linux
    uname='linux work 5.4.0-37-generic #41~18.04.1-ubuntu smp mon jun 8 13:37:29 utc 2020 x86_64 x86_64 x86_64 gnulinux '
    config_args='-de -Dprefix=/home/kes/perl5/perlbrew/perls/perl-5.30.3 -Aeval:scriptdir=/home/kes/perl5/perlbrew/perls/perl-5.30.3/bin'
    hint=recommended
    useposix=true
    d_sigaction=define
    useithreads=undef
    usemultiplicity=undef
    use64bitint=define
    use64bitall=define
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
    bincompat5005=undef
  Compiler:
    cc='cc'
    ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
    optimize='-O2'
    cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    ccversion=''
    gccversion='7.5.0'
    gccosandvers=''
    intsize=4
    longsize=8
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=16
    longdblkind=3
    ivtype='long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='cc'
    ldflags =' -fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/local/lib /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed /usr/include/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu /lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib /lib64 /usr/lib64
    libs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    libc=libc-2.27.so
    so=so
    useshrplib=false
    libperl=libperl.a
    gnulibc_version='2.27'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=so
    d_dlsymun=undef
    ccdlflags='-Wl,-E'
    cccdlflags='-fPIC'
    lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector-strong'

Characteristics of this binary (from libperl): 
  Compile-time options:
    HAS_TIMES
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    USE_64_BIT_ALL
    USE_64_BIT_INT
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
  Locally applied patches:
    Devel::PatchPerl 1.38
  Built under linux
  Compiled at Jun 22 2020 14:54:09
  %ENV:
    PERLBREW="command perlbrew"
    PERLBREW_HOME="/home/kes/.perlbrew"
    PERLBREW_MANPATH="/home/kes/perl5/perlbrew/perls/perl-5.30.3/man"
    PERLBREW_PATH="/home/kes/perl5/perlbrew/bin:/home/kes/perl5/perlbrew/perls/perl-5.30.3/bin"
    PERLBREW_PERL="perl-5.30.3"
    PERLBREW_ROOT="/home/kes/perl5/perlbrew"
    PERLBREW_SHELLRC_VERSION="0.89"
    PERLBREW_VERSION="0.89"
  @INC:
    /home/kes/perl5/perlbrew/perls/perl-5.30.3/lib/site_perl/5.30.3/x86_64-linux
    /home/kes/perl5/perlbrew/perls/perl-5.30.3/lib/site_perl/5.30.3
    /home/kes/perl5/perlbrew/perls/perl-5.30.3/lib/5.30.3/x86_64-linux
    /home/kes/perl5/perlbrew/perls/perl-5.30.3/lib/5.30.3

I do not use =~, but perl thinks that / is not division but start of regular expression.

This is hard to find what is wrong at more wide context. See next part of code:

        my $suma =  U::round $item->{ suma };  # line 36 
        $ts   +=  $suma;
        $tnds +=  U::round $suma /6;
    }

    return( $ts, $tnds );
}

sub create { #line 46
    my( $c ) =  shift;

    my $info =  $c->req->json;
    my $header  =  @$info[0];
    my $details =  @$info[1];

    my $agre =  D::T Agreement =>  $header->{ agreement_id };
    my( $total_suma, $total_nds ) =  total( $details );

    my $saldo    =  0;
    my $iid      =  @$details[0]->{ period };
    my $interval =  D::T Period => $iid //7; # line 58
    # This is first Invoice if operator do not provide activation date
    my $is_first =  !$details->[0]{valid_from}  &&  $iid  &&  $interval;

Error occur:

Can't load application from file "lib/MaitreD/Controller/ManualDocument.pm line 38, near "my $interval =  D::T Period => $iid /"
Unknown regexp modifier "/6" at lib/MaitreD/Controller/ManualDocument.pm line 38, at end of line
Global symbol "$pkg" requires explicit package name (did you forget to declare "my $pkg"?) at lib/MaitreD/Controller/ManualDocument.pm line 41.
...
many errors goes here...

https://stackoverflow.com/q/68863999/4632019

leonerd commented 3 years ago

An unfortunate instance of whitespace sensitivity:

$ perl -wE 'say( $v /3 )'
Search pattern not terminated at -e line 1.

$ perl -wE 'say( $v / 3 )'
Name "main::v" used only once: possible typo at -e line 1.
Use of uninitialized value $v in division (/) at -e line 1.
0

$ perl -wE 'say( $v/3 )'
Name "main::v" used only once: possible typo at -e line 1.
Use of uninitialized value $v in division (/) at -e line 1.
0
KES777 commented 3 years ago

@leonerd Could it be improved?

Leont commented 3 years ago

Could it be improved?

I suspect not. As often happens the culprit here is an indirect method invocation: say $v /3/ is a legitimate expression.

jkeenan commented 3 years ago

Could it be improved?

I suspect not. As often happens the culprit here is an indirect method invocation: say $v /3/ is a legitimate expression.

Is there any way we could provide documentation that would alert users to this type of bug?