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