Aristotle has asked for the wisdom of the Perl Monks concerning the following question:

Observe:
$ perl -e'm!\/?(.*)\/([^/]+\/?)!' $ perl -e'm/\/?(.*)\/([^/]+\/?)/' Unmatched [ before HERE mark in regex m//?(.*)/([ << HERE ^/ at -e lin +e 1.
What we have:
Summary of my perl5 (revision 5.0 version 6 subversion 1) configuratio +n: Platform: osname=linux, osvers=2.4.2, archname=i386-linux uname='linux grommet 2.4.2 #1 smp sun feb 25 16:33:33 pst 2001 i68 +6 unknown ' config_args='-de -Dprefix=/usr -Darchname=i386-linux -Dprivlib=/us +r/lib/perl5 -Darchlib=/us r/lib/perl5/i386-linux -Dsitelib=/usr/lib/perl5/site_perl -Dsitearch=/ +usr/lib/perl5/site_perl/i 386-linux' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultipl +icity=undef useperlio=undef d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef Compiler: cc='cc', ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LA +RGEFILE_SOURCE -D_FILE_OF FSET_BITS=64', optimize='-O2', cppflags='-fno-strict-aliasing -I/usr/local/include' ccversion='', gccversion='2.95.3 20010315 (release)', gccosandvers +='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +2 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=8 alignbytes=4, usemymalloc=n, prototype=define Linker and Libraries: ld='cc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lnsl -lndbm -lgdbm -ldl -lm -lc -lcrypt -lutil perllibs=-lnsl -ldl -lm -lc -lcrypt -lutil libc=/lib/libc-2.2.2.so, so=so, useshrplib=false, libperl=libperl. +a Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynami +c' cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib' Characteristics of this binary (from libperl): Compile-time options: USE_LARGE_FILES Built under linux Compiled at Apr 11 2001 22:57:30 %ENV: PERL5LIB="/home/edgewalker/.perl/lib" @INC: /home/edgewalker/.perl/lib /usr/lib/perl5/i386-linux /usr/lib/perl5 /usr/lib/perl5/site_perl/i386-linux /usr/lib/perl5/site_perl /usr/lib/perl5/site_perl .

____________
Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Bug in RE parser?
by webfiend (Vicar) on May 29, 2002 at 22:03 UTC

    The first one works fine for me, the second works if I escape the slash in brackets:

    $ perl -e'm/\/?(.*)\/([^\/]+\/?)/'

    "All you need is ignorance and confidence; then success is sure."-- Mark Twain

      I could have sworn I did that and got the same error. I was about to post a message to that effect here. Except it worked with an escape for me, too, this time. I don’t know what I did last time when I thought I had escaped that slash. Probably brainfog. I think I’m getting old… sigh.

      Makeshifts last the longest.

        If your brain works anything like mine, you probably escaped that one, but forgot to escape one of the others :-)


        "All you need is ignorance and confidence; then success is sure."-- Mark Twain
Re: Bug in RE parser?
by Anonymous Monk on May 29, 2002 at 22:22 UTC
    You didn't escape the slash inside the char class. It might look like Perl should be smart enough to realize that the slash is inside a char class and isn't supposed to be a delimiter, since it complains about the pattern before it complains about syntax error after the pattern. (m/\/?(.*)\/([^/ is the match op part and ]+\/?)/ is the following Perl code.) But that's just due to perl's optimization. perl compiles patterns at compile-time if it can. The pattern compilation is done before the rest of the program is parsed. Kind of like a BEGIN subroutine. This can be demostrated by making perl unable to compile the pattern at comile-time:
    perl -e 'm/$a\/?(.*)\/([^/]+\/?)/' Unmatched right square bracket at -e line 1, at end of line syntax error at -e line 1, near "m/$a\/?(.*)\/([^/]"
    Cheers,
    -Anomo
Re: Bug in RE parser?
by Anonymous Monk on May 29, 2002 at 22:04 UTC
    Put an unescaped / in a m// delimited re and what do you expect to happen?