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