This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Locale-related pod patches, take 2
[perl5.git] / gv.c
CommitLineData
a0d0e21e 1/* gv.c
79072805 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * 'Mercy!' cried Gandalf. 'If the giving of information is to be the cure
12 * of your inquisitiveness, I shall spend all the rest of my days answering
13 * you. What more do you want to know?'
14 * 'The names of all the stars, and of all living things, and the whole
15 * history of Middle-earth and Over-heaven and of the Sundering Seas,'
16 * laughed Pippin.
79072805
LW
17 */
18
19#include "EXTERN.h"
20#include "perl.h"
21
71be2cbc 22EXT char rcsid[];
93a17b20 23
79072805
LW
24GV *
25gv_AVadd(gv)
26register GV *gv;
27{
a0d0e21e
LW
28 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
29 croak("Bad symbol for array");
79072805
LW
30 if (!GvAV(gv))
31 GvAV(gv) = newAV();
32 return gv;
33}
34
35GV *
36gv_HVadd(gv)
37register GV *gv;
38{
a0d0e21e
LW
39 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
40 croak("Bad symbol for hash");
79072805 41 if (!GvHV(gv))
463ee0b2 42 GvHV(gv) = newHV();
79072805
LW
43 return gv;
44}
45
46GV *
a0d0e21e
LW
47gv_IOadd(gv)
48register GV *gv;
49{
50 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
51 croak("Bad symbol for filehandle");
52 if (!GvIOp(gv))
53 GvIOp(gv) = newIO();
54 return gv;
55}
56
57GV *
79072805
LW
58gv_fetchfile(name)
59char *name;
60{
61 char tmpbuf[1200];
62 GV *gv;
63
8990e307 64 sprintf(tmpbuf,"::_<%s", name);
85e6fe83 65 gv = gv_fetchpv(tmpbuf, TRUE, SVt_PVGV);
79072805 66 sv_setpv(GvSV(gv), name);
a0d0e21e 67 if (*name == '/' && (instr(name,"/lib/") || instr(name,".pm")))
a5f75d66 68 GvMULTI_on(gv);
79072805 69 if (perldb)
93a17b20 70 hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L');
79072805
LW
71 return gv;
72}
73
463ee0b2
LW
74void
75gv_init(gv, stash, name, len, multi)
76GV *gv;
77HV *stash;
78char *name;
79STRLEN len;
80int multi;
81{
82 register GP *gp;
83
dc437b57 84 sv_upgrade((SV*)gv, SVt_PVGV);
463ee0b2
LW
85 if (SvLEN(gv))
86 Safefree(SvPVX(gv));
87 Newz(602,gp, 1, GP);
8990e307 88 GvGP(gv) = gp_ref(gp);
463ee0b2
LW
89 GvREFCNT(gv) = 1;
90 GvSV(gv) = NEWSV(72,0);
91 GvLINE(gv) = curcop->cop_line;
8990e307 92 GvFILEGV(gv) = curcop->cop_filegv;
463ee0b2
LW
93 GvEGV(gv) = gv;
94 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
95 GvSTASH(gv) = stash;
a0d0e21e 96 GvNAME(gv) = savepvn(name, len);
463ee0b2
LW
97 GvNAMELEN(gv) = len;
98 if (multi)
a5f75d66 99 GvMULTI_on(gv);
463ee0b2
LW
100}
101
a0d0e21e
LW
102static void
103gv_init_sv(gv, sv_type)
104GV* gv;
105I32 sv_type;
106{
107 switch (sv_type) {
108 case SVt_PVIO:
109 (void)GvIOn(gv);
110 break;
111 case SVt_PVAV:
112 (void)GvAVn(gv);
113 break;
114 case SVt_PVHV:
115 (void)GvHVn(gv);
116 break;
117 }
118}
119
79072805 120GV *
a0d0e21e 121gv_fetchmeth(stash, name, len, level)
79072805
LW
122HV* stash;
123char* name;
463ee0b2 124STRLEN len;
a0d0e21e 125I32 level;
79072805
LW
126{
127 AV* av;
463ee0b2 128 GV* topgv;
79072805 129 GV* gv;
463ee0b2 130 GV** gvp;
a0d0e21e 131 HV* lastchance;
748a9306 132 CV* cv;
a0d0e21e
LW
133
134 if (!stash)
135 return 0;
136 if (level > 100)
137 croak("Recursive inheritance detected");
463ee0b2
LW
138
139 gvp = (GV**)hv_fetch(stash, name, len, TRUE);
140
141 DEBUG_o( deb("Looking for method %s in package %s\n",name,HvNAME(stash)) );
142 topgv = *gvp;
143 if (SvTYPE(topgv) != SVt_PVGV)
144 gv_init(topgv, stash, name, len, TRUE);
145
7a4c00b4
PP
146 if (cv = GvCV(topgv)) {
147 if (CvXSUB(cv) || CvROOT(cv) || CvGV(cv)) { /* Not deleted, possibly autoloaded. */
148 if (GvCVGEN(topgv) >= sub_generation)
149 return topgv; /* valid cached inheritance */
150 if (!GvCVGEN(topgv)) { /* not an inheritance cache */
151 return topgv;
152 }
748a9306 153 }
7a4c00b4
PP
154 /* stale cached entry, just junk it */
155 SvREFCNT_dec(cv);
156 GvCV(topgv) = cv = 0;
157 GvCVGEN(topgv) = 0;
463ee0b2 158 }
7a4c00b4 159 /* Now cv = 0, and there is no cv in topgv. */
79072805
LW
160
161 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
162 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
163 SV** svp = AvARRAY(av);
164 I32 items = AvFILL(av) + 1;
165 while (items--) {
79072805 166 SV* sv = *svp++;
a0d0e21e 167 HV* basestash = gv_stashsv(sv, FALSE);
9bbf4081 168 if (!basestash) {
79072805 169 if (dowarn)
a0d0e21e 170 warn("Can't locate package %s for @%s::ISA",
463ee0b2 171 SvPVX(sv), HvNAME(stash));
79072805
LW
172 continue;
173 }
a0d0e21e 174 gv = gv_fetchmeth(basestash, name, len, level + 1);
463ee0b2
LW
175 if (gv) {
176 GvCV(topgv) = GvCV(gv); /* cache the CV */
177 GvCVGEN(topgv) = sub_generation; /* valid for now */
7a4c00b4 178 SvREFCNT_inc(GvCV(gv));
79072805 179 return gv;
463ee0b2 180 }
79072805
LW
181 }
182 }
a0d0e21e
LW
183
184 if (!level) {
dc437b57 185 if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) {
a0d0e21e
LW
186 if (gv = gv_fetchmeth(lastchance, name, len, level + 1)) {
187 GvCV(topgv) = GvCV(gv); /* cache the CV */
188 GvCVGEN(topgv) = sub_generation; /* valid for now */
7a4c00b4 189 SvREFCNT_inc(GvCV(gv));
a0d0e21e
LW
190 return gv;
191 }
192 }
193 }
194
79072805
LW
195 return 0;
196}
197
198GV *
463ee0b2
LW
199gv_fetchmethod(stash, name)
200HV* stash;
201char* name;
202{
203 register char *nend;
a0d0e21e
LW
204 char *nsplit = 0;
205 GV* gv;
463ee0b2
LW
206
207 for (nend = name; *nend; nend++) {
a0d0e21e
LW
208 if (*nend == ':' || *nend == '\'')
209 nsplit = nend;
210 }
211 if (nsplit) {
212 char ch;
213 char *origname = name;
214 name = nsplit + 1;
215 ch = *nsplit;
216 if (*nsplit == ':')
217 --nsplit;
218 *nsplit = '\0';
4633a7c4
LW
219 if (strEQ(origname,"SUPER")) {
220 /* Degenerate case ->SUPER::method should really lookup in original stash */
37120919 221 SV *tmpstr = sv_2mortal(newSVpv(HvNAME(curcop->cop_stash),0));
4633a7c4 222 sv_catpvn(tmpstr, "::SUPER", 7);
dc437b57 223 stash = gv_stashpvn(SvPVX(tmpstr),SvCUR(tmpstr),TRUE);
4633a7c4
LW
224 *nsplit = ch;
225 DEBUG_o( deb("Treating %s as %s::%s\n",origname,HvNAME(stash),name) );
226 } else {
dc437b57 227 stash = gv_stashpvn(origname, nsplit - origname, TRUE);
4633a7c4
LW
228 *nsplit = ch;
229 }
a0d0e21e
LW
230 }
231 gv = gv_fetchmeth(stash, name, nend - name, 0);
4633a7c4
LW
232
233 if (!gv) {
234 /* Failed obvious case - look for SUPER as last element of stash's name */
235 char *packname = HvNAME(stash);
236 STRLEN len = strlen(packname);
760ac839 237 if (len >= 7 && strEQ(packname+len-7,"::SUPER")) {
4633a7c4
LW
238 /* Now look for @.*::SUPER::ISA */
239 GV** gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
760ac839 240 len -= 7;
4633a7c4
LW
241 if (!gvp || (gv = *gvp) == (GV*)&sv_undef || !GvAV(gv)) {
242 /* No @ISA in package ending in ::SUPER - drop suffix
243 and see if there is an @ISA there
244 */
245 HV *basestash;
dc437b57 246 char ch = packname[len];
4633a7c4 247 AV *av;
dc437b57
PP
248 packname[len] = '\0';
249 basestash = gv_stashpvn(packname, len, TRUE);
250 packname[len] = ch;
4633a7c4
LW
251 gvp = (GV**)hv_fetch(basestash,"ISA",3,FALSE);
252 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
253 /* Okay found @ISA after dropping the SUPER, alias it */
254 SV *tmpstr = sv_2mortal(newSVpv(HvNAME(stash),0));
255 sv_catpvn(tmpstr, "::ISA", 5);
256 gv = gv_fetchpv(SvPV(tmpstr,na),TRUE,SVt_PVGV);
257 if (gv) {
258 GvAV(gv) = (AV*)SvREFCNT_inc(av);
259 /* ... and re-try lookup */
260 gv = gv_fetchmeth(stash, name, nend - name, 0);
261 } else {
262 croak("Cannot create %s::ISA",HvNAME(stash));
263 }
264 }
265 }
266 }
267 }
268
a0d0e21e
LW
269 if (!gv) {
270 CV* cv;
271
702887b4 272 if (strEQ(name,"import"))
dc437b57 273 gv = (GV*)&sv_yes;
a0d0e21e
LW
274 else if (strNE(name, "AUTOLOAD")) {
275 gv = gv_fetchmeth(stash, "AUTOLOAD", 8, 0);
276 if (gv && (cv = GvCV(gv))) { /* One more chance... */
748a9306 277 SV *tmpstr = sv_2mortal(newSVpv(HvNAME(stash),0));
a0d0e21e
LW
278 sv_catpvn(tmpstr,"::", 2);
279 sv_catpvn(tmpstr, name, nend - name);
280 sv_setsv(GvSV(CvGV(cv)), tmpstr);
bbce6d69 281 SvTAINTED_off(GvSV(CvGV(cv)));
a0d0e21e 282 }
463ee0b2
LW
283 }
284 }
a0d0e21e
LW
285 return gv;
286}
287
288HV*
289gv_stashpv(name,create)
290char *name;
291I32 create;
292{
dc437b57
PP
293 return gv_stashpvn(name, strlen(name), create);
294}
295
296HV*
297gv_stashpvn(name,namelen,create)
298char *name;
299U32 namelen;
300I32 create;
301{
302 char tmpbuf[1203];
a0d0e21e
LW
303 HV *stash;
304 GV *tmpgv;
dc437b57
PP
305
306 if (namelen > 1200) {
307 namelen = 1200;
308#ifdef VMS
309 warn("Weird package name \"%s\" truncated", name);
310#else
311 warn("Weird package name \"%.*s...\" truncated", namelen, name);
312#endif
313 }
314 Copy(name,tmpbuf,namelen,char);
315 tmpbuf[namelen++] = ':';
316 tmpbuf[namelen++] = ':';
317 tmpbuf[namelen] = '\0';
a0d0e21e
LW
318 tmpgv = gv_fetchpv(tmpbuf,create, SVt_PVHV);
319 if (!tmpgv)
320 return 0;
321 if (!GvHV(tmpgv))
322 GvHV(tmpgv) = newHV();
323 stash = GvHV(tmpgv);
324 if (!HvNAME(stash))
325 HvNAME(stash) = savepv(name);
326 return stash;
463ee0b2
LW
327}
328
a0d0e21e
LW
329HV*
330gv_stashsv(sv,create)
331SV *sv;
332I32 create;
333{
dc437b57
PP
334 register char *ptr;
335 STRLEN len;
336 ptr = SvPV(sv,len);
337 return gv_stashpvn(ptr, len, create);
a0d0e21e
LW
338}
339
340
463ee0b2 341GV *
a0d0e21e 342gv_fetchpv(nambeg,add,sv_type)
463ee0b2 343char *nambeg;
79072805 344I32 add;
a0d0e21e 345I32 sv_type;
79072805 346{
463ee0b2
LW
347 register char *name = nambeg;
348 register GV *gv = 0;
79072805 349 GV**gvp;
79072805
LW
350 I32 len;
351 register char *namend;
463ee0b2 352 HV *stash = 0;
79072805 353 bool global = FALSE;
85e6fe83 354 char *tmpbuf;
79072805 355
c07a80fd
PP
356 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
357 name++;
358
79072805 359 for (namend = name; *namend; namend++) {
463ee0b2
LW
360 if ((*namend == '\'' && namend[1]) ||
361 (*namend == ':' && namend[1] == ':'))
362 {
463ee0b2
LW
363 if (!stash)
364 stash = defstash;
dc437b57 365 if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */
a0d0e21e 366 return Nullgv;
463ee0b2 367
85e6fe83
LW
368 len = namend - name;
369 if (len > 0) {
a0d0e21e
LW
370 New(601, tmpbuf, len+3, char);
371 Copy(name, tmpbuf, len, char);
372 tmpbuf[len++] = ':';
373 tmpbuf[len++] = ':';
374 tmpbuf[len] = '\0';
463ee0b2 375 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
85e6fe83 376 Safefree(tmpbuf);
463ee0b2
LW
377 if (!gvp || *gvp == (GV*)&sv_undef)
378 return Nullgv;
379 gv = *gvp;
85e6fe83 380
463ee0b2 381 if (SvTYPE(gv) == SVt_PVGV)
a5f75d66 382 GvMULTI_on(gv);
a0d0e21e
LW
383 else if (!add)
384 return Nullgv;
463ee0b2
LW
385 else
386 gv_init(gv, stash, nambeg, namend - nambeg, (add & 2));
85e6fe83 387
463ee0b2
LW
388 if (!(stash = GvHV(gv)))
389 stash = GvHV(gv) = newHV();
85e6fe83 390
463ee0b2 391 if (!HvNAME(stash))
a0d0e21e 392 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
463ee0b2
LW
393 }
394
395 if (*namend == ':')
396 namend++;
397 namend++;
398 name = namend;
399 if (!*name)
dc437b57 400 return gv ? gv : (GV*)*hv_fetch(defstash, "main::", 6, TRUE);
79072805 401 }
79072805 402 }
a0d0e21e
LW
403 len = namend - name;
404 if (!len)
405 len = 1;
463ee0b2
LW
406
407 /* No stash in name, so see how we can default */
408
409 if (!stash) {
410 if (isIDFIRST(*name)) {
411 if (isUPPER(*name)) {
412 if (*name > 'I') {
413 if (*name == 'S' && (
414 strEQ(name, "SIG") ||
415 strEQ(name, "STDIN") ||
416 strEQ(name, "STDOUT") ||
417 strEQ(name, "STDERR") ))
418 global = TRUE;
419 }
420 else if (*name > 'E') {
421 if (*name == 'I' && strEQ(name, "INC"))
422 global = TRUE;
423 }
424 else if (*name > 'A') {
425 if (*name == 'E' && strEQ(name, "ENV"))
426 global = TRUE;
427 }
428 else if (*name == 'A' && (
429 strEQ(name, "ARGV") ||
430 strEQ(name, "ARGVOUT") ))
431 global = TRUE;
432 }
433 else if (*name == '_' && !name[1])
434 global = TRUE;
435 if (global)
436 stash = defstash;
85e6fe83 437 else if ((COP*)curcop == &compiling) {
a0d0e21e 438 stash = curstash;
748a9306
LW
439 if (add && (hints & HINT_STRICT_VARS) &&
440 sv_type != SVt_PVCV &&
441 sv_type != SVt_PVGV &&
4633a7c4 442 sv_type != SVt_PVFM &&
c07a80fd 443 sv_type != SVt_PVIO &&
377b8fbc 444 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
748a9306 445 {
4633a7c4
LW
446 gvp = (GV**)hv_fetch(stash,name,len,0);
447 if (!gvp ||
a5f75d66
AD
448 *gvp == (GV*)&sv_undef ||
449 SvTYPE(*gvp) != SVt_PVGV)
450 {
4633a7c4 451 stash = 0;
a5f75d66
AD
452 }
453 else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) ||
454 sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) ||
455 sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) )
4633a7c4 456 {
a5f75d66 457 warn("Variable \"%c%s\" is not imported",
4633a7c4
LW
458 sv_type == SVt_PVAV ? '@' :
459 sv_type == SVt_PVHV ? '%' : '$',
460 name);
461 if (GvCV(*gvp))
462 warn("(Did you mean &%s instead?)\n", name);
a0d0e21e 463 stash = 0;
4633a7c4 464 }
a0d0e21e 465 }
85e6fe83 466 }
463ee0b2
LW
467 else
468 stash = curcop->cop_stash;
469 }
470 else
471 stash = defstash;
472 }
473
474 /* By this point we should have a stash and a name */
475
a0d0e21e
LW
476 if (!stash) {
477 if (add) {
478 warn("Global symbol \"%s\" requires explicit package name", name);
479 ++error_count;
480 stash = curstash ? curstash : defstash; /* avoid core dumps */
481 }
482 else
483 return Nullgv;
484 }
485
486 if (!SvREFCNT(stash)) /* symbol table under destruction */
487 return Nullgv;
488
79072805
LW
489 gvp = (GV**)hv_fetch(stash,name,len,add);
490 if (!gvp || *gvp == (GV*)&sv_undef)
491 return Nullgv;
492 gv = *gvp;
493 if (SvTYPE(gv) == SVt_PVGV) {
a0d0e21e 494 if (add) {
a5f75d66 495 GvMULTI_on(gv);
a0d0e21e
LW
496 gv_init_sv(gv, sv_type);
497 }
79072805
LW
498 return gv;
499 }
93a17b20
LW
500
501 /* Adding a new symbol */
502
a0d0e21e
LW
503 if (add & 4)
504 warn("Had to create %s unexpectedly", nambeg);
463ee0b2 505 gv_init(gv, stash, name, len, add & 2);
a0d0e21e 506 gv_init_sv(gv, sv_type);
93a17b20
LW
507
508 /* set up magic where warranted */
509 switch (*name) {
a0d0e21e
LW
510 case 'A':
511 if (strEQ(name, "ARGV")) {
512 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
513 }
514 break;
515
ed6116ce
LW
516 case 'a':
517 case 'b':
518 if (len == 1)
a5f75d66 519 GvMULTI_on(gv);
ed6116ce 520 break;
a0d0e21e
LW
521 case 'E':
522 if (strnEQ(name, "EXPORT", 6))
a5f75d66 523 GvMULTI_on(gv);
a0d0e21e 524 break;
463ee0b2
LW
525 case 'I':
526 if (strEQ(name, "ISA")) {
527 AV* av = GvAVn(gv);
a5f75d66 528 GvMULTI_on(gv);
a0d0e21e
LW
529 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
530 if (add & 2 && strEQ(nambeg,"AnyDBM_File::ISA") && AvFILL(av) == -1)
85e6fe83 531 {
a0d0e21e
LW
532 char *pname;
533 av_push(av, newSVpv(pname = "NDBM_File",0));
dc437b57 534 gv_stashpvn(pname, 9, TRUE);
a0d0e21e 535 av_push(av, newSVpv(pname = "DB_File",0));
dc437b57 536 gv_stashpvn(pname, 7, TRUE);
a0d0e21e 537 av_push(av, newSVpv(pname = "GDBM_File",0));
dc437b57 538 gv_stashpvn(pname, 9, TRUE);
a0d0e21e 539 av_push(av, newSVpv(pname = "SDBM_File",0));
dc437b57 540 gv_stashpvn(pname, 9, TRUE);
a0d0e21e 541 av_push(av, newSVpv(pname = "ODBM_File",0));
dc437b57 542 gv_stashpvn(pname, 9, TRUE);
85e6fe83 543 }
463ee0b2
LW
544 }
545 break;
a0d0e21e
LW
546#ifdef OVERLOAD
547 case 'O':
548 if (strEQ(name, "OVERLOAD")) {
549 HV* hv = GvHVn(gv);
a5f75d66 550 GvMULTI_on(gv);
a0d0e21e
LW
551 sv_magic((SV*)hv, (SV*)gv, 'A', 0, 0);
552 }
553 break;
554#endif /* OVERLOAD */
93a17b20
LW
555 case 'S':
556 if (strEQ(name, "SIG")) {
557 HV *hv;
dc437b57 558 I32 i;
93a17b20 559 siggv = gv;
a5f75d66 560 GvMULTI_on(siggv);
93a17b20
LW
561 hv = GvHVn(siggv);
562 hv_magic(hv, siggv, 'S');
dc437b57
PP
563 for(i=1;sig_name[i];i++) {
564 SV ** init;
565 init=hv_fetch(hv,sig_name[i],strlen(sig_name[i]),1);
566 if(init)
567 sv_setsv(*init,&sv_undef);
568 psig_ptr[i] = 0;
569 psig_name[i] = 0;
570 }
93a17b20
LW
571 /* initialize signal stack */
572 signalstack = newAV();
93a17b20 573 AvREAL_off(signalstack);
a0d0e21e
LW
574 av_extend(signalstack, 30);
575 av_fill(signalstack, 0);
93a17b20
LW
576 }
577 break;
578
579 case '&':
463ee0b2
LW
580 if (len > 1)
581 break;
93a17b20
LW
582 ampergv = gv;
583 sawampersand = TRUE;
a0d0e21e 584 goto ro_magicalize;
93a17b20
LW
585
586 case '`':
463ee0b2
LW
587 if (len > 1)
588 break;
93a17b20
LW
589 leftgv = gv;
590 sawampersand = TRUE;
a0d0e21e 591 goto ro_magicalize;
93a17b20
LW
592
593 case '\'':
463ee0b2
LW
594 if (len > 1)
595 break;
93a17b20
LW
596 rightgv = gv;
597 sawampersand = TRUE;
a0d0e21e 598 goto ro_magicalize;
93a17b20
LW
599
600 case ':':
463ee0b2
LW
601 if (len > 1)
602 break;
93a17b20
LW
603 sv_setpv(GvSV(gv),chopset);
604 goto magicalize;
605
93a17b20 606 case '#':
a0d0e21e
LW
607 case '*':
608 if (dowarn && len == 1 && sv_type == SVt_PV)
609 warn("Use of $%s is deprecated", name);
610 /* FALL THROUGH */
611 case '[':
612 case '!':
93a17b20
LW
613 case '?':
614 case '^':
615 case '~':
616 case '=':
617 case '-':
618 case '%':
619 case '.':
93a17b20
LW
620 case '(':
621 case ')':
622 case '<':
623 case '>':
624 case ',':
625 case '\\':
626 case '/':
93a17b20 627 case '|':
748a9306 628 case '\001':
93a17b20 629 case '\004':
ad8898e0 630 case '\005':
748a9306 631 case '\006':
a0d0e21e 632 case '\010':
ad8898e0 633 case '\017':
93a17b20
LW
634 case '\t':
635 case '\020':
636 case '\024':
637 case '\027':
463ee0b2
LW
638 if (len > 1)
639 break;
640 goto magicalize;
641
a0d0e21e 642 case '+':
463ee0b2
LW
643 case '1':
644 case '2':
645 case '3':
646 case '4':
647 case '5':
648 case '6':
649 case '7':
650 case '8':
651 case '9':
a0d0e21e
LW
652 ro_magicalize:
653 SvREADONLY_on(GvSV(gv));
93a17b20 654 magicalize:
463ee0b2 655 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
93a17b20
LW
656 break;
657
658 case '\014':
463ee0b2
LW
659 if (len > 1)
660 break;
93a17b20
LW
661 sv_setpv(GvSV(gv),"\f");
662 formfeed = GvSV(gv);
663 break;
664 case ';':
463ee0b2
LW
665 if (len > 1)
666 break;
93a17b20
LW
667 sv_setpv(GvSV(gv),"\034");
668 break;
463ee0b2
LW
669 case ']':
670 if (len == 1) {
93a17b20
LW
671 SV *sv;
672 sv = GvSV(gv);
673 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 674 sv_setpv(sv, patchlevel);
93a17b20
LW
675 }
676 break;
79072805 677 }
93a17b20 678 return gv;
79072805
LW
679}
680
681void
f6aff53a 682gv_fullname3(sv, gv, prefix)
79072805
LW
683SV *sv;
684GV *gv;
f967eb5f 685char *prefix;
79072805
LW
686{
687 HV *hv = GvSTASH(gv);
f967eb5f
PP
688 if (!hv) {
689 SvOK_off(sv);
79072805 690 return;
f967eb5f
PP
691 }
692 sv_setpv(sv, prefix ? prefix : "");
79072805 693 sv_catpv(sv,HvNAME(hv));
463ee0b2 694 sv_catpvn(sv,"::", 2);
79072805
LW
695 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
696}
697
698void
f6aff53a 699gv_efullname3(sv, gv, prefix)
79072805
LW
700SV *sv;
701GV *gv;
f967eb5f 702char *prefix;
79072805 703{
f967eb5f 704 GV *egv = GvEGV(gv);
748a9306
LW
705 if (!egv)
706 egv = gv;
f6aff53a
PP
707 gv_fullname3(sv, egv, prefix);
708}
709
710/* XXX compatibility with versions <= 5.003. */
711void
712gv_fullname(sv,gv)
713SV *sv;
714GV *gv;
715{
716 gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : "");
717}
718
719/* XXX compatibility with versions <= 5.003. */
720void
721gv_efullname(sv,gv)
722SV *sv;
723GV *gv;
724{
725 gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : "");
79072805
LW
726}
727
728IO *
729newIO()
730{
731 IO *io;
8990e307
LW
732 GV *iogv;
733
734 io = (IO*)NEWSV(0,0);
a0d0e21e 735 sv_upgrade((SV *)io,SVt_PVIO);
8990e307
LW
736 SvREFCNT(io) = 1;
737 SvOBJECT_on(io);
dc437b57 738 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
8990e307 739 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
79072805
LW
740 return io;
741}
742
743void
463ee0b2
LW
744gv_check(stash)
745HV* stash;
79072805
LW
746{
747 register HE *entry;
748 register I32 i;
749 register GV *gv;
463ee0b2 750 HV *hv;
a0d0e21e 751 GV *filegv;
463ee0b2 752
8990e307
LW
753 if (!HvARRAY(stash))
754 return;
a0d0e21e 755 for (i = 0; i <= (I32) HvMAX(stash); i++) {
dc437b57
PP
756 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
757 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
758 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
a0d0e21e
LW
759 {
760 if (hv != defstash)
761 gv_check(hv); /* nested package */
762 }
dc437b57
PP
763 else if (isALPHA(*HeKEY(entry))) {
764 gv = (GV*)HeVAL(entry);
a5f75d66 765 if (GvMULTI(gv))
463ee0b2
LW
766 continue;
767 curcop->cop_line = GvLINE(gv);
a0d0e21e
LW
768 filegv = GvFILEGV(gv);
769 curcop->cop_filegv = filegv;
a5f75d66 770 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
8990e307 771 continue;
dc437b57 772 warn("Name \"%s::%s\" used only once: possible typo",
a0d0e21e 773 HvNAME(stash), GvNAME(gv));
463ee0b2 774 }
79072805
LW
775 }
776 }
777}
778
779GV *
a0d0e21e
LW
780newGVgen(pack)
781char *pack;
79072805 782{
a0d0e21e 783 (void)sprintf(tokenbuf,"%s::_GEN_%ld",pack,(long)gensym++);
85e6fe83 784 return gv_fetchpv(tokenbuf,TRUE, SVt_PVGV);
79072805
LW
785}
786
787/* hopefully this is only called on local symbol table entries */
788
789GP*
790gp_ref(gp)
791GP* gp;
792{
793 gp->gp_refcnt++;
794 return gp;
795
796}
797
798void
799gp_free(gv)
800GV* gv;
801{
79072805 802 GP* gp;
377b8fbc 803 CV* cv;
79072805
LW
804
805 if (!gv || !(gp = GvGP(gv)))
806 return;
807 if (gp->gp_refcnt == 0) {
808 warn("Attempt to free unreferenced glob pointers");
809 return;
810 }
748a9306
LW
811 if (--gp->gp_refcnt > 0) {
812 if (gp->gp_egv == gv)
813 gp->gp_egv = 0;
79072805 814 return;
748a9306 815 }
79072805 816
8990e307
LW
817 SvREFCNT_dec(gp->gp_sv);
818 SvREFCNT_dec(gp->gp_av);
819 SvREFCNT_dec(gp->gp_hv);
377b8fbc 820 SvREFCNT_dec(gp->gp_io);
8990e307
LW
821 if ((cv = gp->gp_cv) && !GvCVGEN(gv))
822 SvREFCNT_dec(cv);
748a9306
LW
823 SvREFCNT_dec(gp->gp_form);
824
79072805
LW
825 Safefree(gp);
826 GvGP(gv) = 0;
827}
828
829#if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
830#define MICROPORT
831#endif
832
833#ifdef MICROPORT /* Microport 2.4 hack */
834AV *GvAVn(gv)
835register GV *gv;
836{
837 if (GvGP(gv)->gp_av)
838 return GvGP(gv)->gp_av;
839 else
840 return GvGP(gv_AVadd(gv))->gp_av;
841}
842
843HV *GvHVn(gv)
844register GV *gv;
845{
846 if (GvGP(gv)->gp_hv)
847 return GvGP(gv)->gp_hv;
848 else
849 return GvGP(gv_HVadd(gv))->gp_hv;
850}
851#endif /* Microport 2.4 hack */
a0d0e21e
LW
852
853#ifdef OVERLOAD
854/* Updates and caches the CV's */
855
856bool
857Gv_AMupdate(stash)
858HV* stash;
859{
860 GV** gvp;
861 HV* hv;
862 GV* gv;
863 CV* cv;
864 MAGIC* mg=mg_find((SV*)stash,'c');
748a9306 865 AMT *amtp=mg ? (AMT*)mg->mg_ptr: NULL;
a0d0e21e
LW
866
867 if (mg && (amtp=((AMT*)(mg->mg_ptr)))->was_ok_am == amagic_generation &&
868 amtp->was_ok_sub == sub_generation)
869 return HV_AMAGIC(stash)? TRUE: FALSE;
870 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE);
748a9306
LW
871 if (amtp && amtp->table) {
872 int i;
873 for (i=1;i<NofAMmeth*2;i++) {
874 if (amtp->table[i]) {
875 SvREFCNT_dec(amtp->table[i]);
876 }
877 }
878 }
a0d0e21e
LW
879 sv_unmagic((SV*)stash, 'c');
880
881 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
882
883 if (gvp && ((gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv)))) {
884 int filled=0;
885 int i;
886 char *cp;
887 AMT amt;
888 SV* sv;
889 SV** svp;
dc437b57 890 GV** gvp;
a0d0e21e
LW
891
892/* if (*(svp)==(SV*)amagic_generation && *(svp+1)==(SV*)sub_generation) {
893 DEBUG_o( deb("Overload magic in package %s up-to-date\n",HvNAME(stash))
894);
895 return HV_AMAGIC(stash)? TRUE: FALSE;
896 }*/
897
898 amt.was_ok_am=amagic_generation;
899 amt.was_ok_sub=sub_generation;
900 amt.fallback=AMGfallNO;
901
902 /* Work with "fallback" key, which we assume to be first in AMG_names */
903
904 if ((cp=((char**)(*AMG_names))[0]) &&
905 (svp=(SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
906 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
907 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
908 }
909
910 for (i=1;i<NofAMmeth*2;i++) {
911 cv=0;
912
913 if ( (cp=((char**)(*AMG_names))[i]) ) {
914 svp=(SV**)hv_fetch(hv,cp,strlen(cp),FALSE);
dc437b57 915 if (svp && ((sv = *svp) != &sv_undef)) {
a0d0e21e
LW
916 switch (SvTYPE(sv)) {
917 default:
918 if (!SvROK(sv)) {
919 if (!SvOK(sv)) break;
748a9306 920 gv = gv_fetchmethod(stash, SvPV(sv, na));
a0d0e21e
LW
921 if (gv) cv = GvCV(gv);
922 break;
923 }
924 cv = (CV*)SvRV(sv);
925 if (SvTYPE(cv) == SVt_PVCV)
926 break;
927 /* FALL THROUGH */
928 case SVt_PVHV:
929 case SVt_PVAV:
2dd2a305 930 die("Not a subroutine reference in overload table");
a0d0e21e
LW
931 return FALSE;
932 case SVt_PVCV:
933 cv = (CV*)sv;
934 break;
935 case SVt_PVGV:
936 if (!(cv = GvCV((GV*)sv)))
937 cv = sv_2cv(sv, &stash, &gv, TRUE);
938 break;
939 }
940 if (cv) filled=1;
941 else {
06b43eb7 942 die("Method for operation %s not found in package %.256s during blessing\n",
a0d0e21e
LW
943 cp,HvNAME(stash));
944 return FALSE;
945 }
946 }
947 }
748a9306 948 amt.table[i]=(CV*)SvREFCNT_inc(cv);
a0d0e21e
LW
949 }
950 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(amt));
951 if (filled) {
952/* HV_badAMAGIC_off(stash);*/
953 HV_AMAGIC_on(stash);
954 return TRUE;
955 }
956 }
957/*HV_badAMAGIC_off(stash);*/
958 HV_AMAGIC_off(stash);
959 return FALSE;
960}
961
962/* During call to this subroutine stack can be reallocated. It is
963 * advised to call SPAGAIN macro in your code after call */
964
965SV*
966amagic_call(left,right,method,flags)
967SV* left;
968SV* right;
969int method;
970int flags;
971{
972 MAGIC *mg;
973 CV *cv;
974 CV **cvp=NULL, **ocvp=NULL;
975 AMT *amtp, *oamtp;
976 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
748a9306 977 int postpr=0, inc_dec_ass=0, assignshift=assign?1:0;
a0d0e21e
LW
978 HV* stash;
979 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
980 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
981 && (ocvp = cvp = ((oamtp=amtp=(AMT*)mg->mg_ptr)->table))
748a9306
LW
982 && ((cv = cvp[off=method+assignshift])
983 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
984 * usual method */
985 (fl = 1, cv = cvp[off=method])))) {
a0d0e21e
LW
986 lr = -1; /* Call method for left argument */
987 } else {
988 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
989 int logic;
990
991 /* look for substituted methods */
992 switch (method) {
993 case inc_amg:
748a9306 994 if (((cv = cvp[off=add_ass_amg]) && (inc_dec_ass=1))
a0d0e21e
LW
995 || ((cv = cvp[off=add_amg]) && (postpr=1))) {
996 right = &sv_yes; lr = -1; assign = 1;
997 }
998 break;
999 case dec_amg:
748a9306 1000 if (((cv = cvp[off=subtr_ass_amg]) && (inc_dec_ass=1))
a0d0e21e
LW
1001 || ((cv = cvp[off=subtr_amg]) && (postpr=1))) {
1002 right = &sv_yes; lr = -1; assign = 1;
1003 }
1004 break;
1005 case bool__amg:
1006 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
1007 break;
1008 case numer_amg:
1009 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1010 break;
1011 case string_amg:
1012 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1013 break;
dc437b57
PP
1014 case not_amg:
1015 (void)((cv = cvp[off=bool__amg])
1016 || (cv = cvp[off=numer_amg])
1017 || (cv = cvp[off=string_amg]));
1018 postpr = 1;
1019 break;
748a9306
LW
1020 case copy_amg:
1021 {
1022 SV* ref=SvRV(left);
1023 if (!SvROK(ref) && SvTYPE(ref) <= SVt_PVMG) { /* Just to be
1024 * extra
1025 * causious,
1026 * maybe in some
1027 * additional
1028 * cases sv_setsv
1029 * is safe too */
1030 SV* newref = newSVsv(ref);
1031 SvOBJECT_on(newref);
1032 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(ref));
1033 return newref;
1034 }
1035 }
1036 break;
a0d0e21e 1037 case abs_amg:
748a9306 1038 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
a0d0e21e 1039 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
748a9306 1040 SV* nullsv=sv_2mortal(newSViv(0));
a0d0e21e 1041 if (off1==lt_amg) {
748a9306 1042 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1043 lt_amg,AMGf_noright);
1044 logic = SvTRUE(lessp);
1045 } else {
748a9306 1046 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1047 ncmp_amg,AMGf_noright);
1048 logic = (SvNV(lessp) < 0);
1049 }
1050 if (logic) {
1051 if (off==subtr_amg) {
1052 right = left;
748a9306 1053 left = nullsv;
a0d0e21e
LW
1054 lr = 1;
1055 }
1056 } else {
1057 return left;
1058 }
1059 }
1060 break;
1061 case neg_amg:
1062 if (cv = cvp[off=subtr_amg]) {
1063 right = left;
1064 left = sv_2mortal(newSViv(0));
1065 lr = 1;
1066 }
1067 break;
1068 default:
1069 goto not_found;
1070 }
1071 if (!cv) goto not_found;
1072 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1073 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1074 && (cvp = ((amtp=(AMT*)mg->mg_ptr)->table))
1075 && (cv = cvp[off=method])) { /* Method for right
1076 * argument found */
1077 lr=1;
748a9306 1078 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
4633a7c4 1079 && (cvp=ocvp) && (lr = -1))
a0d0e21e
LW
1080 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1081 && !(flags & AMGf_unary)) {
1082 /* We look for substitution for
1083 * comparison operations and
1084 * concatendation */
1085 if (method==concat_amg || method==concat_ass_amg
1086 || method==repeat_amg || method==repeat_ass_amg) {
1087 return NULL; /* Delegate operation to string conversion */
1088 }
1089 off = -1;
1090 switch (method) {
1091 case lt_amg:
1092 case le_amg:
1093 case gt_amg:
1094 case ge_amg:
1095 case eq_amg:
1096 case ne_amg:
1097 postpr = 1; off=ncmp_amg; break;
1098 case slt_amg:
1099 case sle_amg:
1100 case sgt_amg:
1101 case sge_amg:
1102 case seq_amg:
1103 case sne_amg:
1104 postpr = 1; off=scmp_amg; break;
1105 }
1106 if (off != -1) cv = cvp[off];
1107 if (!cv) {
1108 goto not_found;
1109 }
1110 } else {
1111 not_found: /* No method found, either report or die */
1112 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1113 notfound = 1; lr = -1;
1114 } else if (cvp && (cv=cvp[nomethod_amg])) {
1115 notfound = 1; lr = 1;
1116 } else {
748a9306 1117 if (off==-1) off=method;
06b43eb7 1118 sprintf(buf, "Operation `%s': no method found,\n\tleft argument %s%.256s,\n\tright argument %s%.256s",
c07a80fd 1119 ((char**)AMG_names)[method + assignshift],
a0d0e21e
LW
1120 SvAMAGIC(left)?
1121 "in overloaded package ":
1122 "has no overloaded magic",
1123 SvAMAGIC(left)?
1124 HvNAME(SvSTASH(SvRV(left))):
1125 "",
1126 SvAMAGIC(right)?
1127 "in overloaded package ":
1128 "has no overloaded magic",
1129 SvAMAGIC(right)?
1130 HvNAME(SvSTASH(SvRV(right))):
1131 "");
1132 if (amtp && amtp->fallback >= AMGfallYES) {
06b43eb7 1133 DEBUG_o( deb(buf) );
a0d0e21e 1134 } else {
06b43eb7 1135 die(buf);
a0d0e21e
LW
1136 }
1137 return NULL;
1138 }
1139 }
1140 }
1141 if (!notfound) {
06b43eb7 1142 DEBUG_o( deb("Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %.256s%s\n",
a0d0e21e 1143 ((char**)AMG_names)[off],
748a9306
LW
1144 method+assignshift==off? "" :
1145 " (initially `",
1146 method+assignshift==off? "" :
1147 ((char**)AMG_names)[method+assignshift],
1148 method+assignshift==off? "" : "')",
1149 flags & AMGf_unary? "" :
1150 lr==1 ? " for right argument": " for left argument",
1151 flags & AMGf_unary? " for argument" : "",
a0d0e21e
LW
1152 HvNAME(stash),
1153 fl? ",\n\tassignment variant used": "") );
748a9306
LW
1154 /* Since we use shallow copy during assignment, we need
1155 * to dublicate the contents, probably calling user-supplied
1156 * version of copy operator
1157 */
c07a80fd 1158 if ((method + assignshift==off
748a9306
LW
1159 && (assign || method==inc_amg || method==dec_amg))
1160 || inc_dec_ass) RvDEEPCP(left);
a0d0e21e
LW
1161 }
1162 {
1163 dSP;
1164 BINOP myop;
1165 SV* res;
1166
1167 Zero(&myop, 1, BINOP);
1168 myop.op_last = (OP *) &myop;
1169 myop.op_next = Nullop;
1170 myop.op_flags = OPf_KNOW|OPf_STACKED;
1171
1172 ENTER;
1173 SAVESPTR(op);
1174 op = (OP *) &myop;
dc437b57
PP
1175 if (perldb && curstash != debstash)
1176 op->op_private |= OPpENTERSUB_DB;
a0d0e21e
LW
1177 PUTBACK;
1178 pp_pushmark();
1179
1180 EXTEND(sp, notfound + 5);
1181 PUSHs(lr>0? right: left);
1182 PUSHs(lr>0? left: right);
1183 PUSHs( assign ? &sv_undef : (lr>0? &sv_yes: &sv_no));
1184 if (notfound) {
c07a80fd 1185 PUSHs( sv_2mortal(newSVpv(((char**)AMG_names)[method + assignshift],0)) );
a0d0e21e
LW
1186 }
1187 PUSHs((SV*)cv);
1188 PUTBACK;
1189
1190 if (op = pp_entersub())
ad8898e0 1191 runops();
a0d0e21e
LW
1192 LEAVE;
1193 SPAGAIN;
1194
1195 res=POPs;
1196 PUTBACK;
1197
1198 if (notfound) {
1199 /* sv_2mortal(res); */
1200 return NULL;
1201 }
1202
1203 if (postpr) {
1204 int ans;
1205 switch (method) {
1206 case le_amg:
1207 case sle_amg:
1208 ans=SvIV(res)<=0; break;
1209 case lt_amg:
1210 case slt_amg:
1211 ans=SvIV(res)<0; break;
1212 case ge_amg:
1213 case sge_amg:
1214 ans=SvIV(res)>=0; break;
1215 case gt_amg:
1216 case sgt_amg:
1217 ans=SvIV(res)>0; break;
1218 case eq_amg:
1219 case seq_amg:
1220 ans=SvIV(res)==0; break;
1221 case ne_amg:
1222 case sne_amg:
1223 ans=SvIV(res)!=0; break;
1224 case inc_amg:
1225 case dec_amg:
bbce6d69 1226 SvSetSV(left,res); return left;
dc437b57
PP
1227 case not_amg:
1228ans=!SvOK(res); break;
a0d0e21e
LW
1229 }
1230 return ans? &sv_yes: &sv_no;
748a9306
LW
1231 } else if (method==copy_amg) {
1232 if (!SvROK(res)) {
1233 die("Copy method did not return a reference");
1234 }
1235 return SvREFCNT_inc(SvRV(res));
a0d0e21e
LW
1236 } else {
1237 return res;
1238 }
1239 }
1240}
1241#endif /* OVERLOAD */