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