This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: pre extending hash? - need speed
[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 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
279 if (strEQ(name,"import") || strEQ(name,"unimport"))
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 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 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 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 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 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 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
f967eb5f 690gv_fullname(sv, gv, prefix)
79072805
LW
691SV *sv;
692GV *gv;
f967eb5f 693char *prefix;
79072805
LW
694{
695 HV *hv = GvSTASH(gv);
f967eb5f 696 if (!hv) {
697 SvOK_off(sv);
79072805 698 return;
f967eb5f 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
f967eb5f 707gv_efullname(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;
f967eb5f 715 gv_fullname(sv, egv, prefix);
79072805
LW
716}
717
718IO *
719newIO()
720{
721 IO *io;
8990e307
LW
722 GV *iogv;
723
724 io = (IO*)NEWSV(0,0);
a0d0e21e 725 sv_upgrade((SV *)io,SVt_PVIO);
8990e307
LW
726 SvREFCNT(io) = 1;
727 SvOBJECT_on(io);
dc437b57 728 iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV);
8990e307 729 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
79072805
LW
730 return io;
731}
732
733void
463ee0b2
LW
734gv_check(stash)
735HV* stash;
79072805
LW
736{
737 register HE *entry;
738 register I32 i;
739 register GV *gv;
463ee0b2 740 HV *hv;
a0d0e21e 741 GV *filegv;
463ee0b2 742
8990e307
LW
743 if (!HvARRAY(stash))
744 return;
a0d0e21e 745 for (i = 0; i <= (I32) HvMAX(stash); i++) {
dc437b57 746 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
747 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
748 (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv))
a0d0e21e
LW
749 {
750 if (hv != defstash)
751 gv_check(hv); /* nested package */
752 }
dc437b57 753 else if (isALPHA(*HeKEY(entry))) {
754 gv = (GV*)HeVAL(entry);
a5f75d66 755 if (GvMULTI(gv))
463ee0b2
LW
756 continue;
757 curcop->cop_line = GvLINE(gv);
a0d0e21e
LW
758 filegv = GvFILEGV(gv);
759 curcop->cop_filegv = filegv;
a5f75d66 760 if (filegv && GvMULTI(filegv)) /* Filename began with slash */
8990e307 761 continue;
dc437b57 762 warn("Name \"%s::%s\" used only once: possible typo",
a0d0e21e 763 HvNAME(stash), GvNAME(gv));
463ee0b2 764 }
79072805
LW
765 }
766 }
767}
768
769GV *
a0d0e21e
LW
770newGVgen(pack)
771char *pack;
79072805 772{
a0d0e21e 773 (void)sprintf(tokenbuf,"%s::_GEN_%ld",pack,(long)gensym++);
85e6fe83 774 return gv_fetchpv(tokenbuf,TRUE, SVt_PVGV);
79072805
LW
775}
776
777/* hopefully this is only called on local symbol table entries */
778
779GP*
780gp_ref(gp)
781GP* gp;
782{
783 gp->gp_refcnt++;
784 return gp;
785
786}
787
788void
789gp_free(gv)
790GV* gv;
791{
79072805 792 GP* gp;
377b8fbc 793 CV* cv;
79072805
LW
794
795 if (!gv || !(gp = GvGP(gv)))
796 return;
797 if (gp->gp_refcnt == 0) {
798 warn("Attempt to free unreferenced glob pointers");
799 return;
800 }
748a9306
LW
801 if (--gp->gp_refcnt > 0) {
802 if (gp->gp_egv == gv)
803 gp->gp_egv = 0;
79072805 804 return;
748a9306 805 }
79072805 806
8990e307
LW
807 SvREFCNT_dec(gp->gp_sv);
808 SvREFCNT_dec(gp->gp_av);
809 SvREFCNT_dec(gp->gp_hv);
377b8fbc 810 SvREFCNT_dec(gp->gp_io);
8990e307
LW
811 if ((cv = gp->gp_cv) && !GvCVGEN(gv))
812 SvREFCNT_dec(cv);
748a9306
LW
813 SvREFCNT_dec(gp->gp_form);
814
79072805
LW
815 Safefree(gp);
816 GvGP(gv) = 0;
817}
818
819#if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
820#define MICROPORT
821#endif
822
823#ifdef MICROPORT /* Microport 2.4 hack */
824AV *GvAVn(gv)
825register GV *gv;
826{
827 if (GvGP(gv)->gp_av)
828 return GvGP(gv)->gp_av;
829 else
830 return GvGP(gv_AVadd(gv))->gp_av;
831}
832
833HV *GvHVn(gv)
834register GV *gv;
835{
836 if (GvGP(gv)->gp_hv)
837 return GvGP(gv)->gp_hv;
838 else
839 return GvGP(gv_HVadd(gv))->gp_hv;
840}
841#endif /* Microport 2.4 hack */
a0d0e21e
LW
842
843#ifdef OVERLOAD
844/* Updates and caches the CV's */
845
846bool
847Gv_AMupdate(stash)
848HV* stash;
849{
850 GV** gvp;
851 HV* hv;
852 GV* gv;
853 CV* cv;
854 MAGIC* mg=mg_find((SV*)stash,'c');
748a9306 855 AMT *amtp=mg ? (AMT*)mg->mg_ptr: NULL;
a0d0e21e
LW
856
857 if (mg && (amtp=((AMT*)(mg->mg_ptr)))->was_ok_am == amagic_generation &&
858 amtp->was_ok_sub == sub_generation)
859 return HV_AMAGIC(stash)? TRUE: FALSE;
860 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE);
748a9306
LW
861 if (amtp && amtp->table) {
862 int i;
863 for (i=1;i<NofAMmeth*2;i++) {
864 if (amtp->table[i]) {
865 SvREFCNT_dec(amtp->table[i]);
866 }
867 }
868 }
a0d0e21e
LW
869 sv_unmagic((SV*)stash, 'c');
870
871 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
872
873 if (gvp && ((gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv)))) {
874 int filled=0;
875 int i;
876 char *cp;
877 AMT amt;
878 SV* sv;
879 SV** svp;
dc437b57 880 GV** gvp;
a0d0e21e
LW
881
882/* if (*(svp)==(SV*)amagic_generation && *(svp+1)==(SV*)sub_generation) {
883 DEBUG_o( deb("Overload magic in package %s up-to-date\n",HvNAME(stash))
884);
885 return HV_AMAGIC(stash)? TRUE: FALSE;
886 }*/
887
888 amt.was_ok_am=amagic_generation;
889 amt.was_ok_sub=sub_generation;
890 amt.fallback=AMGfallNO;
891
892 /* Work with "fallback" key, which we assume to be first in AMG_names */
893
894 if ((cp=((char**)(*AMG_names))[0]) &&
895 (svp=(SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
896 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
897 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
898 }
899
900 for (i=1;i<NofAMmeth*2;i++) {
901 cv=0;
902
903 if ( (cp=((char**)(*AMG_names))[i]) ) {
904 svp=(SV**)hv_fetch(hv,cp,strlen(cp),FALSE);
dc437b57 905 if (svp && ((sv = *svp) != &sv_undef)) {
a0d0e21e
LW
906 switch (SvTYPE(sv)) {
907 default:
908 if (!SvROK(sv)) {
909 if (!SvOK(sv)) break;
748a9306 910 gv = gv_fetchmethod(stash, SvPV(sv, na));
a0d0e21e
LW
911 if (gv) cv = GvCV(gv);
912 break;
913 }
914 cv = (CV*)SvRV(sv);
915 if (SvTYPE(cv) == SVt_PVCV)
916 break;
917 /* FALL THROUGH */
918 case SVt_PVHV:
919 case SVt_PVAV:
920 die("Not a subroutine reference in %%OVERLOAD");
921 return FALSE;
922 case SVt_PVCV:
923 cv = (CV*)sv;
924 break;
925 case SVt_PVGV:
926 if (!(cv = GvCV((GV*)sv)))
927 cv = sv_2cv(sv, &stash, &gv, TRUE);
928 break;
929 }
930 if (cv) filled=1;
931 else {
06b43eb7 932 die("Method for operation %s not found in package %.256s during blessing\n",
a0d0e21e
LW
933 cp,HvNAME(stash));
934 return FALSE;
935 }
936 }
937 }
748a9306 938 amt.table[i]=(CV*)SvREFCNT_inc(cv);
a0d0e21e
LW
939 }
940 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(amt));
941 if (filled) {
942/* HV_badAMAGIC_off(stash);*/
943 HV_AMAGIC_on(stash);
944 return TRUE;
945 }
946 }
947/*HV_badAMAGIC_off(stash);*/
948 HV_AMAGIC_off(stash);
949 return FALSE;
950}
951
952/* During call to this subroutine stack can be reallocated. It is
953 * advised to call SPAGAIN macro in your code after call */
954
955SV*
956amagic_call(left,right,method,flags)
957SV* left;
958SV* right;
959int method;
960int flags;
961{
962 MAGIC *mg;
963 CV *cv;
964 CV **cvp=NULL, **ocvp=NULL;
965 AMT *amtp, *oamtp;
966 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
748a9306 967 int postpr=0, inc_dec_ass=0, assignshift=assign?1:0;
a0d0e21e
LW
968 HV* stash;
969 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
970 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
971 && (ocvp = cvp = ((oamtp=amtp=(AMT*)mg->mg_ptr)->table))
748a9306
LW
972 && ((cv = cvp[off=method+assignshift])
973 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
974 * usual method */
975 (fl = 1, cv = cvp[off=method])))) {
a0d0e21e
LW
976 lr = -1; /* Call method for left argument */
977 } else {
978 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
979 int logic;
980
981 /* look for substituted methods */
982 switch (method) {
983 case inc_amg:
748a9306 984 if (((cv = cvp[off=add_ass_amg]) && (inc_dec_ass=1))
a0d0e21e
LW
985 || ((cv = cvp[off=add_amg]) && (postpr=1))) {
986 right = &sv_yes; lr = -1; assign = 1;
987 }
988 break;
989 case dec_amg:
748a9306 990 if (((cv = cvp[off=subtr_ass_amg]) && (inc_dec_ass=1))
a0d0e21e
LW
991 || ((cv = cvp[off=subtr_amg]) && (postpr=1))) {
992 right = &sv_yes; lr = -1; assign = 1;
993 }
994 break;
995 case bool__amg:
996 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
997 break;
998 case numer_amg:
999 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
1000 break;
1001 case string_amg:
1002 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
1003 break;
dc437b57 1004 case not_amg:
1005 (void)((cv = cvp[off=bool__amg])
1006 || (cv = cvp[off=numer_amg])
1007 || (cv = cvp[off=string_amg]));
1008 postpr = 1;
1009 break;
748a9306
LW
1010 case copy_amg:
1011 {
1012 SV* ref=SvRV(left);
1013 if (!SvROK(ref) && SvTYPE(ref) <= SVt_PVMG) { /* Just to be
1014 * extra
1015 * causious,
1016 * maybe in some
1017 * additional
1018 * cases sv_setsv
1019 * is safe too */
1020 SV* newref = newSVsv(ref);
1021 SvOBJECT_on(newref);
1022 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(ref));
1023 return newref;
1024 }
1025 }
1026 break;
a0d0e21e 1027 case abs_amg:
748a9306 1028 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
a0d0e21e 1029 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
748a9306 1030 SV* nullsv=sv_2mortal(newSViv(0));
a0d0e21e 1031 if (off1==lt_amg) {
748a9306 1032 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1033 lt_amg,AMGf_noright);
1034 logic = SvTRUE(lessp);
1035 } else {
748a9306 1036 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1037 ncmp_amg,AMGf_noright);
1038 logic = (SvNV(lessp) < 0);
1039 }
1040 if (logic) {
1041 if (off==subtr_amg) {
1042 right = left;
748a9306 1043 left = nullsv;
a0d0e21e
LW
1044 lr = 1;
1045 }
1046 } else {
1047 return left;
1048 }
1049 }
1050 break;
1051 case neg_amg:
1052 if (cv = cvp[off=subtr_amg]) {
1053 right = left;
1054 left = sv_2mortal(newSViv(0));
1055 lr = 1;
1056 }
1057 break;
1058 default:
1059 goto not_found;
1060 }
1061 if (!cv) goto not_found;
1062 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1063 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1064 && (cvp = ((amtp=(AMT*)mg->mg_ptr)->table))
1065 && (cv = cvp[off=method])) { /* Method for right
1066 * argument found */
1067 lr=1;
748a9306 1068 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
4633a7c4 1069 && (cvp=ocvp) && (lr = -1))
a0d0e21e
LW
1070 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1071 && !(flags & AMGf_unary)) {
1072 /* We look for substitution for
1073 * comparison operations and
1074 * concatendation */
1075 if (method==concat_amg || method==concat_ass_amg
1076 || method==repeat_amg || method==repeat_ass_amg) {
1077 return NULL; /* Delegate operation to string conversion */
1078 }
1079 off = -1;
1080 switch (method) {
1081 case lt_amg:
1082 case le_amg:
1083 case gt_amg:
1084 case ge_amg:
1085 case eq_amg:
1086 case ne_amg:
1087 postpr = 1; off=ncmp_amg; break;
1088 case slt_amg:
1089 case sle_amg:
1090 case sgt_amg:
1091 case sge_amg:
1092 case seq_amg:
1093 case sne_amg:
1094 postpr = 1; off=scmp_amg; break;
1095 }
1096 if (off != -1) cv = cvp[off];
1097 if (!cv) {
1098 goto not_found;
1099 }
1100 } else {
1101 not_found: /* No method found, either report or die */
1102 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1103 notfound = 1; lr = -1;
1104 } else if (cvp && (cv=cvp[nomethod_amg])) {
1105 notfound = 1; lr = 1;
1106 } else {
748a9306 1107 if (off==-1) off=method;
06b43eb7 1108 sprintf(buf, "Operation `%s': no method found,\n\tleft argument %s%.256s,\n\tright argument %s%.256s",
c07a80fd 1109 ((char**)AMG_names)[method + assignshift],
a0d0e21e
LW
1110 SvAMAGIC(left)?
1111 "in overloaded package ":
1112 "has no overloaded magic",
1113 SvAMAGIC(left)?
1114 HvNAME(SvSTASH(SvRV(left))):
1115 "",
1116 SvAMAGIC(right)?
1117 "in overloaded package ":
1118 "has no overloaded magic",
1119 SvAMAGIC(right)?
1120 HvNAME(SvSTASH(SvRV(right))):
1121 "");
1122 if (amtp && amtp->fallback >= AMGfallYES) {
06b43eb7 1123 DEBUG_o( deb(buf) );
a0d0e21e 1124 } else {
06b43eb7 1125 die(buf);
a0d0e21e
LW
1126 }
1127 return NULL;
1128 }
1129 }
1130 }
1131 if (!notfound) {
06b43eb7 1132 DEBUG_o( deb("Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %.256s%s\n",
a0d0e21e 1133 ((char**)AMG_names)[off],
748a9306
LW
1134 method+assignshift==off? "" :
1135 " (initially `",
1136 method+assignshift==off? "" :
1137 ((char**)AMG_names)[method+assignshift],
1138 method+assignshift==off? "" : "')",
1139 flags & AMGf_unary? "" :
1140 lr==1 ? " for right argument": " for left argument",
1141 flags & AMGf_unary? " for argument" : "",
a0d0e21e
LW
1142 HvNAME(stash),
1143 fl? ",\n\tassignment variant used": "") );
748a9306
LW
1144 /* Since we use shallow copy during assignment, we need
1145 * to dublicate the contents, probably calling user-supplied
1146 * version of copy operator
1147 */
c07a80fd 1148 if ((method + assignshift==off
748a9306
LW
1149 && (assign || method==inc_amg || method==dec_amg))
1150 || inc_dec_ass) RvDEEPCP(left);
a0d0e21e
LW
1151 }
1152 {
1153 dSP;
1154 BINOP myop;
1155 SV* res;
1156
1157 Zero(&myop, 1, BINOP);
1158 myop.op_last = (OP *) &myop;
1159 myop.op_next = Nullop;
1160 myop.op_flags = OPf_KNOW|OPf_STACKED;
1161
1162 ENTER;
1163 SAVESPTR(op);
1164 op = (OP *) &myop;
dc437b57 1165 if (perldb && curstash != debstash)
1166 op->op_private |= OPpENTERSUB_DB;
a0d0e21e
LW
1167 PUTBACK;
1168 pp_pushmark();
1169
1170 EXTEND(sp, notfound + 5);
1171 PUSHs(lr>0? right: left);
1172 PUSHs(lr>0? left: right);
1173 PUSHs( assign ? &sv_undef : (lr>0? &sv_yes: &sv_no));
1174 if (notfound) {
c07a80fd 1175 PUSHs( sv_2mortal(newSVpv(((char**)AMG_names)[method + assignshift],0)) );
a0d0e21e
LW
1176 }
1177 PUSHs((SV*)cv);
1178 PUTBACK;
1179
1180 if (op = pp_entersub())
ad8898e0 1181 runops();
a0d0e21e
LW
1182 LEAVE;
1183 SPAGAIN;
1184
1185 res=POPs;
1186 PUTBACK;
1187
1188 if (notfound) {
1189 /* sv_2mortal(res); */
1190 return NULL;
1191 }
1192
1193 if (postpr) {
1194 int ans;
1195 switch (method) {
1196 case le_amg:
1197 case sle_amg:
1198 ans=SvIV(res)<=0; break;
1199 case lt_amg:
1200 case slt_amg:
1201 ans=SvIV(res)<0; break;
1202 case ge_amg:
1203 case sge_amg:
1204 ans=SvIV(res)>=0; break;
1205 case gt_amg:
1206 case sgt_amg:
1207 ans=SvIV(res)>0; break;
1208 case eq_amg:
1209 case seq_amg:
1210 ans=SvIV(res)==0; break;
1211 case ne_amg:
1212 case sne_amg:
1213 ans=SvIV(res)!=0; break;
1214 case inc_amg:
1215 case dec_amg:
1216 SvSetSV(left,res); return res; break;
dc437b57 1217 case not_amg:
1218ans=!SvOK(res); break;
a0d0e21e
LW
1219 }
1220 return ans? &sv_yes: &sv_no;
748a9306
LW
1221 } else if (method==copy_amg) {
1222 if (!SvROK(res)) {
1223 die("Copy method did not return a reference");
1224 }
1225 return SvREFCNT_inc(SvRV(res));
a0d0e21e
LW
1226 } else {
1227 return res;
1228 }
1229 }
1230}
1231#endif /* OVERLOAD */