[inseparable changes from match from perl-5.003_97b to perl-5.003_97c]
[p5sagit/p5-mst-13.2.git] / toke.c
diff --git a/toke.c b/toke.c
index 0ce1749..6a306ec 100644 (file)
--- a/toke.c
+++ b/toke.c
@@ -1,6 +1,6 @@
 /*    toke.c
  *
- *    Copyright (c) 1991-1994, Larry Wall
+ *    Copyright (c) 1991-1997, Larry Wall
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -50,6 +50,8 @@ static void restore_rsfp _((void *f));
 
 static char *linestart;                /* beg. of most recently read line */
 
+static char pending_ident;     /* pending identifier lookup */
+
 static struct {
     I32 super_state;   /* lexer state to save */
     I32 sub_inwhat;    /* "lex_inwhat" to use */
@@ -155,12 +157,15 @@ no_op(what, s)
 char *what;
 char *s;
 {
-    char tmpbuf[128];
     char *oldbp = bufptr;
     bool is_first = (oldbufptr == linestart);
+    char *msg;
+
     bufptr = s;
-    sprintf(tmpbuf, "%s found where operator expected", what);
-    yywarn(tmpbuf);
+    New(890, msg, strlen(what) + 40, char);
+    sprintf(msg, "%s found where operator expected", what);
+    yywarn(msg);
+    Safefree(msg);
     if (is_first)
        warn("\t(Missing semicolon on previous line?)\n");
     else if (oldoldbufptr && isIDFIRST(*oldoldbufptr)) {
@@ -189,7 +194,7 @@ char *s;
     }
     else if (multi_close < 32 || multi_close == 127) {
        *tmpbuf = '^';
-       tmpbuf[1] = multi_close ^ 64;
+       tmpbuf[1] = toCTRL(multi_close);
        s = "\\n";
        tmpbuf[2] = '\0';
        s = tmpbuf;
@@ -284,6 +289,7 @@ SV *line;
 void
 lex_end()
 {
+    doextract = FALSE;
 }
 
 static void
@@ -442,10 +448,15 @@ char *s;
 #define LOP(f,x) return lop(f,x,s)
 
 static I32
-lop(f,x,s)
+lop
+#ifdef CAN_PROTOTYPE
+   (I32 f, expectation x, char *s)
+#else
+   (f,x,s)
 I32 f;
 expectation x;
 char *s;
+#endif /* CAN_PROTOTYPE */
 {
     yylval.ival = f;
     CLINE;
@@ -611,7 +622,11 @@ sublex_start()
        return THING;
     }
     if (op_type == OP_CONST || op_type == OP_READLINE) {
-       yylval.opval = (OP*)newSVOP(op_type, 0, q(lex_stuff));
+       SV *sv = q(lex_stuff);
+       STRLEN len;
+       char *p = SvPV(sv, len);
+       yylval.opval = (OP*)newSVOP(op_type, 0, newSVpv(p, len));
+       SvREFCNT_dec(sv);
        lex_stuff = Nullsv;
        return THING;
     }
@@ -822,10 +837,8 @@ char *start;
                continue;
            case 'c':
                s++;
-               *d = *s++;
-               if (isLOWER(*d))
-                   *d = toUPPER(*d);
-               *d++ ^= 64;
+               len = *s++;
+               *d++ = toCTRL(len);
                continue;
            case 'b':
                *d++ = '\b';
@@ -1012,7 +1025,7 @@ GV *gv;
     if (gv) {
        if (GvIO(gv))
            return 0;
-       if (!GvCV(gv))
+       if (!GvCVu(gv))
            gv = 0;
     }
     s = scan_word(s, tmpbuf, TRUE, &len);
@@ -1026,7 +1039,7 @@ GV *gv;
     }
     if (!keyword(tmpbuf, len)) {
        indirgv = gv_fetchpv(tmpbuf,FALSE, SVt_PVCV);
-       if (indirgv && GvCV(indirgv))
+       if (indirgv && GvCVu(indirgv))
            return 0;
        /* filehandle or package name makes it a method */
        if (!gv || GvIO(indirgv) || gv_stashpvn(tmpbuf, len, FALSE)) {
@@ -1095,7 +1108,7 @@ filter_add(funcp, datasv)
         die("Can't upgrade filter_add data to SVt_PVIO");
     IoDIRP(datasv) = (DIR*)funcp; /* stash funcp into spare field */
     if (filter_debug)
-       warn("filter_add func %lx (%s)", funcp, SvPV(datasv,na));
+       warn("filter_add func %p (%s)", funcp, SvPV(datasv,na));
     av_unshift(rsfp_filters, 1);
     av_store(rsfp_filters, 0, datasv) ;
     return(datasv);
@@ -1108,7 +1121,7 @@ filter_del(funcp)
     filter_t funcp;
 {
     if (filter_debug)
-       warn("filter_del func %lx", funcp);
+       warn("filter_del func %p", funcp);
     if (!rsfp_filters || AvFILL(rsfp_filters)<0)
        return;
     /* if filter is on top of stack (usual case) just pop it off */
@@ -1174,7 +1187,7 @@ filter_read(idx, buf_sv, maxlen)
     /* Get function pointer hidden within datasv       */
     funcp = (filter_t)IoDIRP(datasv);
     if (filter_debug)
-       warn("filter_read %d: via function %lx (%s)\n",
+       warn("filter_read %d: via function %p (%s)\n",
                idx, funcp, SvPV(datasv,na));
     /* Call function. The function is expected to      */
     /* call "FILTER_READ(idx+1, buf_sv)" first.                */
@@ -1208,7 +1221,7 @@ STRLEN append;
        { "OPERATOR", "TERM", "REF", "STATE", "BLOCK", "TERMBLOCK" };
 #endif
 
-extern int yychar;             /* last token */
+EXT int yychar;                /* last token */
 
 int
 yylex()
@@ -1218,6 +1231,60 @@ yylex()
     register I32 tmp;
     STRLEN len;
 
+    if (pending_ident) {
+       char pit = pending_ident;
+       pending_ident = 0;
+
+       if (in_my) {
+           if (strchr(tokenbuf,':'))
+               croak(no_myglob,tokenbuf);
+           yylval.opval = newOP(OP_PADANY, 0);
+           yylval.opval->op_targ = pad_allocmy(tokenbuf);
+           return PRIVATEREF;
+       }
+
+       if (!strchr(tokenbuf,':') && (tmp = pad_findmy(tokenbuf))) {
+           if (last_lop_op == OP_SORT &&
+               tokenbuf[0] == '$' &&
+               (tokenbuf[1] == 'a' || tokenbuf[1] == 'b')
+               && !tokenbuf[2])
+           {
+               for (d = in_eval ? oldoldbufptr : linestart;
+                    d < bufend && *d != '\n';
+                    d++)
+               {
+                   if (strnEQ(d,"<=>",3) || strnEQ(d,"cmp",3)) {
+                       croak("Can't use \"my %s\" in sort comparison",
+                             tokenbuf);
+                   }
+               }
+           }
+
+           yylval.opval = newOP(OP_PADANY, 0);
+           yylval.opval->op_targ = tmp;
+           return PRIVATEREF;
+       }
+
+       /* Force them to make up their mind on "@foo". */
+       if (pit == '@' && lex_state != LEX_NORMAL && !lex_brackets) {
+           GV *gv = gv_fetchpv(tokenbuf+1, FALSE, SVt_PVAV);
+           if (!gv || ((tokenbuf[0] == '@') ? !GvAV(gv) : !GvHV(gv))) {
+               char tmpbuf[1024];
+               sprintf(tmpbuf, "In string, %s now must be written as \\%s",
+                       tokenbuf, tokenbuf);
+               yyerror(tmpbuf);
+           }
+       }
+
+       yylval.opval = (OP*)newSVOP(OP_CONST, 0, newSVpv(tokenbuf+1, 0));
+       yylval.opval->op_private = OPpCONST_ENTERED;
+       gv_fetchpv(tokenbuf+1, in_eval ? GV_ADDMULTI : TRUE,
+                  ((tokenbuf[0] == '$') ? SVt_PV
+                   : (tokenbuf[0] == '@') ? SVt_PVAV
+                   : SVt_PVHV));
+       return WORD;
+    }
+
     switch (lex_state) {
 #ifdef COMMENTARY
     case LEX_NORMAL:           /* Some compilers will produce faster */
@@ -1325,9 +1392,7 @@ yylex()
            s = bufptr;
            Aop(OP_CONCAT);
        }
-       else
-           return yylex();
-       break;
+       return yylex();
 
     case LEX_INTERPENDMAYBE:
        if (intuit_more(bufptr)) {
@@ -1397,8 +1462,7 @@ yylex()
   retry:
     switch (*s) {
     default:
-       warn("Unrecognized character \\%03o ignored", *s++ & 255);
-       goto retry;
+       croak("Unrecognized character \\%03o", *s & 255);
     case 4:
     case 26:
        goto fake_eof;                  /* emulate EOF on ^D or ^Z */
@@ -1433,16 +1497,33 @@ yylex()
                sv_catpv(linestr, "LINE: while (<>) {");
                if (minus_l)
                    sv_catpv(linestr,"chomp;");
-               if (minus_a){
-                   if (minus_F){
-                     char tmpbuf1[50];
-                     if ( splitstr[0] == '/' || 
-                          splitstr[0] == '\'' || 
-                          splitstr[0] == '"' )
-                           sprintf( tmpbuf1, "@F=split(%s);", splitstr );
-                       else
-                           sprintf( tmpbuf1, "@F=split('%s');", splitstr );
-                       sv_catpv(linestr,tmpbuf1);
+               if (minus_a) {
+                   GV* gv = gv_fetchpv("::F", TRUE, SVt_PVAV);
+                   if (gv)
+                       GvIMPORTED_AV_on(gv);
+                   if (minus_F) {
+                       char *tmpbuf1;
+                       New(201, tmpbuf1, strlen(splitstr) * 2 + 20, char);
+                       if (strchr("/'\"", *splitstr)
+                             && strchr(splitstr + 1, *splitstr))
+                           sprintf(tmpbuf1, "@F=split(%s);", splitstr);
+                       else {
+                           char delim;
+                           s = "'~#\200\1'"; /* surely one char is unused...*/
+                           while (s[1] && strchr(splitstr, *s))  s++;
+                           delim = *s;
+                           sprintf(tmpbuf1, "@F=split(%s%c",
+                                   "q" + (delim == '\''), delim);
+                           d = tmpbuf1 + strlen(tmpbuf1);
+                           for (s = splitstr; *s; ) {
+                               if (*s == '\\')
+                                   *d++ = '\\';
+                               *d++ = *s++;
+                           }
+                           sprintf(d, "%c);", delim);
+                       }
+                       sv_catpv(linestr,tmpbuf1);
+                       Safefree(tmpbuf1);
                    }
                    else
                        sv_catpv(linestr,"@F=split(' ');");
@@ -1512,25 +1593,84 @@ yylex()
                s++;
            if (*s == ':' && s[1] != ':') /* for csh execing sh scripts */
                s++;
-           if (!in_eval && *s == '#' && s[1] == '!') {
+           d = Nullch;
+           if (!in_eval) {
+               if (*s == '#' && *(s+1) == '!')
+                   d = s + 2;
+#ifdef ALTERNATE_SHEBANG
+               else {
+                   static char as[] = ALTERNATE_SHEBANG;
+                   if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
+                       d = s + (sizeof(as) - 1);
+               }
+#endif /* ALTERNATE_SHEBANG */
+           }
+           if (d) {
+               char *ipath;
+               char *ipathend;
+
+               while (isSPACE(*d))
+                   d++;
+               ipath = d;
+               while (*d && !isSPACE(*d))
+                   d++;
+               ipathend = d;
+
+#ifdef ARG_ZERO_IS_SCRIPT
+               if (ipathend > ipath) {
+                   /*
+                    * HP-UX (at least) sets argv[0] to the script name,
+                    * which makes $^X incorrect.  And Digital UNIX and Linux,
+                    * at least, set argv[0] to the basename of the Perl
+                    * interpreter. So, having found "#!", we'll set it right.
+                    */
+                   SV *x = GvSV(gv_fetchpv("\030", TRUE, SVt_PV));
+                   assert(SvPOK(x) || SvGMAGICAL(x));
+                   if (sv_eq(x, GvSV(curcop->cop_filegv))) {
+                       sv_setpvn(x, ipath, ipathend - ipath);
+                       SvSETMAGIC(x);
+                   }
+                   TAINT_NOT;  /* $^X is always tainted, but that's OK */
+               }
+#endif /* ARG_ZERO_IS_SCRIPT */
+
+               /*
+                * Look for options.
+                */
                d = instr(s,"perl -");
                if (!d)
                    d = instr(s,"perl");
+#ifdef ALTERNATE_SHEBANG
+               /*
+                * If the ALTERNATE_SHEBANG on this system starts with a
+                * character that can be part of a Perl expression, then if
+                * we see it but not "perl", we're probably looking at the
+                * start of Perl code, not a request to hand off to some
+                * other interpreter.  Similarly, if "perl" is there, but
+                * not in the first 'word' of the line, we assume the line
+                * contains the start of the Perl program.
+                */
+               if (d && *s != '#') {
+                   char *c = ipath;
+                   while (*c && !strchr("; \t\r\n\f\v#", *c))
+                       c++;
+                   if (c < d)
+                       d = Nullch;     /* "perl" not in first word; ignore */
+                   else
+                       *s = '#';       /* Don't try to parse shebang line */
+               }
+#endif /* ALTERNATE_SHEBANG */
                if (!d &&
+                   *s == '#' &&
+                   ipathend > ipath &&
                    !minus_c &&
                    !instr(s,"indir") &&
                    instr(origargv[0],"perl"))
                {
                    char **newargv;
-                   char *cmd;
 
-                   s += 2;
-                   if (*s == ' ')
-                       s++;
-                   cmd = s;
-                   while (s < bufend && !isSPACE(*s))
-                       s++;
-                   *s++ = '\0';
+                   *ipathend = '\0';
+                   s = ipathend + 1;
                    while (s < bufend && isSPACE(*s))
                        s++;
                    if (s < bufend) {
@@ -1543,9 +1683,9 @@ yylex()
                    }
                    else
                        newargv = origargv;
-                   newargv[0] = cmd;
-                   execv(cmd,newargv);
-                   croak("Can't exec %s", cmd);
+                   newargv[0] = ipath;
+                   execv(ipath, newargv);
+                   croak("Can't exec %s", ipath);
                }
                if (d) {
                    int oldpdb = perldb;
@@ -1556,7 +1696,15 @@ yylex()
                    while (*d == ' ' || *d == '\t') d++;
 
                    if (*d++ == '-') {
-                       while (d = moreswitches(d)) ;
+                       do {
+                           if (*d == 'M' || *d == 'm') {
+                               char *m = d;
+                               while (*d && !isSPACE(*d)) d++;
+                               croak("Too late for \"-%.*s\" option",
+                                     (int)(d - m), m);
+                           }
+                           d = moreswitches(d);
+                       } while (d);
                        if (perldb && !oldpdb ||
                            ( minus_n || minus_p ) && !(oldn || oldp) )
                              /* if we have already added "LINE: while (<>) {",
@@ -1580,7 +1728,11 @@ yylex()
            return yylex();
        }
        goto retry;
-    case ' ': case '\t': case '\f': case '\r': case 013:
+    case '\r':
+       warn("Illegal character \\%03o (carriage return)", '\r');
+       croak(
+      "(Maybe you didn't strip carriage returns after a network transfer?)\n");
+    case ' ': case '\t': case '\f': case 013:
        s++;
        goto retry;
     case '#':
@@ -1615,7 +1767,7 @@ yylex()
            if (strnEQ(s,"=>",2)) {
                if (dowarn)
                    warn("Ambiguous use of -%c => resolved to \"-%c\" =>",
-                       tmp, tmp);
+                       (int)tmp, (int)tmp);
                s = force_word(bufptr,WORD,FALSE,FALSE,FALSE);
                OPERATOR('-');          /* unary minus */
            }
@@ -1650,7 +1802,7 @@ yylex()
            case 'A': gv_fetchpv("\024",TRUE, SVt_PV); FTST(OP_FTATIME);
            case 'C': gv_fetchpv("\024",TRUE, SVt_PV); FTST(OP_FTCTIME);
            default:
-               croak("Unrecognized file test: -%c", tmp);
+               croak("Unrecognized file test: -%c", (int)tmp);
                break;
            }
        }
@@ -1716,35 +1868,19 @@ yylex()
        Mop(OP_MULTIPLY);
 
     case '%':
-       if (expect != XOPERATOR) {
-           s = scan_ident(s, bufend, tokenbuf + 1, TRUE);
-           if (tokenbuf[1]) {
-               expect = XOPERATOR;
-               tokenbuf[0] = '%';
-               if (in_my) {
-                   if (strchr(tokenbuf,':'))
-                       croak(no_myglob,tokenbuf);
-                   nextval[nexttoke].opval = newOP(OP_PADANY, 0);
-                   nextval[nexttoke].opval->op_targ = pad_allocmy(tokenbuf);
-                   force_next(PRIVATEREF);
-                   TERM('%');
-               }
-               if (!strchr(tokenbuf,':')) {
-                   if (tmp = pad_findmy(tokenbuf)) {
-                       nextval[nexttoke].opval = newOP(OP_PADANY, 0);
-                       nextval[nexttoke].opval->op_targ = tmp;
-                       force_next(PRIVATEREF);
-                       TERM('%');
-                   }
-               }
-               force_ident(tokenbuf + 1, *tokenbuf);
-           }
-           else
-               PREREF('%');
-           TERM('%');
+       if (expect == XOPERATOR) {
+           ++s;
+           Mop(OP_MODULO);
+       }
+       tokenbuf[0] = '%';
+       s = scan_ident(s, bufend, tokenbuf+1, TRUE);
+       if (!tokenbuf[1]) {
+           if (s == bufend)
+               yyerror("Final % should be \\% or %name");
+           PREREF('%');
        }
-       ++s;
-       Mop(OP_MODULO);
+       pending_ident = '%';
+       TERM('%');
 
     case '^':
        s++;
@@ -1815,21 +1951,32 @@ yylex()
            else
                lex_brackstack[lex_brackets++] = XOPERATOR;
            OPERATOR(HASHBRACK);
-           break;
        case XOPERATOR:
            while (s < bufend && (*s == ' ' || *s == '\t'))
                s++;
-           if (s < bufend && (isALPHA(*s) || *s == '_')) {
-               d = scan_word(s, tokenbuf, FALSE, &len);
+           d = s;
+           tokenbuf[0] = '\0';
+           if (d < bufend && *d == '-') {
+               tokenbuf[0] = '-';
+               d++;
+               while (d < bufend && (*d == ' ' || *d == '\t'))
+                   d++;
+           }
+           if (d < bufend && isIDFIRST(*d)) {
+               d = scan_word(d, tokenbuf + 1, FALSE, &len);
                while (d < bufend && (*d == ' ' || *d == '\t'))
                    d++;
                if (*d == '}') {
+                   char minus = (tokenbuf[0] == '-');
                    if (dowarn &&
-                     (keyword(tokenbuf, len) ||
-                      perl_get_cv(tokenbuf, FALSE) ))
+                       (keyword(tokenbuf + 1, len) ||
+                        (minus && len == 1 && isALPHA(tokenbuf[1])) ||
+                        perl_get_cv(tokenbuf + 1, FALSE) ))
                        warn("Ambiguous use of {%s} resolved to {\"%s\"}",
-                           tokenbuf, tokenbuf);
-                   s = force_word(s,WORD,FALSE,TRUE,FALSE);
+                            tokenbuf + !minus, tokenbuf + !minus);
+                   s = force_word(s + minus, WORD, FALSE, TRUE, FALSE);
+                   if (minus)
+                       force_next('-');
                }
            }
            /* FALL THROUGH */
@@ -1893,7 +2040,9 @@ yylex()
                    bufptr = s;
                    return yylex();             /* ignore fake brackets */
                }
-               if (*s != '[' && *s != '{' && (*s != '-' || s[1] != '>'))
+               if (*s == '-' && s[1] == '>')
+                   lex_state = LEX_INTERPENDMAYBE;
+               else if (*s != '[' && *s != '{')
                    lex_state = LEX_INTERPEND;
            }
        }
@@ -1946,7 +2095,7 @@ yylex()
        if (tmp == '~')
            PMop(OP_MATCH);
        if (dowarn && tmp && isSPACE(*s) && strchr("+-*/%.^&|<",tmp))
-           warn("Reversed %c= operator",tmp);
+           warn("Reversed %c= operator",(int)tmp);
        s--;
        if (expect == XSTATE && isALPHA(tmp) &&
                (s == linestart+1 || s[-2] == '\n') )
@@ -2027,67 +2176,72 @@ yylex()
        Rop(OP_GT);
 
     case '$':
-       if (s[1] == '#'  && (isALPHA(s[2]) || strchr("_{$:", s[2]))) {
-           s = scan_ident(s+1, bufend, tokenbuf+1, FALSE);
-           if (expect == XOPERATOR) {
-               if (lex_formbrack && lex_brackets == lex_formbrack) {
-                   expect = XTERM;
-                   depcom();
-                   return ','; /* grandfather non-comma-format format */
-               }
-               else
-                   no_op("Array length",s);
+       CLINE;
+
+       if (expect == XOPERATOR) {
+           if (lex_formbrack && lex_brackets == lex_formbrack) {
+               expect = XTERM;
+               depcom();
+               return ','; /* grandfather non-comma-format format */
            }
-           else if (!tokenbuf[1])
+       }
+
+       if (s[1] == '#' && (isALPHA(s[2]) || strchr("_{$:", s[2]))) {
+           if (expect == XOPERATOR)
+               no_op("Array length", bufptr);
+           tokenbuf[0] = '@';
+           s = scan_ident(s+1, bufend, tokenbuf+1, FALSE);
+           if (!tokenbuf[1])
                PREREF(DOLSHARP);
-           if (!strchr(tokenbuf+1,':')) {
-               tokenbuf[0] = '@';
-               if (tmp = pad_findmy(tokenbuf)) {
-                   nextval[nexttoke].opval = newOP(OP_PADANY, 0);
-                   nextval[nexttoke].opval->op_targ = tmp;
-                   expect = XOPERATOR;
-                   force_next(PRIVATEREF);
-                   TOKEN(DOLSHARP);
-               }
-           }
            expect = XOPERATOR;
-           force_ident(tokenbuf+1, *tokenbuf);
+           pending_ident = '#';
            TOKEN(DOLSHARP);
        }
+
+       if (expect == XOPERATOR)
+           no_op("Scalar", bufptr);
+       tokenbuf[0] = '$';
        s = scan_ident(s, bufend, tokenbuf+1, FALSE);
-       if (expect == XOPERATOR) {
-           if (lex_formbrack && lex_brackets == lex_formbrack) {
-               expect = XTERM;
-               depcom();
-               return ',';     /* grandfather non-comma-format format */
-           }
-           else
-               no_op("Scalar",s);
+       if (!tokenbuf[1]) {
+           if (s == bufend)
+               yyerror("Final $ should be \\$ or $name");
+           PREREF('$');
        }
-       if (tokenbuf[1]) {
-           expectation oldexpect = expect;
 
-           /* This kludge not intended to be bulletproof. */
-           if (tokenbuf[1] == '[' && !tokenbuf[2]) {
-               yylval.opval = newSVOP(OP_CONST, 0,
-                                       newSViv((IV)compiling.cop_arybase));
-               yylval.opval->op_private = OPpCONST_ARYBASE;
-               TERM(THING);
-           }
-           tokenbuf[0] = '$';
-           if (dowarn) {
-               char *t;
-               if (*s == '[' && oldexpect != XREF) {
-                   for (t = s+1; isSPACE(*t) || isALNUM(*t) || *t == '$'; t++) ;
+       /* This kludge not intended to be bulletproof. */
+       if (tokenbuf[1] == '[' && !tokenbuf[2]) {
+           yylval.opval = newSVOP(OP_CONST, 0,
+                                  newSViv((IV)compiling.cop_arybase));
+           yylval.opval->op_private = OPpCONST_ARYBASE;
+           TERM(THING);
+       }
+
+       d = s;
+       if (lex_state == LEX_NORMAL)
+           s = skipspace(s);
+
+       if ((expect != XREF || oldoldbufptr == last_lop) && intuit_more(s)) {
+           char *t;
+           if (*s == '[') {
+               tokenbuf[0] = '@';
+               if (dowarn) {
+                   for(t = s + 1;
+                       isSPACE(*t) || isALNUM(*t) || *t == '$';
+                       t++) ;
                    if (*t++ == ',') {
                        bufptr = skipspace(bufptr);
-                       while (t < bufend && *t != ']') t++;
+                       while (t < bufend && *t != ']')
+                           t++;
                        warn("Multidimensional syntax %.*s not supported",
-                           t-bufptr+1, bufptr);
+                            (t - bufptr) + 1, bufptr);
                    }
                }
-               if (*s == '{' && strEQ(tokenbuf, "$SIG") &&
-                 (t = strchr(s,'}')) && (t = strchr(t,'='))) {
+           }
+           else if (*s == '{') {
+               tokenbuf[0] = '%';
+               if (dowarn && strEQ(tokenbuf+1, "SIG") &&
+                   (t = strchr(s, '}')) && (t = strchr(t, '=')))
+               {
                    char tmpbuf[1024];
                    STRLEN len;
                    for (t++; isSPACE(*t); t++) ;
@@ -2098,114 +2252,55 @@ yylex()
                    }
                }
            }
-           expect = XOPERATOR;
-           if (lex_state == LEX_NORMAL && isSPACE(*s)) {
-               bool islop = (last_lop == oldoldbufptr);
-               s = skipspace(s);
-               if (!islop || last_lop_op == OP_GREPSTART)
-                   expect = XOPERATOR;
-               else if (strchr("$@\"'`q", *s))
-                   expect = XTERM;             /* e.g. print $fh "foo" */
-               else if (strchr("&*<%", *s) && isIDFIRST(s[1]))
-                   expect = XTERM;             /* e.g. print $fh &sub */
-               else if (isDIGIT(*s))
-                   expect = XTERM;             /* e.g. print $fh 3 */
-               else if (*s == '.' && isDIGIT(s[1]))
-                   expect = XTERM;             /* e.g. print $fh .3 */
-               else if (strchr("/?-+", *s) && !isSPACE(s[1]))
-                   expect = XTERM;             /* e.g. print $fh -1 */
-               else if (*s == '<' && s[1] == '<' && !isSPACE(s[2]))
-                   expect = XTERM;             /* print $fh <<"EOF" */
-           }
-           if (in_my) {
-               if (strchr(tokenbuf,':'))
-                   croak(no_myglob,tokenbuf);
-               nextval[nexttoke].opval = newOP(OP_PADANY, 0);
-               nextval[nexttoke].opval->op_targ = pad_allocmy(tokenbuf);
-               force_next(PRIVATEREF);
-           }
-           else if (!strchr(tokenbuf,':')) {
-               if (oldexpect != XREF || oldoldbufptr == last_lop) {
-                   if (intuit_more(s)) {
-                       if (*s == '[')
-                           tokenbuf[0] = '@';
-                       else if (*s == '{')
-                           tokenbuf[0] = '%';
-                   }
-               }
-               if (tmp = pad_findmy(tokenbuf)) {
-                   if (last_lop_op == OP_SORT &&
-                       !tokenbuf[2] && *tokenbuf =='$' &&
-                       tokenbuf[1] <= 'b' && tokenbuf[1] >= 'a')
-                   {
-                       for (d = in_eval ? oldoldbufptr : linestart;
-                           d < bufend && *d != '\n';
-                           d++)
-                       {
-                           if (strnEQ(d,"<=>",3) || strnEQ(d,"cmp",3)) {
-                               croak("Can't use \"my %s\" in sort comparison",
-                                   tokenbuf);
-                           }
-                       }
-                   }
-                   nextval[nexttoke].opval = newOP(OP_PADANY, 0);
-                   nextval[nexttoke].opval->op_targ = tmp;
-                   force_next(PRIVATEREF);
+       }
+
+       expect = XOPERATOR;
+       if (lex_state == LEX_NORMAL && isSPACE(*d)) {
+           bool islop = (last_lop == oldoldbufptr);
+           if (!islop || last_lop_op == OP_GREPSTART)
+               expect = XOPERATOR;
+           else if (strchr("$@\"'`q", *s))
+               expect = XTERM;         /* e.g. print $fh "foo" */
+           else if (strchr("&*<%", *s) && isIDFIRST(s[1]))
+               expect = XTERM;         /* e.g. print $fh &sub */
+           else if (isIDFIRST(*s)) {
+               char tmpbuf[1024];
+               scan_word(s, tmpbuf, TRUE, &len);
+               if (keyword(tmpbuf, len))
+                   expect = XTERM;     /* e.g. print $fh length() */
+               else {
+                   GV *gv = gv_fetchpv(tmpbuf, FALSE, SVt_PVCV);
+                   if (gv && GvCVu(gv))
+                       expect = XTERM; /* e.g. print $fh subr() */
                }
-               else
-                   force_ident(tokenbuf+1, *tokenbuf);
            }
-           else
-               force_ident(tokenbuf+1, *tokenbuf);
-       }
-       else {
-           if (s == bufend)
-               yyerror("Final $ should be \\$ or $name");
-           PREREF('$');
-       }
+           else if (isDIGIT(*s))
+               expect = XTERM;         /* e.g. print $fh 3 */
+           else if (*s == '.' && isDIGIT(s[1]))
+               expect = XTERM;         /* e.g. print $fh .3 */
+           else if (strchr("/?-+", *s) && !isSPACE(s[1]))
+               expect = XTERM;         /* e.g. print $fh -1 */
+           else if (*s == '<' && s[1] == '<' && !isSPACE(s[2]))
+               expect = XTERM;         /* print $fh <<"EOF" */
+       }
+       pending_ident = '$';
        TOKEN('$');
 
     case '@':
-       s = scan_ident(s, bufend, tokenbuf+1, FALSE);
        if (expect == XOPERATOR)
-           no_op("Array",s);
-       if (tokenbuf[1]) {
-           GV* gv;
-
-           tokenbuf[0] = '@';
-           expect = XOPERATOR;
-           if (in_my) {
-               if (strchr(tokenbuf,':'))
-                   croak(no_myglob,tokenbuf);
-               nextval[nexttoke].opval = newOP(OP_PADANY, 0);
-               nextval[nexttoke].opval->op_targ = pad_allocmy(tokenbuf);
-               force_next(PRIVATEREF);
-               TERM('@');
-           }
-           else if (!strchr(tokenbuf,':')) {
-               if (intuit_more(s)) {
-                   if (*s == '{')
-                       tokenbuf[0] = '%';
-               }
-               if (tmp = pad_findmy(tokenbuf)) {
-                   nextval[nexttoke].opval = newOP(OP_PADANY, 0);
-                   nextval[nexttoke].opval->op_targ = tmp;
-                   force_next(PRIVATEREF);
-                   TERM('@');
-               }
-           }
-
-           /* Force them to make up their mind on "@foo". */
-           if (lex_state != LEX_NORMAL && !lex_brackets &&
-                   ( !(gv = gv_fetchpv(tokenbuf+1, FALSE, SVt_PVAV)) ||
-                     (*tokenbuf == '@'
-                       ? !GvAV(gv)
-                       : !GvHV(gv) )))
-           {
-               char tmpbuf[1024];
-               sprintf(tmpbuf, "Literal @%s now requires backslash",tokenbuf+1);
-               yyerror(tmpbuf);
-           }
+           no_op("Array", s);
+       tokenbuf[0] = '@';
+       s = scan_ident(s, bufend, tokenbuf+1, FALSE);
+       if (!tokenbuf[1]) {
+           if (s == bufend)
+               yyerror("Final @ should be \\@ or @name");
+           PREREF('@');
+       }
+       if (lex_state == LEX_NORMAL)
+           s = skipspace(s);
+       if ((expect != XREF || oldoldbufptr == last_lop) && intuit_more(s)) {
+           if (*s == '{')
+               tokenbuf[0] = '%';
 
            /* Warn about @ where they meant $. */
            if (dowarn) {
@@ -2221,13 +2316,8 @@ yylex()
                    }
                }
            }
-           force_ident(tokenbuf+1, *tokenbuf);
-       }
-       else {
-           if (s == bufend)
-               yyerror("Final @ should be \\@ or @name");
-           PREREF('@');
        }
+       pending_ident = '@';
        TERM('@');
 
     case '/':                  /* may either be division or pattern */
@@ -2367,16 +2457,34 @@ yylex()
       keylookup:
        bufptr = s;
        s = scan_word(s, tokenbuf, FALSE, &len);
-       
-       if (*s == ':' && s[1] == ':' && strNE(tokenbuf, "CORE"))
+
+       /* Some keywords can be followed by any delimiter, including ':' */
+       tmp = (len == 1 && strchr("msyq", tokenbuf[0]) ||
+              len == 2 && ((tokenbuf[0] == 't' && tokenbuf[1] == 'r') ||
+                           (tokenbuf[0] == 'q' &&
+                            strchr("qwx", tokenbuf[1]))));
+
+       /* x::* is just a word, unless x is "CORE" */
+       if (!tmp && *s == ':' && s[1] == ':' && strNE(tokenbuf, "CORE"))
            goto just_a_word;
 
+       d = s;
+       while (d < bufend && isSPACE(*d))
+               d++;    /* no comments skipped here, or s### is misparsed */
+
+       /* Is this a label? */
+       if (!tmp && expect == XSTATE
+             && d < bufend && *d == ':' && *(d + 1) != ':') {
+           s = d + 1;
+           yylval.pval = savepv(tokenbuf);
+           CLINE;
+           TOKEN(LABEL);
+       }
+
+       /* Check for keywords */
        tmp = keyword(tokenbuf, len);
 
        /* Is this a word before a => operator? */
-       d = s;
-       while (d < bufend && (*d == ' ' || *d == '\t'))
-               d++;    /* no comments skipped here, or s### is misparsed */
        if (strnEQ(d,"=>",2)) {
            CLINE;
            if (dowarn && (tmp || perl_get_cv(tokenbuf, FALSE)))
@@ -2416,25 +2524,14 @@ yylex()
                        croak("Bad name after %s::", tokenbuf);
                }
 
-               /* Do special processing at start of statement. */
-
-               if (expect == XSTATE) {
-                   while (isSPACE(*s)) s++;
-                   if (*s == ':') {    /* It's a label. */
-                       yylval.pval = savepv(tokenbuf);
-                       s++;
-                       CLINE;
-                       TOKEN(LABEL);
-                   }
-               }
-               else if (expect == XOPERATOR) {
+               if (expect == XOPERATOR) {
                    if (bufptr == linestart) {
                        curcop->cop_line--;
                        warn(warn_nosemi);
                        curcop->cop_line++;
                    }
                    else
-                       no_op("Bare word",s);
+                       no_op("Bareword",s);
                }
 
                /* Look for a subroutine with this name in current package. */
@@ -2470,7 +2567,7 @@ yylex()
                    /* (But it's an indir obj regardless for sort.) */
 
                    if ((last_lop_op == OP_SORT ||
-                         (!immediate_paren && (!gv || !GvCV(gv))) ) &&
+                         (!immediate_paren && (!gv || !GvCVu(gv))) ) &&
                         (last_lop_op != OP_MAPSTART && last_lop_op != OP_GREPSTART)){
                        expect = (last_lop == oldoldbufptr) ? XTERM : XOPERATOR;
                        goto bareword;
@@ -2492,7 +2589,7 @@ yylex()
 
                /* If followed by var or block, call it a method (unless sub) */
 
-               if ((*s == '$' || *s == '{') && (!gv || !GvCV(gv))) {
+               if ((*s == '$' || *s == '{') && (!gv || !GvCVu(gv))) {
                    last_lop = oldbufptr;
                    last_lop_op = OP_METHOD;
                    PREBLOCK(METHOD);
@@ -2505,7 +2602,7 @@ yylex()
 
                /* Not a method, so call it a subroutine (if defined) */
 
-               if (gv && GvCV(gv)) {
+               if (gv && GvCVu(gv)) {
                    CV* cv = GvCV(gv);
                    if (*s == '(') {
                        nextval[nexttoke].opval = yylval.opval;
@@ -2585,15 +2682,21 @@ yylex()
                TOKEN(WORD);
            }
 
+       case KEY___FILE__:
        case KEY___LINE__:
-       case KEY___FILE__: {
            if (tokenbuf[2] == 'L')
                (void)sprintf(tokenbuf,"%ld",(long)curcop->cop_line);
            else
                strcpy(tokenbuf, SvPVX(GvSV(curcop->cop_filegv)));
            yylval.opval = (OP*)newSVOP(OP_CONST, 0, newSVpv(tokenbuf,0));
            TERM(THING);
-       }
+
+       case KEY___PACKAGE__:
+           yylval.opval = (OP*)newSVOP(OP_CONST, 0,
+                                       (curstash
+                                        ? newSVsv(curstname)
+                                        : &sv_undef));
+           TERM(THING);
 
        case KEY___DATA__:
        case KEY___END__: {
@@ -2880,10 +2983,10 @@ yylex()
            FUN0(OP_GPWENT);
 
        case KEY_getpwnam:
-           FUN1(OP_GPWNAM);
+           UNI(OP_GPWNAM);
 
        case KEY_getpwuid:
-           FUN1(OP_GPWUID);
+           UNI(OP_GPWUID);
 
        case KEY_getpeername:
            UNI(OP_GETPEERNAME);
@@ -2925,10 +3028,10 @@ yylex()
            FUN0(OP_GGRENT);
 
        case KEY_getgrnam:
-           FUN1(OP_GGRNAM);
+           UNI(OP_GGRNAM);
 
        case KEY_getgrgid:
-           FUN1(OP_GGRGID);
+           UNI(OP_GGRGID);
 
        case KEY_getlogin:
            FUN0(OP_GETLOGIN);
@@ -3256,16 +3359,16 @@ yylex()
            LOP(OP_SETPRIORITY,XTERM);
 
        case KEY_sethostent:
-           FUN1(OP_SHOSTENT);
+           UNI(OP_SHOSTENT);
 
        case KEY_setnetent:
-           FUN1(OP_SNETENT);
+           UNI(OP_SNETENT);
 
        case KEY_setservent:
-           FUN1(OP_SSERVENT);
+           UNI(OP_SSERVENT);
 
        case KEY_setprotoent:
-           FUN1(OP_SPROTOENT);
+           UNI(OP_SPROTOENT);
 
        case KEY_setpwent:
            FUN0(OP_SPWENT);
@@ -3376,6 +3479,8 @@ yylex()
 
            /* Look for a prototype */
            if (*s == '(') {
+               char *p;
+
                s = scan_str(s);
                if (!s) {
                    if (lex_stuff)
@@ -3383,6 +3488,16 @@ yylex()
                    lex_stuff = Nullsv;
                    croak("Prototype not terminated");
                }
+               /* strip spaces */
+               d = SvPVX(lex_stuff);
+               tmp = 0;
+               for (p = d; *p; ++p) {
+                   if (!isSPACE(*p))
+                       d[tmp++] = *p;
+               }
+               d[tmp] = '\0';
+               SvCUR(lex_stuff) = tmp;
+
                nexttoke++;
                nextval[1] = nextval[0];
                nexttype[1] = nexttype[0];
@@ -3557,8 +3672,9 @@ I32 len;
     switch (*d) {
     case '_':
        if (d[1] == '_') {
-           if (strEQ(d,"__LINE__"))            return -KEY___LINE__;
            if (strEQ(d,"__FILE__"))            return -KEY___FILE__;
+           if (strEQ(d,"__LINE__"))            return -KEY___LINE__;
+           if (strEQ(d,"__PACKAGE__"))         return -KEY___PACKAGE__;
            if (strEQ(d,"__DATA__"))            return KEY___DATA__;
            if (strEQ(d,"__END__"))             return KEY___END__;
        }
@@ -4279,8 +4395,13 @@ I32 ck_uni;
        return s;
     }
     if (*s == '$' && s[1] &&
-      (isALPHA(s[1]) || strchr("$_{", s[1]) || strnEQ(s+1,"::",2)) )
-       return s;
+      (isALNUM(s[1]) || strchr("${", s[1]) || strnEQ(s+1,"::",2)) )
+    {
+       if (isDIGIT(s[1]) && lex_state == LEX_INTERPNORMAL)
+           deprecate("\"$$<digit>\" to mean \"${$}<digit>\"");
+       else
+           return s;
+    }
     if (*s == '{') {
        bracket = s;
        s++;
@@ -4291,20 +4412,26 @@ I32 ck_uni;
        *d = *s++;
     d[1] = '\0';
     if (*d == '^' && *s && (isUPPER(*s) || strchr("[\\]^_?", *s))) {
-       *d = *s++ ^ 64;
+       *d = toCTRL(*s);
+       s++;
     }
     if (bracket) {
        if (isSPACE(s[-1])) {
-           while (s < send && (*s == ' ' || *s == '\t')) s++;
-           *d = *s;
+           while (s < send) {
+               char ch = *s++;
+               if (ch != ' ' && ch != '\t') {
+                   *d = ch;
+                   break;
+               }
+           }
        }
-       if (isALPHA(*d) || *d == '_') {
+       if (isIDFIRST(*d)) {
            d++;
            while (isALNUM(*s) || *s == ':')
                *d++ = *s++;
            *d = '\0';
            while (s < send && (*s == ' ' || *s == '\t')) s++;
-           if ((*s == '[' || *s == '{')) {
+           if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) {
                if (dowarn && keyword(dest, d - dest)) {
                    char *brack = *s == '[' ? "[...]" : "{...}";
                    warn("Ambiguous use of %c{%s%s} resolved to %c%s%s",
@@ -4341,10 +4468,8 @@ void pmflag(pmfl,ch)
 U16* pmfl;
 int ch;
 {
-    if (ch == 'i') {
-       sawi = TRUE;
+    if (ch == 'i')
        *pmfl |= PMf_FOLD;
-    }
     else if (ch == 'g')
        *pmfl |= PMf_GLOBAL;
     else if (ch == 'o')
@@ -4371,14 +4496,14 @@ char *start;
        lex_stuff = Nullsv;
        croak("Search pattern not terminated");
     }
+
     pm = (PMOP*)newPMOP(OP_MATCH, 0);
     if (multi_open == '?')
        pm->op_pmflags |= PMf_ONCE;
-
     while (*s && strchr("iogmsx", *s))
        pmflag(&pm->op_pmflags,*s++);
-
     pm->op_pmpermflags = pm->op_pmflags;
+
     lex_op = (OP*)pm;
     yylval.ival = OP_MATCH;
     return s;
@@ -4390,6 +4515,7 @@ char *start;
 {
     register char *s;
     register PMOP *pm;
+    I32 first_start;
     I32 es = 0;
 
     yylval.ival = OP_NULL;
@@ -4406,6 +4532,7 @@ char *start;
     if (s[-1] == multi_open)
        s--;
 
+    first_start = multi_start;
     s = scan_str(s);
     if (!s) {
        if (lex_stuff)
@@ -4416,6 +4543,7 @@ char *start;
        lex_repl = Nullsv;
        croak("Substitution replacement not terminated");
     }
+    multi_start = first_start; /* so whole substitution is taken together */
 
     pm = (PMOP*)newPMOP(OP_SUBST, 0);
     while (*s && strchr("iogmsex", *s)) {
@@ -4456,8 +4584,6 @@ register PMOP *pm;
        ) {
        if (!(pm->op_pmregexp->reganch & ROPT_ANCH))
            pm->op_pmflags |= PMf_SCANFIRST;
-       else if (pm->op_pmflags & PMf_FOLD)
-           return;
        pm->op_pmshort = SvREFCNT_inc(pm->op_pmregexp->regstart);
        pm->op_pmslen = SvCUR(pm->op_pmshort);
     }
@@ -4475,9 +4601,11 @@ register PMOP *pm;
                return;
            }
        }
-       if (!pm->op_pmshort ||  /* promote the better string */
-         ((pm->op_pmflags & PMf_SCANFIRST) &&
-          (SvCUR(pm->op_pmshort) < SvCUR(pm->op_pmregexp->regmust)) )){
+       /* promote the better string */
+       if ((!pm->op_pmshort &&
+            !(pm->op_pmregexp->reganch & ROPT_ANCH_GPOS)) ||
+           ((pm->op_pmflags & PMf_SCANFIRST) &&
+            (SvCUR(pm->op_pmshort) < SvCUR(pm->op_pmregexp->regmust)))) {
            SvREFCNT_dec(pm->op_pmshort);               /* ok if null */
            pm->op_pmshort = pm->op_pmregexp->regmust;
            pm->op_pmslen = SvCUR(pm->op_pmshort);
@@ -4552,10 +4680,11 @@ register char *s;
     char term;
     register char *d;
     char *peek;
+    int outer = (rsfp && !lex_inwhat);
 
     s += 2;
     d = tokenbuf;
-    if (!rsfp)
+    if (!outer)
        *d++ = '\n';
     for (peek = s; *peek == ' ' || *peek == '\t'; peek++) ;
     if (*peek && strchr("`'\"",*peek)) {
@@ -4580,7 +4709,7 @@ register char *s;
     *d = '\0';
     len = d - tokenbuf;
     d = "\n";
-    if (rsfp || !(d=ninstr(s,bufend,d,d+1)))
+    if (outer || !(d=ninstr(s,bufend,d,d+1)))
        herewas = newSVpv(s,bufend-s);
     else
        s--, herewas = newSVpv(s,d-s);
@@ -4601,10 +4730,10 @@ register char *s;
     multi_start = curcop->cop_line;
     multi_open = multi_close = '<';
     term = *tokenbuf;
-    if (!rsfp) {
+    if (!outer) {
        d = s;
        while (s < bufend &&
-         (*s != term || memcmp(s,tokenbuf,len) != 0) ) {
+         (*s != term || memNE(s,tokenbuf,len)) ) {
            if (*s++ == '\n')
                curcop->cop_line++;
        }
@@ -4622,7 +4751,7 @@ register char *s;
     else
        sv_setpvn(tmpstr,"",0);   /* avoid "uninitialized" warning */
     while (s >= bufend) {      /* multiple line string? */
-       if (!rsfp ||
+       if (!outer ||
         !(oldoldbufptr = oldbufptr = s = linestart = filter_gets(linestr, rsfp, 0))) {
            curcop->cop_line = multi_start;
            missingterm(tokenbuf);
@@ -4637,7 +4766,7 @@ register char *s;
              (I32)curcop->cop_line,sv);
        }
        bufend = SvPVX(linestr) + SvCUR(linestr);
-       if (*s == term && memcmp(s,tokenbuf,len) == 0) {
+       if (*s == term && memEQ(s,tokenbuf,len)) {
            s = bufend - 1;
            *s = ' ';
            sv_catsv(linestr,herewas);
@@ -4817,7 +4946,7 @@ char *start;
 {
     register char *s = start;
     register char *d;
-    I32 tryi32;
+    I32 tryiv;
     double value;
     SV *sv;
     I32 floatit;
@@ -4916,12 +5045,13 @@ char *start;
        }
        *d = '\0';
        sv = NEWSV(92,0);
+       SET_NUMERIC_STANDARD();
        value = atof(tokenbuf);
-       tryi32 = I_32(value);
-       if (!floatit && (double)tryi32 == value)
-           sv_setiv(sv,tryi32);
+       tryiv = I_V(value);
+       if (!floatit && (double)tryiv == value)
+           sv_setiv(sv, tryiv);
        else
-           sv_setnv(sv,value);
+           sv_setnv(sv, value);
        break;
     }
 
@@ -5010,17 +5140,17 @@ set_csh()
 }
 
 int
-start_subparse()
+start_subparse(is_format, flags)
+I32 is_format;
+U32 flags;
 {
     int oldsavestack_ix = savestack_ix;
     CV* outsidecv = compcv;
     AV* comppadlist;
 
-#ifndef __QNX__
     if (compcv) {
        assert(SvTYPE(compcv) == SVt_PVCV);
     }
-#endif
     save_I32(&subline);
     save_item(subname);
     SAVEI32(padix);
@@ -5034,7 +5164,8 @@ start_subparse()
     SAVEI32(pad_reset_pending);
 
     compcv = (CV*)NEWSV(1104,0);
-    sv_upgrade((SV *)compcv, SVt_PVCV);
+    sv_upgrade((SV *)compcv, is_format ? SVt_PVFM : SVt_PVCV);
+    CvFLAGS(compcv) |= flags;
 
     comppad = newAV();
     comppad_name = newAV();
@@ -5071,44 +5202,56 @@ int
 yyerror(s)
 char *s;
 {
-    char tmpbuf[258];
-    char *tname = tmpbuf;
-
-    if (bufptr > oldoldbufptr && bufptr - oldoldbufptr < 200 &&
+    char wbuf[40];
+    char *where = NULL;
+    char *context = NULL;
+    int contlen = -1;
+
+    if (!yychar || (yychar == ';' && !rsfp))
+       where = "at EOF";
+    else if (bufptr > oldoldbufptr && bufptr - oldoldbufptr < 200 &&
       oldoldbufptr != oldbufptr && oldbufptr != bufptr) {
        while (isSPACE(*oldoldbufptr))
            oldoldbufptr++;
-       sprintf(tname,"near \"%.*s\"",bufptr - oldoldbufptr, oldoldbufptr);
+       context = oldoldbufptr;
+       contlen = bufptr - oldoldbufptr;
     }
     else if (bufptr > oldbufptr && bufptr - oldbufptr < 200 &&
       oldbufptr != bufptr) {
        while (isSPACE(*oldbufptr))
            oldbufptr++;
-       sprintf(tname,"near \"%.*s\"",bufptr - oldbufptr, oldbufptr);
+       context = oldbufptr;
+       contlen = bufptr - oldbufptr;
     }
     else if (yychar > 255)
-       tname = "next token ???";
-    else if (!yychar || (yychar == ';' && !rsfp))
-       (void)strcpy(tname,"at EOF");
+       where = "next token ???";
     else if ((yychar & 127) == 127) {
        if (lex_state == LEX_NORMAL ||
           (lex_state == LEX_KNOWNEXT && lex_defer == LEX_NORMAL))
-           (void)strcpy(tname,"at end of line");
+           where = "at end of line";
        else if (lex_inpat)
-           (void)strcpy(tname,"within pattern");
+           where = "within pattern";
        else
-           (void)strcpy(tname,"within string");
+           where = "within string";
     }
     else if (yychar < 32)
-       (void)sprintf(tname,"next char ^%c",yychar+64);
+       (void)sprintf(where = wbuf, "next char ^%c", toCTRL(yychar));
+    else if (isPRINT_LC(yychar))
+       (void)sprintf(where = wbuf, "next char %c", yychar);
+    else
+       (void)sprintf(where = wbuf, "next char \\%03o", yychar & 255);
+    if (contlen == -1)
+       contlen = strlen(where);
+    (void)sprintf(buf, "%s at %s line %d, ",
+                 s, SvPVX(GvSV(curcop->cop_filegv)), curcop->cop_line);
+    if (context)
+       (void)sprintf(buf+strlen(buf), "near \"%.*s\"\n", contlen, context);
     else
-       (void)sprintf(tname,"next char %c",yychar);
-    (void)sprintf(buf, "%s at %s line %d, %s\n",
-      s,SvPVX(GvSV(curcop->cop_filegv)),curcop->cop_line,tname);
-    if (curcop->cop_line == multi_end && multi_start < multi_end) {
+       (void)sprintf(buf+strlen(buf), "%s\n", where);
+    if (multi_start < multi_end && (U32)(curcop->cop_line - multi_end) <= 1) {
        sprintf(buf+strlen(buf),
-         "  (Might be a runaway multi-line %c%c string starting on line %ld)\n",
-         multi_open,multi_close,(long)multi_start);
+       "  (Might be a runaway multi-line %c%c string starting on line %ld)\n",
+               (int)multi_open,(int)multi_close,(long)multi_start);
         multi_end = 0;
     }
     if (in_eval & 2)