This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
../lib isn't valid after a chdir "..";
[perl5.git] / pp.c
CommitLineData
a0d0e21e 1/* pp.c
79072805 2 *
1129b882
NC
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
79072805 5 *
a0d0e21e
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
79072805 8 *
a0d0e21e
LW
9 */
10
11/*
4ac71550
TC
12 * 'It's a big house this, and very peculiar. Always a bit more
13 * to discover, and no knowing what you'll find round a corner.
14 * And Elves, sir!' --Samwise Gamgee
15 *
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
a0d0e21e 17 */
79072805 18
166f8a29
DM
19/* This file contains general pp ("push/pop") functions that execute the
20 * opcodes that make up a perl program. A typical pp function expects to
21 * find its arguments on the stack, and usually pushes its results onto
22 * the stack, hence the 'pp' terminology. Each OP structure contains
23 * a pointer to the relevant pp_foo() function.
24 */
25
79072805 26#include "EXTERN.h"
864dbfa3 27#define PERL_IN_PP_C
79072805 28#include "perl.h"
77bc9082 29#include "keywords.h"
79072805 30
a4af207c
JH
31#include "reentr.h"
32
dfe9444c
AD
33/* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
35 --AD 2/20/1998
36*/
37#ifdef NEED_GETPID_PROTO
38extern Pid_t getpid (void);
8ac85365
NIS
39#endif
40
0630166f
SP
41/*
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
44 */
45#if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
47#endif
48
13017935
SM
49/* variations on pp_null */
50
93a17b20
LW
51PP(pp_stub)
52{
97aff369 53 dVAR;
39644a26 54 dSP;
54310121 55 if (GIMME_V == G_SCALAR)
3280af22 56 XPUSHs(&PL_sv_undef);
93a17b20
LW
57 RETURN;
58}
59
79072805
LW
60/* Pushy stuff. */
61
93a17b20
LW
62PP(pp_padav)
63{
97aff369 64 dVAR; dSP; dTARGET;
13017935 65 I32 gimme;
533c011a 66 if (PL_op->op_private & OPpLVAL_INTRO)
a5911867
RGS
67 if (!(PL_op->op_private & OPpPAD_STATE))
68 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
85e6fe83 69 EXTEND(SP, 1);
533c011a 70 if (PL_op->op_flags & OPf_REF) {
85e6fe83 71 PUSHs(TARG);
93a17b20 72 RETURN;
78f9721b
SM
73 } else if (LVRET) {
74 if (GIMME == G_SCALAR)
75 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
76 PUSHs(TARG);
77 RETURN;
85e6fe83 78 }
13017935
SM
79 gimme = GIMME_V;
80 if (gimme == G_ARRAY) {
502c6561 81 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
85e6fe83 82 EXTEND(SP, maxarg);
93965878
NIS
83 if (SvMAGICAL(TARG)) {
84 U32 i;
eb160463 85 for (i=0; i < (U32)maxarg; i++) {
502c6561 86 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
3280af22 87 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
93965878
NIS
88 }
89 }
90 else {
502c6561 91 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
93965878 92 }
85e6fe83
LW
93 SP += maxarg;
94 }
13017935 95 else if (gimme == G_SCALAR) {
1b6737cc 96 SV* const sv = sv_newmortal();
502c6561 97 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
85e6fe83
LW
98 sv_setiv(sv, maxarg);
99 PUSHs(sv);
100 }
101 RETURN;
93a17b20
LW
102}
103
104PP(pp_padhv)
105{
97aff369 106 dVAR; dSP; dTARGET;
54310121 107 I32 gimme;
108
93a17b20 109 XPUSHs(TARG);
533c011a 110 if (PL_op->op_private & OPpLVAL_INTRO)
a5911867
RGS
111 if (!(PL_op->op_private & OPpPAD_STATE))
112 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
533c011a 113 if (PL_op->op_flags & OPf_REF)
93a17b20 114 RETURN;
78f9721b
SM
115 else if (LVRET) {
116 if (GIMME == G_SCALAR)
117 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
118 RETURN;
119 }
54310121 120 gimme = GIMME_V;
121 if (gimme == G_ARRAY) {
cea2e8a9 122 RETURNOP(do_kv());
85e6fe83 123 }
54310121 124 else if (gimme == G_SCALAR) {
85fbaab2 125 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
85e6fe83 126 SETs(sv);
85e6fe83 127 }
54310121 128 RETURN;
93a17b20
LW
129}
130
79072805
LW
131/* Translations. */
132
133PP(pp_rv2gv)
134{
97aff369 135 dVAR; dSP; dTOPss;
8ec5e241 136
ed6116ce 137 if (SvROK(sv)) {
a0d0e21e 138 wasref:
f5284f61
IZ
139 tryAMAGICunDEREF(to_gv);
140
ed6116ce 141 sv = SvRV(sv);
b1dadf13 142 if (SvTYPE(sv) == SVt_PVIO) {
159b6efe 143 GV * const gv = MUTABLE_GV(sv_newmortal());
b1dadf13 144 gv_init(gv, 0, "", 0, 0);
a45c7426 145 GvIOp(gv) = MUTABLE_IO(sv);
b37c2d43 146 SvREFCNT_inc_void_NN(sv);
ad64d0ec 147 sv = MUTABLE_SV(gv);
ef54e1a4 148 }
6e592b3a 149 else if (!isGV_with_GP(sv))
cea2e8a9 150 DIE(aTHX_ "Not a GLOB reference");
79072805
LW
151 }
152 else {
6e592b3a 153 if (!isGV_with_GP(sv)) {
a0d0e21e
LW
154 if (SvGMAGICAL(sv)) {
155 mg_get(sv);
156 if (SvROK(sv))
157 goto wasref;
158 }
afd1915d 159 if (!SvOK(sv) && sv != &PL_sv_undef) {
b13b2135 160 /* If this is a 'my' scalar and flag is set then vivify
853846ea 161 * NI-S 1999/05/07
b13b2135 162 */
ac53db4c 163 if (SvREADONLY(sv))
f1f66076 164 Perl_croak(aTHX_ "%s", PL_no_modify);
1d8d4d2a 165 if (PL_op->op_private & OPpDEREF) {
2c8ac474
GS
166 GV *gv;
167 if (cUNOP->op_targ) {
168 STRLEN len;
0bd48802
AL
169 SV * const namesv = PAD_SV(cUNOP->op_targ);
170 const char * const name = SvPV(namesv, len);
159b6efe 171 gv = MUTABLE_GV(newSV(0));
2c8ac474
GS
172 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
173 }
174 else {
0bd48802 175 const char * const name = CopSTASHPV(PL_curcop);
2c8ac474 176 gv = newGVgen(name);
1d8d4d2a 177 }
43230e26 178 prepare_SV_for_RV(sv);
ad64d0ec 179 SvRV_set(sv, MUTABLE_SV(gv));
853846ea 180 SvROK_on(sv);
1d8d4d2a 181 SvSETMAGIC(sv);
853846ea 182 goto wasref;
2c8ac474 183 }
533c011a
NIS
184 if (PL_op->op_flags & OPf_REF ||
185 PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 186 DIE(aTHX_ PL_no_usym, "a symbol");
599cee73 187 if (ckWARN(WARN_UNINITIALIZED))
29489e7c 188 report_uninit(sv);
a0d0e21e
LW
189 RETSETUNDEF;
190 }
35cd451c
GS
191 if ((PL_op->op_flags & OPf_SPECIAL) &&
192 !(PL_op->op_flags & OPf_MOD))
193 {
ad64d0ec 194 SV * const temp = MUTABLE_SV(gv_fetchsv(sv, 0, SVt_PVGV));
7a5fd60d
NC
195 if (!temp
196 && (!is_gv_magical_sv(sv,0)
ad64d0ec
NC
197 || !(sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD,
198 SVt_PVGV))))) {
35cd451c 199 RETSETUNDEF;
c9d5ac95 200 }
7a5fd60d 201 sv = temp;
35cd451c
GS
202 }
203 else {
204 if (PL_op->op_private & HINT_STRICT_REFS)
7a5fd60d 205 DIE(aTHX_ PL_no_symref_sv, sv, "a symbol");
e26df76a
NC
206 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
207 == OPpDONT_INIT_GV) {
208 /* We are the target of a coderef assignment. Return
209 the scalar unchanged, and let pp_sasssign deal with
210 things. */
211 RETURN;
212 }
ad64d0ec 213 sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD, SVt_PVGV));
35cd451c 214 }
93a17b20 215 }
79072805 216 }
533c011a 217 if (PL_op->op_private & OPpLVAL_INTRO)
159b6efe 218 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
79072805
LW
219 SETs(sv);
220 RETURN;
221}
222
dc3c76f8
NC
223/* Helper function for pp_rv2sv and pp_rv2av */
224GV *
fe9845cc
RB
225Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
226 const svtype type, SV ***spp)
dc3c76f8
NC
227{
228 dVAR;
229 GV *gv;
230
7918f24d
NC
231 PERL_ARGS_ASSERT_SOFTREF2XV;
232
dc3c76f8
NC
233 if (PL_op->op_private & HINT_STRICT_REFS) {
234 if (SvOK(sv))
235 Perl_die(aTHX_ PL_no_symref_sv, sv, what);
236 else
237 Perl_die(aTHX_ PL_no_usym, what);
238 }
239 if (!SvOK(sv)) {
240 if (PL_op->op_flags & OPf_REF)
241 Perl_die(aTHX_ PL_no_usym, what);
242 if (ckWARN(WARN_UNINITIALIZED))
243 report_uninit(sv);
244 if (type != SVt_PV && GIMME_V == G_ARRAY) {
245 (*spp)--;
246 return NULL;
247 }
248 **spp = &PL_sv_undef;
249 return NULL;
250 }
251 if ((PL_op->op_flags & OPf_SPECIAL) &&
252 !(PL_op->op_flags & OPf_MOD))
253 {
81e3fc25 254 gv = gv_fetchsv(sv, 0, type);
dc3c76f8
NC
255 if (!gv
256 && (!is_gv_magical_sv(sv,0)
81e3fc25 257 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
dc3c76f8
NC
258 {
259 **spp = &PL_sv_undef;
260 return NULL;
261 }
262 }
263 else {
81e3fc25 264 gv = gv_fetchsv(sv, GV_ADD, type);
dc3c76f8
NC
265 }
266 return gv;
267}
268
79072805
LW
269PP(pp_rv2sv)
270{
97aff369 271 dVAR; dSP; dTOPss;
c445ea15 272 GV *gv = NULL;
79072805 273
ed6116ce 274 if (SvROK(sv)) {
a0d0e21e 275 wasref:
f5284f61
IZ
276 tryAMAGICunDEREF(to_sv);
277
ed6116ce 278 sv = SvRV(sv);
79072805
LW
279 switch (SvTYPE(sv)) {
280 case SVt_PVAV:
281 case SVt_PVHV:
282 case SVt_PVCV:
cbae9b9f
YST
283 case SVt_PVFM:
284 case SVt_PVIO:
cea2e8a9 285 DIE(aTHX_ "Not a SCALAR reference");
42d0e0b7 286 default: NOOP;
79072805
LW
287 }
288 }
289 else {
159b6efe 290 gv = MUTABLE_GV(sv);
748a9306 291
6e592b3a 292 if (!isGV_with_GP(gv)) {
a0d0e21e
LW
293 if (SvGMAGICAL(sv)) {
294 mg_get(sv);
295 if (SvROK(sv))
296 goto wasref;
297 }
dc3c76f8
NC
298 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
299 if (!gv)
300 RETURN;
463ee0b2 301 }
29c711a3 302 sv = GvSVn(gv);
a0d0e21e 303 }
533c011a 304 if (PL_op->op_flags & OPf_MOD) {
82d03984
RGS
305 if (PL_op->op_private & OPpLVAL_INTRO) {
306 if (cUNOP->op_first->op_type == OP_NULL)
159b6efe 307 sv = save_scalar(MUTABLE_GV(TOPs));
82d03984
RGS
308 else if (gv)
309 sv = save_scalar(gv);
310 else
f1f66076 311 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
82d03984 312 }
533c011a
NIS
313 else if (PL_op->op_private & OPpDEREF)
314 vivify_ref(sv, PL_op->op_private & OPpDEREF);
79072805 315 }
a0d0e21e 316 SETs(sv);
79072805
LW
317 RETURN;
318}
319
320PP(pp_av2arylen)
321{
97aff369 322 dVAR; dSP;
502c6561
NC
323 AV * const av = MUTABLE_AV(TOPs);
324 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
a3874608 325 if (!*sv) {
b9f83d2f 326 *sv = newSV_type(SVt_PVMG);
ad64d0ec 327 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
79072805 328 }
a3874608 329 SETs(*sv);
79072805
LW
330 RETURN;
331}
332
a0d0e21e
LW
333PP(pp_pos)
334{
97aff369 335 dVAR; dSP; dTARGET; dPOPss;
8ec5e241 336
78f9721b 337 if (PL_op->op_flags & OPf_MOD || LVRET) {
5f05dabc 338 if (SvTYPE(TARG) < SVt_PVLV) {
339 sv_upgrade(TARG, SVt_PVLV);
c445ea15 340 sv_magic(TARG, NULL, PERL_MAGIC_pos, NULL, 0);
5f05dabc 341 }
342
343 LvTYPE(TARG) = '.';
6ff81951
GS
344 if (LvTARG(TARG) != sv) {
345 if (LvTARG(TARG))
346 SvREFCNT_dec(LvTARG(TARG));
b37c2d43 347 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
6ff81951 348 }
a0d0e21e
LW
349 PUSHs(TARG); /* no SvSETMAGIC */
350 RETURN;
351 }
352 else {
a0d0e21e 353 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
1b6737cc 354 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
565764a8 355 if (mg && mg->mg_len >= 0) {
a0ed51b3 356 I32 i = mg->mg_len;
7e2040f0 357 if (DO_UTF8(sv))
a0ed51b3 358 sv_pos_b2u(sv, &i);
fc15ae8f 359 PUSHi(i + CopARYBASE_get(PL_curcop));
a0d0e21e
LW
360 RETURN;
361 }
362 }
363 RETPUSHUNDEF;
364 }
365}
366
79072805
LW
367PP(pp_rv2cv)
368{
97aff369 369 dVAR; dSP;
79072805 370 GV *gv;
1eced8f8 371 HV *stash_unused;
c445ea15
AL
372 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
373 ? 0
374 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
375 ? GV_ADD|GV_NOEXPAND
376 : GV_ADD;
4633a7c4
LW
377 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
378 /* (But not in defined().) */
e26df76a 379
1eced8f8 380 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
07055b4c
CS
381 if (cv) {
382 if (CvCLONE(cv))
ad64d0ec 383 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
d32f2495
SC
384 if ((PL_op->op_private & OPpLVAL_INTRO)) {
385 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
386 cv = GvCV(gv);
387 if (!CvLVALUE(cv))
388 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
389 }
07055b4c 390 }
e26df76a 391 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
ea726b52 392 cv = MUTABLE_CV(gv);
e26df76a 393 }
07055b4c 394 else
ea726b52 395 cv = MUTABLE_CV(&PL_sv_undef);
ad64d0ec 396 SETs(MUTABLE_SV(cv));
79072805
LW
397 RETURN;
398}
399
c07a80fd 400PP(pp_prototype)
401{
97aff369 402 dVAR; dSP;
c07a80fd 403 CV *cv;
404 HV *stash;
405 GV *gv;
fabdb6c0 406 SV *ret = &PL_sv_undef;
c07a80fd 407
b6c543e3 408 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
e3f73d4e 409 const char * s = SvPVX_const(TOPs);
b6c543e3 410 if (strnEQ(s, "CORE::", 6)) {
5458a98a 411 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
b6c543e3
IZ
412 if (code < 0) { /* Overridable. */
413#define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
59b085e1 414 int i = 0, n = 0, seen_question = 0, defgv = 0;
b6c543e3
IZ
415 I32 oa;
416 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
417
bdf1bb36 418 if (code == -KEY_chop || code == -KEY_chomp
f23102e2 419 || code == -KEY_exec || code == -KEY_system)
77bc9082 420 goto set;
d116c547 421 if (code == -KEY_mkdir) {
84bafc02 422 ret = newSVpvs_flags("_;$", SVs_TEMP);
d116c547
RGS
423 goto set;
424 }
7c8178a1
RGS
425 if (code == -KEY_keys || code == -KEY_values || code == -KEY_each) {
426 ret = newSVpvs_flags("\\[@%]", SVs_TEMP);
427 goto set;
428 }
e3f73d4e
RGS
429 if (code == -KEY_readpipe) {
430 s = "CORE::backtick";
431 }
b6c543e3 432 while (i < MAXO) { /* The slow way. */
22c35a8c
GS
433 if (strEQ(s + 6, PL_op_name[i])
434 || strEQ(s + 6, PL_op_desc[i]))
435 {
b6c543e3 436 goto found;
22c35a8c 437 }
b6c543e3
IZ
438 i++;
439 }
440 goto nonesuch; /* Should not happen... */
441 found:
59b085e1 442 defgv = PL_opargs[i] & OA_DEFGV;
22c35a8c 443 oa = PL_opargs[i] >> OASHIFT;
b6c543e3 444 while (oa) {
59b085e1 445 if (oa & OA_OPTIONAL && !seen_question && !defgv) {
b6c543e3
IZ
446 seen_question = 1;
447 str[n++] = ';';
ef54e1a4 448 }
b13b2135 449 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
6e97e420
SC
450 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
451 /* But globs are already references (kinda) */
452 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
453 ) {
b6c543e3
IZ
454 str[n++] = '\\';
455 }
b6c543e3
IZ
456 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
457 oa = oa >> 4;
458 }
59b085e1
RGS
459 if (defgv && str[n - 1] == '$')
460 str[n - 1] = '_';
b6c543e3 461 str[n++] = '\0';
59cd0e26 462 ret = newSVpvn_flags(str, n - 1, SVs_TEMP);
ef54e1a4
JH
463 }
464 else if (code) /* Non-Overridable */
b6c543e3
IZ
465 goto set;
466 else { /* None such */
467 nonesuch:
d470f89e 468 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
b6c543e3
IZ
469 }
470 }
471 }
f2c0649b 472 cv = sv_2cv(TOPs, &stash, &gv, 0);
5f05dabc 473 if (cv && SvPOK(cv))
59cd0e26 474 ret = newSVpvn_flags(SvPVX_const(cv), SvCUR(cv), SVs_TEMP);
b6c543e3 475 set:
c07a80fd 476 SETs(ret);
477 RETURN;
478}
479
a0d0e21e
LW
480PP(pp_anoncode)
481{
97aff369 482 dVAR; dSP;
ea726b52 483 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
a5f75d66 484 if (CvCLONE(cv))
ad64d0ec 485 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
5f05dabc 486 EXTEND(SP,1);
ad64d0ec 487 PUSHs(MUTABLE_SV(cv));
a0d0e21e
LW
488 RETURN;
489}
490
491PP(pp_srefgen)
79072805 492{
97aff369 493 dVAR; dSP;
71be2cbc 494 *SP = refto(*SP);
79072805 495 RETURN;
8ec5e241 496}
a0d0e21e
LW
497
498PP(pp_refgen)
499{
97aff369 500 dVAR; dSP; dMARK;
a0d0e21e 501 if (GIMME != G_ARRAY) {
5f0b1d4e
GS
502 if (++MARK <= SP)
503 *MARK = *SP;
504 else
3280af22 505 *MARK = &PL_sv_undef;
5f0b1d4e
GS
506 *MARK = refto(*MARK);
507 SP = MARK;
508 RETURN;
a0d0e21e 509 }
bbce6d69 510 EXTEND_MORTAL(SP - MARK);
71be2cbc 511 while (++MARK <= SP)
512 *MARK = refto(*MARK);
a0d0e21e 513 RETURN;
79072805
LW
514}
515
76e3520e 516STATIC SV*
cea2e8a9 517S_refto(pTHX_ SV *sv)
71be2cbc 518{
97aff369 519 dVAR;
71be2cbc 520 SV* rv;
521
7918f24d
NC
522 PERL_ARGS_ASSERT_REFTO;
523
71be2cbc 524 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
525 if (LvTARGLEN(sv))
68dc0745 526 vivify_defelem(sv);
527 if (!(sv = LvTARG(sv)))
3280af22 528 sv = &PL_sv_undef;
0dd88869 529 else
b37c2d43 530 SvREFCNT_inc_void_NN(sv);
71be2cbc 531 }
d8b46c1b 532 else if (SvTYPE(sv) == SVt_PVAV) {
502c6561
NC
533 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
534 av_reify(MUTABLE_AV(sv));
d8b46c1b 535 SvTEMP_off(sv);
b37c2d43 536 SvREFCNT_inc_void_NN(sv);
d8b46c1b 537 }
f2933f5f
DM
538 else if (SvPADTMP(sv) && !IS_PADGV(sv))
539 sv = newSVsv(sv);
71be2cbc 540 else {
541 SvTEMP_off(sv);
b37c2d43 542 SvREFCNT_inc_void_NN(sv);
71be2cbc 543 }
544 rv = sv_newmortal();
4df7f6af 545 sv_upgrade(rv, SVt_IV);
b162af07 546 SvRV_set(rv, sv);
71be2cbc 547 SvROK_on(rv);
548 return rv;
549}
550
79072805
LW
551PP(pp_ref)
552{
97aff369 553 dVAR; dSP; dTARGET;
e1ec3a88 554 const char *pv;
1b6737cc 555 SV * const sv = POPs;
f12c7020 556
5b295bef
RD
557 if (sv)
558 SvGETMAGIC(sv);
f12c7020 559
a0d0e21e 560 if (!sv || !SvROK(sv))
4633a7c4 561 RETPUSHNO;
79072805 562
1b6737cc 563 pv = sv_reftype(SvRV(sv),TRUE);
463ee0b2 564 PUSHp(pv, strlen(pv));
79072805
LW
565 RETURN;
566}
567
568PP(pp_bless)
569{
97aff369 570 dVAR; dSP;
463ee0b2 571 HV *stash;
79072805 572
463ee0b2 573 if (MAXARG == 1)
11faa288 574 stash = CopSTASH(PL_curcop);
7b8d334a 575 else {
1b6737cc 576 SV * const ssv = POPs;
7b8d334a 577 STRLEN len;
e1ec3a88 578 const char *ptr;
81689caa 579
016a42f3 580 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
81689caa 581 Perl_croak(aTHX_ "Attempt to bless into a reference");
5c144d81 582 ptr = SvPV_const(ssv,len);
a2a5de95
NC
583 if (len == 0)
584 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
585 "Explicit blessing to '' (assuming package main)");
da51bb9b 586 stash = gv_stashpvn(ptr, len, GV_ADD);
7b8d334a 587 }
a0d0e21e 588
5d3fdfeb 589 (void)sv_bless(TOPs, stash);
79072805
LW
590 RETURN;
591}
592
fb73857a 593PP(pp_gelem)
594{
97aff369 595 dVAR; dSP;
b13b2135 596
1b6737cc
AL
597 SV *sv = POPs;
598 const char * const elem = SvPV_nolen_const(sv);
159b6efe 599 GV * const gv = MUTABLE_GV(POPs);
c445ea15 600 SV * tmpRef = NULL;
1b6737cc 601
c445ea15 602 sv = NULL;
c4ba80c3
NC
603 if (elem) {
604 /* elem will always be NUL terminated. */
1b6737cc 605 const char * const second_letter = elem + 1;
c4ba80c3
NC
606 switch (*elem) {
607 case 'A':
1b6737cc 608 if (strEQ(second_letter, "RRAY"))
ad64d0ec 609 tmpRef = MUTABLE_SV(GvAV(gv));
c4ba80c3
NC
610 break;
611 case 'C':
1b6737cc 612 if (strEQ(second_letter, "ODE"))
ad64d0ec 613 tmpRef = MUTABLE_SV(GvCVu(gv));
c4ba80c3
NC
614 break;
615 case 'F':
1b6737cc 616 if (strEQ(second_letter, "ILEHANDLE")) {
c4ba80c3
NC
617 /* finally deprecated in 5.8.0 */
618 deprecate("*glob{FILEHANDLE}");
ad64d0ec 619 tmpRef = MUTABLE_SV(GvIOp(gv));
c4ba80c3
NC
620 }
621 else
1b6737cc 622 if (strEQ(second_letter, "ORMAT"))
ad64d0ec 623 tmpRef = MUTABLE_SV(GvFORM(gv));
c4ba80c3
NC
624 break;
625 case 'G':
1b6737cc 626 if (strEQ(second_letter, "LOB"))
ad64d0ec 627 tmpRef = MUTABLE_SV(gv);
c4ba80c3
NC
628 break;
629 case 'H':
1b6737cc 630 if (strEQ(second_letter, "ASH"))
ad64d0ec 631 tmpRef = MUTABLE_SV(GvHV(gv));
c4ba80c3
NC
632 break;
633 case 'I':
1b6737cc 634 if (*second_letter == 'O' && !elem[2])
ad64d0ec 635 tmpRef = MUTABLE_SV(GvIOp(gv));
c4ba80c3
NC
636 break;
637 case 'N':
1b6737cc 638 if (strEQ(second_letter, "AME"))
a663657d 639 sv = newSVhek(GvNAME_HEK(gv));
c4ba80c3
NC
640 break;
641 case 'P':
1b6737cc 642 if (strEQ(second_letter, "ACKAGE")) {
7fa3a4ab
NC
643 const HV * const stash = GvSTASH(gv);
644 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
396482e1 645 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
c4ba80c3
NC
646 }
647 break;
648 case 'S':
1b6737cc 649 if (strEQ(second_letter, "CALAR"))
f9d52e31 650 tmpRef = GvSVn(gv);
c4ba80c3 651 break;
39b99f21 652 }
fb73857a 653 }
76e3520e
GS
654 if (tmpRef)
655 sv = newRV(tmpRef);
fb73857a 656 if (sv)
657 sv_2mortal(sv);
658 else
3280af22 659 sv = &PL_sv_undef;
fb73857a 660 XPUSHs(sv);
661 RETURN;
662}
663
a0d0e21e 664/* Pattern matching */
79072805 665
a0d0e21e 666PP(pp_study)
79072805 667{
97aff369 668 dVAR; dSP; dPOPss;
a0d0e21e
LW
669 register unsigned char *s;
670 register I32 pos;
671 register I32 ch;
672 register I32 *sfirst;
673 register I32 *snext;
a0d0e21e
LW
674 STRLEN len;
675
3280af22 676 if (sv == PL_lastscream) {
1e422769 677 if (SvSCREAM(sv))
678 RETPUSHYES;
679 }
a4f4e906
NC
680 s = (unsigned char*)(SvPV(sv, len));
681 pos = len;
c9b9f909 682 if (pos <= 0 || !SvPOK(sv) || SvUTF8(sv)) {
a4f4e906
NC
683 /* No point in studying a zero length string, and not safe to study
684 anything that doesn't appear to be a simple scalar (and hence might
685 change between now and when the regexp engine runs without our set
bd473224 686 magic ever running) such as a reference to an object with overloaded
a4f4e906
NC
687 stringification. */
688 RETPUSHNO;
689 }
690
691 if (PL_lastscream) {
692 SvSCREAM_off(PL_lastscream);
693 SvREFCNT_dec(PL_lastscream);
c07a80fd 694 }
b37c2d43 695 PL_lastscream = SvREFCNT_inc_simple(sv);
1e422769 696
697 s = (unsigned char*)(SvPV(sv, len));
698 pos = len;
699 if (pos <= 0)
700 RETPUSHNO;
3280af22
NIS
701 if (pos > PL_maxscream) {
702 if (PL_maxscream < 0) {
703 PL_maxscream = pos + 80;
a02a5408
JC
704 Newx(PL_screamfirst, 256, I32);
705 Newx(PL_screamnext, PL_maxscream, I32);
79072805
LW
706 }
707 else {
3280af22
NIS
708 PL_maxscream = pos + pos / 4;
709 Renew(PL_screamnext, PL_maxscream, I32);
79072805 710 }
79072805 711 }
a0d0e21e 712
3280af22
NIS
713 sfirst = PL_screamfirst;
714 snext = PL_screamnext;
a0d0e21e
LW
715
716 if (!sfirst || !snext)
cea2e8a9 717 DIE(aTHX_ "do_study: out of memory");
a0d0e21e
LW
718
719 for (ch = 256; ch; --ch)
720 *sfirst++ = -1;
721 sfirst -= 256;
722
723 while (--pos >= 0) {
1b6737cc 724 register const I32 ch = s[pos];
a0d0e21e
LW
725 if (sfirst[ch] >= 0)
726 snext[pos] = sfirst[ch] - pos;
727 else
728 snext[pos] = -pos;
729 sfirst[ch] = pos;
79072805
LW
730 }
731
c07a80fd 732 SvSCREAM_on(sv);
14befaf4 733 /* piggyback on m//g magic */
c445ea15 734 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
1e422769 735 RETPUSHYES;
79072805
LW
736}
737
a0d0e21e 738PP(pp_trans)
79072805 739{
97aff369 740 dVAR; dSP; dTARG;
a0d0e21e
LW
741 SV *sv;
742
533c011a 743 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e 744 sv = POPs;
59f00321
RGS
745 else if (PL_op->op_private & OPpTARGET_MY)
746 sv = GETTARGET;
79072805 747 else {
54b9620d 748 sv = DEFSV;
a0d0e21e 749 EXTEND(SP,1);
79072805 750 }
adbc6bb1 751 TARG = sv_newmortal();
4757a243 752 PUSHi(do_trans(sv));
a0d0e21e 753 RETURN;
79072805
LW
754}
755
a0d0e21e 756/* Lvalue operators. */
79072805 757
a0d0e21e
LW
758PP(pp_schop)
759{
97aff369 760 dVAR; dSP; dTARGET;
a0d0e21e
LW
761 do_chop(TARG, TOPs);
762 SETTARG;
763 RETURN;
79072805
LW
764}
765
a0d0e21e 766PP(pp_chop)
79072805 767{
97aff369 768 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
2ec6af5f
RG
769 while (MARK < SP)
770 do_chop(TARG, *++MARK);
771 SP = ORIGMARK;
b59aed67 772 XPUSHTARG;
a0d0e21e 773 RETURN;
79072805
LW
774}
775
a0d0e21e 776PP(pp_schomp)
79072805 777{
97aff369 778 dVAR; dSP; dTARGET;
a0d0e21e
LW
779 SETi(do_chomp(TOPs));
780 RETURN;
79072805
LW
781}
782
a0d0e21e 783PP(pp_chomp)
79072805 784{
97aff369 785 dVAR; dSP; dMARK; dTARGET;
a0d0e21e 786 register I32 count = 0;
8ec5e241 787
a0d0e21e
LW
788 while (SP > MARK)
789 count += do_chomp(POPs);
b59aed67 790 XPUSHi(count);
a0d0e21e 791 RETURN;
79072805
LW
792}
793
a0d0e21e
LW
794PP(pp_undef)
795{
97aff369 796 dVAR; dSP;
a0d0e21e
LW
797 SV *sv;
798
533c011a 799 if (!PL_op->op_private) {
774d564b 800 EXTEND(SP, 1);
a0d0e21e 801 RETPUSHUNDEF;
774d564b 802 }
79072805 803
a0d0e21e
LW
804 sv = POPs;
805 if (!sv)
806 RETPUSHUNDEF;
85e6fe83 807
765f542d 808 SV_CHECK_THINKFIRST_COW_DROP(sv);
85e6fe83 809
a0d0e21e
LW
810 switch (SvTYPE(sv)) {
811 case SVt_NULL:
812 break;
813 case SVt_PVAV:
502c6561 814 av_undef(MUTABLE_AV(sv));
a0d0e21e
LW
815 break;
816 case SVt_PVHV:
85fbaab2 817 hv_undef(MUTABLE_HV(sv));
a0d0e21e
LW
818 break;
819 case SVt_PVCV:
a2a5de95
NC
820 if (cv_const_sv((const CV *)sv))
821 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
822 CvANON((const CV *)sv) ? "(anonymous)"
823 : GvENAME(CvGV((const CV *)sv)));
5f66b61c 824 /* FALLTHROUGH */
9607fc9c 825 case SVt_PVFM:
6fc92669
GS
826 {
827 /* let user-undef'd sub keep its identity */
ea726b52
NC
828 GV* const gv = CvGV((const CV *)sv);
829 cv_undef(MUTABLE_CV(sv));
830 CvGV((const CV *)sv) = gv;
6fc92669 831 }
a0d0e21e 832 break;
8e07c86e 833 case SVt_PVGV:
6e592b3a 834 if (SvFAKE(sv)) {
3280af22 835 SvSetMagicSV(sv, &PL_sv_undef);
6e592b3a
BM
836 break;
837 }
838 else if (isGV_with_GP(sv)) {
20408e3c 839 GP *gp;
dd69841b
BB
840 HV *stash;
841
842 /* undef *Foo:: */
159b6efe 843 if((stash = GvHV((const GV *)sv)) && HvNAME_get(stash))
dd69841b
BB
844 mro_isa_changed_in(stash);
845 /* undef *Pkg::meth_name ... */
159b6efe
NC
846 else if(GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
847 && HvNAME_get(stash))
dd69841b
BB
848 mro_method_changed_in(stash);
849
159b6efe 850 gp_free(MUTABLE_GV(sv));
a02a5408 851 Newxz(gp, 1, GP);
20408e3c 852 GvGP(sv) = gp_ref(gp);
561b68a9 853 GvSV(sv) = newSV(0);
57843af0 854 GvLINE(sv) = CopLINE(PL_curcop);
159b6efe 855 GvEGV(sv) = MUTABLE_GV(sv);
20408e3c 856 GvMULTI_on(sv);
6e592b3a 857 break;
20408e3c 858 }
6e592b3a 859 /* FALL THROUGH */
a0d0e21e 860 default:
b15aece3 861 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
8bd4d4c5 862 SvPV_free(sv);
c445ea15 863 SvPV_set(sv, NULL);
4633a7c4 864 SvLEN_set(sv, 0);
a0d0e21e 865 }
0c34ef67 866 SvOK_off(sv);
4633a7c4 867 SvSETMAGIC(sv);
79072805 868 }
a0d0e21e
LW
869
870 RETPUSHUNDEF;
79072805
LW
871}
872
a0d0e21e 873PP(pp_predec)
79072805 874{
97aff369 875 dVAR; dSP;
6e592b3a 876 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
f1f66076 877 DIE(aTHX_ "%s", PL_no_modify);
3510b4a1
NC
878 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
879 && SvIVX(TOPs) != IV_MIN)
55497cff 880 {
45977657 881 SvIV_set(TOPs, SvIVX(TOPs) - 1);
55497cff 882 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
883 }
884 else
885 sv_dec(TOPs);
a0d0e21e
LW
886 SvSETMAGIC(TOPs);
887 return NORMAL;
888}
79072805 889
a0d0e21e
LW
890PP(pp_postinc)
891{
97aff369 892 dVAR; dSP; dTARGET;
6e592b3a 893 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
f1f66076 894 DIE(aTHX_ "%s", PL_no_modify);
a0d0e21e 895 sv_setsv(TARG, TOPs);
3510b4a1
NC
896 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
897 && SvIVX(TOPs) != IV_MAX)
55497cff 898 {
45977657 899 SvIV_set(TOPs, SvIVX(TOPs) + 1);
55497cff 900 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
901 }
902 else
903 sv_inc(TOPs);
a0d0e21e 904 SvSETMAGIC(TOPs);
1e54a23f 905 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
a0d0e21e
LW
906 if (!SvOK(TARG))
907 sv_setiv(TARG, 0);
908 SETs(TARG);
909 return NORMAL;
910}
79072805 911
a0d0e21e
LW
912PP(pp_postdec)
913{
97aff369 914 dVAR; dSP; dTARGET;
6e592b3a 915 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
f1f66076 916 DIE(aTHX_ "%s", PL_no_modify);
a0d0e21e 917 sv_setsv(TARG, TOPs);
3510b4a1
NC
918 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
919 && SvIVX(TOPs) != IV_MIN)
55497cff 920 {
45977657 921 SvIV_set(TOPs, SvIVX(TOPs) - 1);
55497cff 922 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
923 }
924 else
925 sv_dec(TOPs);
a0d0e21e
LW
926 SvSETMAGIC(TOPs);
927 SETs(TARG);
928 return NORMAL;
929}
79072805 930
a0d0e21e
LW
931/* Ordinary operators. */
932
933PP(pp_pow)
934{
800401ee 935 dVAR; dSP; dATARGET; SV *svl, *svr;
58d76dfd 936#ifdef PERL_PRESERVE_IVUV
52a96ae6
HS
937 bool is_int = 0;
938#endif
939 tryAMAGICbin(pow,opASSIGN);
800401ee
JH
940 svl = sv_2num(TOPm1s);
941 svr = sv_2num(TOPs);
52a96ae6
HS
942#ifdef PERL_PRESERVE_IVUV
943 /* For integer to integer power, we do the calculation by hand wherever
944 we're sure it is safe; otherwise we call pow() and try to convert to
945 integer afterwards. */
58d76dfd 946 {
800401ee
JH
947 SvIV_please(svr);
948 if (SvIOK(svr)) {
949 SvIV_please(svl);
950 if (SvIOK(svl)) {
900658e3
PF
951 UV power;
952 bool baseuok;
953 UV baseuv;
954
800401ee
JH
955 if (SvUOK(svr)) {
956 power = SvUVX(svr);
900658e3 957 } else {
800401ee 958 const IV iv = SvIVX(svr);
900658e3
PF
959 if (iv >= 0) {
960 power = iv;
961 } else {
962 goto float_it; /* Can't do negative powers this way. */
963 }
964 }
965
800401ee 966 baseuok = SvUOK(svl);
900658e3 967 if (baseuok) {
800401ee 968 baseuv = SvUVX(svl);
900658e3 969 } else {
800401ee 970 const IV iv = SvIVX(svl);
900658e3
PF
971 if (iv >= 0) {
972 baseuv = iv;
973 baseuok = TRUE; /* effectively it's a UV now */
974 } else {
975 baseuv = -iv; /* abs, baseuok == false records sign */
976 }
977 }
52a96ae6
HS
978 /* now we have integer ** positive integer. */
979 is_int = 1;
980
981 /* foo & (foo - 1) is zero only for a power of 2. */
58d76dfd 982 if (!(baseuv & (baseuv - 1))) {
52a96ae6 983 /* We are raising power-of-2 to a positive integer.
58d76dfd
JH
984 The logic here will work for any base (even non-integer
985 bases) but it can be less accurate than
986 pow (base,power) or exp (power * log (base)) when the
987 intermediate values start to spill out of the mantissa.
988 With powers of 2 we know this can't happen.
989 And powers of 2 are the favourite thing for perl
990 programmers to notice ** not doing what they mean. */
991 NV result = 1.0;
992 NV base = baseuok ? baseuv : -(NV)baseuv;
900658e3
PF
993
994 if (power & 1) {
995 result *= base;
996 }
997 while (power >>= 1) {
998 base *= base;
999 if (power & 1) {
1000 result *= base;
1001 }
1002 }
58d76dfd
JH
1003 SP--;
1004 SETn( result );
800401ee 1005 SvIV_please(svr);
58d76dfd 1006 RETURN;
52a96ae6
HS
1007 } else {
1008 register unsigned int highbit = 8 * sizeof(UV);
900658e3
PF
1009 register unsigned int diff = 8 * sizeof(UV);
1010 while (diff >>= 1) {
1011 highbit -= diff;
1012 if (baseuv >> highbit) {
1013 highbit += diff;
1014 }
52a96ae6
HS
1015 }
1016 /* we now have baseuv < 2 ** highbit */
1017 if (power * highbit <= 8 * sizeof(UV)) {
1018 /* result will definitely fit in UV, so use UV math
1019 on same algorithm as above */
1020 register UV result = 1;
1021 register UV base = baseuv;
900658e3
PF
1022 const bool odd_power = (bool)(power & 1);
1023 if (odd_power) {
1024 result *= base;
1025 }
1026 while (power >>= 1) {
1027 base *= base;
1028 if (power & 1) {
52a96ae6 1029 result *= base;
52a96ae6
HS
1030 }
1031 }
1032 SP--;
0615a994 1033 if (baseuok || !odd_power)
52a96ae6
HS
1034 /* answer is positive */
1035 SETu( result );
1036 else if (result <= (UV)IV_MAX)
1037 /* answer negative, fits in IV */
1038 SETi( -(IV)result );
1039 else if (result == (UV)IV_MIN)
1040 /* 2's complement assumption: special case IV_MIN */
1041 SETi( IV_MIN );
1042 else
1043 /* answer negative, doesn't fit */
1044 SETn( -(NV)result );
1045 RETURN;
1046 }
1047 }
1048 }
1049 }
58d76dfd 1050 }
52a96ae6 1051 float_it:
58d76dfd 1052#endif
a0d0e21e 1053 {
4efa5a16
RD
1054 NV right = SvNV(svr);
1055 NV left = SvNV(svl);
1056 (void)POPs;
3aaeb624
JA
1057
1058#if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1059 /*
1060 We are building perl with long double support and are on an AIX OS
1061 afflicted with a powl() function that wrongly returns NaNQ for any
1062 negative base. This was reported to IBM as PMR #23047-379 on
1063 03/06/2006. The problem exists in at least the following versions
1064 of AIX and the libm fileset, and no doubt others as well:
1065
1066 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1067 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1068 AIX 5.2.0 bos.adt.libm 5.2.0.85
1069
1070 So, until IBM fixes powl(), we provide the following workaround to
1071 handle the problem ourselves. Our logic is as follows: for
1072 negative bases (left), we use fmod(right, 2) to check if the
1073 exponent is an odd or even integer:
1074
1075 - if odd, powl(left, right) == -powl(-left, right)
1076 - if even, powl(left, right) == powl(-left, right)
1077
1078 If the exponent is not an integer, the result is rightly NaNQ, so
1079 we just return that (as NV_NAN).
1080 */
1081
1082 if (left < 0.0) {
1083 NV mod2 = Perl_fmod( right, 2.0 );
1084 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1085 SETn( -Perl_pow( -left, right) );
1086 } else if (mod2 == 0.0) { /* even integer */
1087 SETn( Perl_pow( -left, right) );
1088 } else { /* fractional power */
1089 SETn( NV_NAN );
1090 }
1091 } else {
1092 SETn( Perl_pow( left, right) );
1093 }
1094#else
52a96ae6 1095 SETn( Perl_pow( left, right) );
3aaeb624
JA
1096#endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1097
52a96ae6
HS
1098#ifdef PERL_PRESERVE_IVUV
1099 if (is_int)
800401ee 1100 SvIV_please(svr);
52a96ae6
HS
1101#endif
1102 RETURN;
93a17b20 1103 }
a0d0e21e
LW
1104}
1105
1106PP(pp_multiply)
1107{
800401ee
JH
1108 dVAR; dSP; dATARGET; SV *svl, *svr;
1109 tryAMAGICbin(mult,opASSIGN);
1110 svl = sv_2num(TOPm1s);
1111 svr = sv_2num(TOPs);
28e5dec8 1112#ifdef PERL_PRESERVE_IVUV
800401ee
JH
1113 SvIV_please(svr);
1114 if (SvIOK(svr)) {
28e5dec8
JH
1115 /* Unless the left argument is integer in range we are going to have to
1116 use NV maths. Hence only attempt to coerce the right argument if
1117 we know the left is integer. */
1118 /* Left operand is defined, so is it IV? */
800401ee
JH
1119 SvIV_please(svl);
1120 if (SvIOK(svl)) {
1121 bool auvok = SvUOK(svl);
1122 bool buvok = SvUOK(svr);
28e5dec8
JH
1123 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1124 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1125 UV alow;
1126 UV ahigh;
1127 UV blow;
1128 UV bhigh;
1129
1130 if (auvok) {
800401ee 1131 alow = SvUVX(svl);
28e5dec8 1132 } else {
800401ee 1133 const IV aiv = SvIVX(svl);
28e5dec8
JH
1134 if (aiv >= 0) {
1135 alow = aiv;
1136 auvok = TRUE; /* effectively it's a UV now */
1137 } else {
1138 alow = -aiv; /* abs, auvok == false records sign */
1139 }
1140 }
1141 if (buvok) {
800401ee 1142 blow = SvUVX(svr);
28e5dec8 1143 } else {
800401ee 1144 const IV biv = SvIVX(svr);
28e5dec8
JH
1145 if (biv >= 0) {
1146 blow = biv;
1147 buvok = TRUE; /* effectively it's a UV now */
1148 } else {
1149 blow = -biv; /* abs, buvok == false records sign */
1150 }
1151 }
1152
1153 /* If this does sign extension on unsigned it's time for plan B */
1154 ahigh = alow >> (4 * sizeof (UV));
1155 alow &= botmask;
1156 bhigh = blow >> (4 * sizeof (UV));
1157 blow &= botmask;
1158 if (ahigh && bhigh) {
6f207bd3 1159 NOOP;
28e5dec8
JH
1160 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1161 which is overflow. Drop to NVs below. */
1162 } else if (!ahigh && !bhigh) {
1163 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1164 so the unsigned multiply cannot overflow. */
c445ea15 1165 const UV product = alow * blow;
28e5dec8
JH
1166 if (auvok == buvok) {
1167 /* -ve * -ve or +ve * +ve gives a +ve result. */
1168 SP--;
1169 SETu( product );
1170 RETURN;
1171 } else if (product <= (UV)IV_MIN) {
1172 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1173 /* -ve result, which could overflow an IV */
1174 SP--;
25716404 1175 SETi( -(IV)product );
28e5dec8
JH
1176 RETURN;
1177 } /* else drop to NVs below. */
1178 } else {
1179 /* One operand is large, 1 small */
1180 UV product_middle;
1181 if (bhigh) {
1182 /* swap the operands */
1183 ahigh = bhigh;
1184 bhigh = blow; /* bhigh now the temp var for the swap */
1185 blow = alow;
1186 alow = bhigh;
1187 }
1188 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1189 multiplies can't overflow. shift can, add can, -ve can. */
1190 product_middle = ahigh * blow;
1191 if (!(product_middle & topmask)) {
1192 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1193 UV product_low;
1194 product_middle <<= (4 * sizeof (UV));
1195 product_low = alow * blow;
1196
1197 /* as for pp_add, UV + something mustn't get smaller.
1198 IIRC ANSI mandates this wrapping *behaviour* for
1199 unsigned whatever the actual representation*/
1200 product_low += product_middle;
1201 if (product_low >= product_middle) {
1202 /* didn't overflow */
1203 if (auvok == buvok) {
1204 /* -ve * -ve or +ve * +ve gives a +ve result. */
1205 SP--;
1206 SETu( product_low );
1207 RETURN;
1208 } else if (product_low <= (UV)IV_MIN) {
1209 /* 2s complement assumption again */
1210 /* -ve result, which could overflow an IV */
1211 SP--;
25716404 1212 SETi( -(IV)product_low );
28e5dec8
JH
1213 RETURN;
1214 } /* else drop to NVs below. */
1215 }
1216 } /* product_middle too large */
1217 } /* ahigh && bhigh */
800401ee
JH
1218 } /* SvIOK(svl) */
1219 } /* SvIOK(svr) */
28e5dec8 1220#endif
a0d0e21e 1221 {
4efa5a16
RD
1222 NV right = SvNV(svr);
1223 NV left = SvNV(svl);
1224 (void)POPs;
a0d0e21e
LW
1225 SETn( left * right );
1226 RETURN;
79072805 1227 }
a0d0e21e
LW
1228}
1229
1230PP(pp_divide)
1231{
800401ee
JH
1232 dVAR; dSP; dATARGET; SV *svl, *svr;
1233 tryAMAGICbin(div,opASSIGN);
1234 svl = sv_2num(TOPm1s);
1235 svr = sv_2num(TOPs);
5479d192 1236 /* Only try to do UV divide first
68795e93 1237 if ((SLOPPYDIVIDE is true) or
5479d192
NC
1238 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1239 to preserve))
1240 The assumption is that it is better to use floating point divide
1241 whenever possible, only doing integer divide first if we can't be sure.
1242 If NV_PRESERVES_UV is true then we know at compile time that no UV
1243 can be too large to preserve, so don't need to compile the code to
1244 test the size of UVs. */
1245
a0d0e21e 1246#ifdef SLOPPYDIVIDE
5479d192
NC
1247# define PERL_TRY_UV_DIVIDE
1248 /* ensure that 20./5. == 4. */
a0d0e21e 1249#else
5479d192
NC
1250# ifdef PERL_PRESERVE_IVUV
1251# ifndef NV_PRESERVES_UV
1252# define PERL_TRY_UV_DIVIDE
1253# endif
1254# endif
a0d0e21e 1255#endif
5479d192
NC
1256
1257#ifdef PERL_TRY_UV_DIVIDE
800401ee
JH
1258 SvIV_please(svr);
1259 if (SvIOK(svr)) {
1260 SvIV_please(svl);
1261 if (SvIOK(svl)) {
1262 bool left_non_neg = SvUOK(svl);
1263 bool right_non_neg = SvUOK(svr);
5479d192
NC
1264 UV left;
1265 UV right;
1266
1267 if (right_non_neg) {
800401ee 1268 right = SvUVX(svr);
5479d192
NC
1269 }
1270 else {
800401ee 1271 const IV biv = SvIVX(svr);
5479d192
NC
1272 if (biv >= 0) {
1273 right = biv;
1274 right_non_neg = TRUE; /* effectively it's a UV now */
1275 }
1276 else {
1277 right = -biv;
1278 }
1279 }
1280 /* historically undef()/0 gives a "Use of uninitialized value"
1281 warning before dieing, hence this test goes here.
1282 If it were immediately before the second SvIV_please, then
1283 DIE() would be invoked before left was even inspected, so
1284 no inpsection would give no warning. */
1285 if (right == 0)
1286 DIE(aTHX_ "Illegal division by zero");
1287
1288 if (left_non_neg) {
800401ee 1289 left = SvUVX(svl);
5479d192
NC
1290 }
1291 else {
800401ee 1292 const IV aiv = SvIVX(svl);
5479d192
NC
1293 if (aiv >= 0) {
1294 left = aiv;
1295 left_non_neg = TRUE; /* effectively it's a UV now */
1296 }
1297 else {
1298 left = -aiv;
1299 }
1300 }
1301
1302 if (left >= right
1303#ifdef SLOPPYDIVIDE
1304 /* For sloppy divide we always attempt integer division. */
1305#else
1306 /* Otherwise we only attempt it if either or both operands
1307 would not be preserved by an NV. If both fit in NVs
0c2ee62a
NC
1308 we fall through to the NV divide code below. However,
1309 as left >= right to ensure integer result here, we know that
1310 we can skip the test on the right operand - right big
1311 enough not to be preserved can't get here unless left is
1312 also too big. */
1313
1314 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
5479d192
NC
1315#endif
1316 ) {
1317 /* Integer division can't overflow, but it can be imprecise. */
1b6737cc 1318 const UV result = left / right;
5479d192
NC
1319 if (result * right == left) {
1320 SP--; /* result is valid */
1321 if (left_non_neg == right_non_neg) {
1322 /* signs identical, result is positive. */
1323 SETu( result );
1324 RETURN;
1325 }
1326 /* 2s complement assumption */
1327 if (result <= (UV)IV_MIN)
91f3b821 1328 SETi( -(IV)result );
5479d192
NC
1329 else {
1330 /* It's exact but too negative for IV. */
1331 SETn( -(NV)result );
1332 }
1333 RETURN;
1334 } /* tried integer divide but it was not an integer result */
32fdb065 1335 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
5479d192
NC
1336 } /* left wasn't SvIOK */
1337 } /* right wasn't SvIOK */
1338#endif /* PERL_TRY_UV_DIVIDE */
1339 {
4efa5a16
RD
1340 NV right = SvNV(svr);
1341 NV left = SvNV(svl);
1342 (void)POPs;(void)POPs;
ebc6a117
PD
1343#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1344 if (! Perl_isnan(right) && right == 0.0)
1345#else
5479d192 1346 if (right == 0.0)
ebc6a117 1347#endif
5479d192
NC
1348 DIE(aTHX_ "Illegal division by zero");
1349 PUSHn( left / right );
1350 RETURN;
79072805 1351 }
a0d0e21e
LW
1352}
1353
1354PP(pp_modulo)
1355{
97aff369 1356 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
a0d0e21e 1357 {
9c5ffd7c
JH
1358 UV left = 0;
1359 UV right = 0;
dc656993
JH
1360 bool left_neg = FALSE;
1361 bool right_neg = FALSE;
e2c88acc
NC
1362 bool use_double = FALSE;
1363 bool dright_valid = FALSE;
9c5ffd7c
JH
1364 NV dright = 0.0;
1365 NV dleft = 0.0;
800401ee
JH
1366 SV * svl;
1367 SV * const svr = sv_2num(TOPs);
1368 SvIV_please(svr);
1369 if (SvIOK(svr)) {
1370 right_neg = !SvUOK(svr);
e2c88acc 1371 if (!right_neg) {
800401ee 1372 right = SvUVX(svr);
e2c88acc 1373 } else {
800401ee 1374 const IV biv = SvIVX(svr);
e2c88acc
NC
1375 if (biv >= 0) {
1376 right = biv;
1377 right_neg = FALSE; /* effectively it's a UV now */
1378 } else {
1379 right = -biv;
1380 }
1381 }
1382 }
1383 else {
4efa5a16 1384 dright = SvNV(svr);
787eafbd
IZ
1385 right_neg = dright < 0;
1386 if (right_neg)
1387 dright = -dright;
e2c88acc
NC
1388 if (dright < UV_MAX_P1) {
1389 right = U_V(dright);
1390 dright_valid = TRUE; /* In case we need to use double below. */
1391 } else {
1392 use_double = TRUE;
1393 }
787eafbd 1394 }
4efa5a16 1395 sp--;
a0d0e21e 1396
e2c88acc
NC
1397 /* At this point use_double is only true if right is out of range for
1398 a UV. In range NV has been rounded down to nearest UV and
1399 use_double false. */
800401ee
JH
1400 svl = sv_2num(TOPs);
1401 SvIV_please(svl);
1402 if (!use_double && SvIOK(svl)) {
1403 if (SvIOK(svl)) {
1404 left_neg = !SvUOK(svl);
e2c88acc 1405 if (!left_neg) {
800401ee 1406 left = SvUVX(svl);
e2c88acc 1407 } else {
800401ee 1408 const IV aiv = SvIVX(svl);
e2c88acc
NC
1409 if (aiv >= 0) {
1410 left = aiv;
1411 left_neg = FALSE; /* effectively it's a UV now */
1412 } else {
1413 left = -aiv;
1414 }
1415 }
1416 }
1417 }
787eafbd 1418 else {
4efa5a16 1419 dleft = SvNV(svl);
787eafbd
IZ
1420 left_neg = dleft < 0;
1421 if (left_neg)
1422 dleft = -dleft;
68dc0745 1423
e2c88acc
NC
1424 /* This should be exactly the 5.6 behaviour - if left and right are
1425 both in range for UV then use U_V() rather than floor. */
1426 if (!use_double) {
1427 if (dleft < UV_MAX_P1) {
1428 /* right was in range, so is dleft, so use UVs not double.
1429 */
1430 left = U_V(dleft);
1431 }
1432 /* left is out of range for UV, right was in range, so promote
1433 right (back) to double. */
1434 else {
1435 /* The +0.5 is used in 5.6 even though it is not strictly
1436 consistent with the implicit +0 floor in the U_V()
1437 inside the #if 1. */
1438 dleft = Perl_floor(dleft + 0.5);
1439 use_double = TRUE;
1440 if (dright_valid)
1441 dright = Perl_floor(dright + 0.5);
1442 else
1443 dright = right;
1444 }
1445 }
1446 }
4efa5a16 1447 sp--;
787eafbd 1448 if (use_double) {
65202027 1449 NV dans;
787eafbd 1450
787eafbd 1451 if (!dright)
cea2e8a9 1452 DIE(aTHX_ "Illegal modulus zero");
787eafbd 1453
65202027 1454 dans = Perl_fmod(dleft, dright);
787eafbd
IZ
1455 if ((left_neg != right_neg) && dans)
1456 dans = dright - dans;
1457 if (right_neg)
1458 dans = -dans;
1459 sv_setnv(TARG, dans);
1460 }
1461 else {
1462 UV ans;
1463
787eafbd 1464 if (!right)
cea2e8a9 1465 DIE(aTHX_ "Illegal modulus zero");
787eafbd
IZ
1466
1467 ans = left % right;
1468 if ((left_neg != right_neg) && ans)
1469 ans = right - ans;
1470 if (right_neg) {
1471 /* XXX may warn: unary minus operator applied to unsigned type */
1472 /* could change -foo to be (~foo)+1 instead */
1473 if (ans <= ~((UV)IV_MAX)+1)
1474 sv_setiv(TARG, ~ans+1);
1475 else
65202027 1476 sv_setnv(TARG, -(NV)ans);
787eafbd
IZ
1477 }
1478 else
1479 sv_setuv(TARG, ans);
1480 }
1481 PUSHTARG;
1482 RETURN;
79072805 1483 }
a0d0e21e 1484}
79072805 1485
a0d0e21e
LW
1486PP(pp_repeat)
1487{
97aff369 1488 dVAR; dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
748a9306 1489 {
2b573ace
JH
1490 register IV count;
1491 dPOPss;
5b295bef 1492 SvGETMAGIC(sv);
2b573ace
JH
1493 if (SvIOKp(sv)) {
1494 if (SvUOK(sv)) {
1b6737cc 1495 const UV uv = SvUV(sv);
2b573ace
JH
1496 if (uv > IV_MAX)
1497 count = IV_MAX; /* The best we can do? */
1498 else
1499 count = uv;
1500 } else {
0bd48802 1501 const IV iv = SvIV(sv);
2b573ace
JH
1502 if (iv < 0)
1503 count = 0;
1504 else
1505 count = iv;
1506 }
1507 }
1508 else if (SvNOKp(sv)) {
1b6737cc 1509 const NV nv = SvNV(sv);
2b573ace
JH
1510 if (nv < 0.0)
1511 count = 0;
1512 else
1513 count = (IV)nv;
1514 }
1515 else
4ea561bc 1516 count = SvIV(sv);
533c011a 1517 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
a0d0e21e 1518 dMARK;
0bd48802
AL
1519 static const char oom_list_extend[] = "Out of memory during list extend";
1520 const I32 items = SP - MARK;
1521 const I32 max = items * count;
79072805 1522
2b573ace
JH
1523 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1524 /* Did the max computation overflow? */
27d5b266 1525 if (items > 0 && max > 0 && (max < items || max < count))
2b573ace 1526 Perl_croak(aTHX_ oom_list_extend);
a0d0e21e
LW
1527 MEXTEND(MARK, max);
1528 if (count > 1) {
1529 while (SP > MARK) {
976c8a39
JH
1530#if 0
1531 /* This code was intended to fix 20010809.028:
1532
1533 $x = 'abcd';
1534 for (($x =~ /./g) x 2) {
1535 print chop; # "abcdabcd" expected as output.
1536 }
1537
1538 * but that change (#11635) broke this code:
1539
1540 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1541
1542 * I can't think of a better fix that doesn't introduce
1543 * an efficiency hit by copying the SVs. The stack isn't
1544 * refcounted, and mortalisation obviously doesn't
1545 * Do The Right Thing when the stack has more than
1546 * one pointer to the same mortal value.
1547 * .robin.
1548 */
e30acc16
RH
1549 if (*SP) {
1550 *SP = sv_2mortal(newSVsv(*SP));
1551 SvREADONLY_on(*SP);
1552 }
976c8a39
JH
1553#else
1554 if (*SP)
1555 SvTEMP_off((*SP));
1556#endif
a0d0e21e 1557 SP--;
79072805 1558 }
a0d0e21e
LW
1559 MARK++;
1560 repeatcpy((char*)(MARK + items), (char*)MARK,
ad64d0ec 1561 items * sizeof(const SV *), count - 1);
a0d0e21e 1562 SP += max;
79072805 1563 }
a0d0e21e
LW
1564 else if (count <= 0)
1565 SP -= items;
79072805 1566 }
a0d0e21e 1567 else { /* Note: mark already snarfed by pp_list */
0bd48802 1568 SV * const tmpstr = POPs;
a0d0e21e 1569 STRLEN len;
9b877dbb 1570 bool isutf;
2b573ace
JH
1571 static const char oom_string_extend[] =
1572 "Out of memory during string extend";
a0d0e21e 1573
a0d0e21e
LW
1574 SvSetSV(TARG, tmpstr);
1575 SvPV_force(TARG, len);
9b877dbb 1576 isutf = DO_UTF8(TARG);
8ebc5c01 1577 if (count != 1) {
1578 if (count < 1)
1579 SvCUR_set(TARG, 0);
1580 else {
c445ea15 1581 const STRLEN max = (UV)count * len;
19a94d75 1582 if (len > MEM_SIZE_MAX / count)
2b573ace
JH
1583 Perl_croak(aTHX_ oom_string_extend);
1584 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
8569b9dc 1585 SvGROW(TARG, max + 1);
a0d0e21e 1586 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
b162af07 1587 SvCUR_set(TARG, SvCUR(TARG) * count);
7a4c00b4 1588 }
a0d0e21e 1589 *SvEND(TARG) = '\0';
a0d0e21e 1590 }
dfcb284a
GS
1591 if (isutf)
1592 (void)SvPOK_only_UTF8(TARG);
1593 else
1594 (void)SvPOK_only(TARG);
b80b6069
RH
1595
1596 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1597 /* The parser saw this as a list repeat, and there
1598 are probably several items on the stack. But we're
1599 in scalar context, and there's no pp_list to save us
1600 now. So drop the rest of the items -- robin@kitsite.com
1601 */
1602 dMARK;
1603 SP = MARK;
1604 }
a0d0e21e 1605 PUSHTARG;
79072805 1606 }
a0d0e21e 1607 RETURN;
748a9306 1608 }
a0d0e21e 1609}
79072805 1610
a0d0e21e
LW
1611PP(pp_subtract)
1612{
800401ee
JH
1613 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1614 tryAMAGICbin(subtr,opASSIGN);
1615 svl = sv_2num(TOPm1s);
1616 svr = sv_2num(TOPs);
1617 useleft = USE_LEFT(svl);
28e5dec8 1618#ifdef PERL_PRESERVE_IVUV
7dca457a
NC
1619 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1620 "bad things" happen if you rely on signed integers wrapping. */
800401ee
JH
1621 SvIV_please(svr);
1622 if (SvIOK(svr)) {
28e5dec8
JH
1623 /* Unless the left argument is integer in range we are going to have to
1624 use NV maths. Hence only attempt to coerce the right argument if
1625 we know the left is integer. */
9c5ffd7c
JH
1626 register UV auv = 0;
1627 bool auvok = FALSE;
7dca457a
NC
1628 bool a_valid = 0;
1629
28e5dec8 1630 if (!useleft) {
7dca457a
NC
1631 auv = 0;
1632 a_valid = auvok = 1;
1633 /* left operand is undef, treat as zero. */
28e5dec8
JH
1634 } else {
1635 /* Left operand is defined, so is it IV? */
800401ee
JH
1636 SvIV_please(svl);
1637 if (SvIOK(svl)) {
1638 if ((auvok = SvUOK(svl)))
1639 auv = SvUVX(svl);
7dca457a 1640 else {
800401ee 1641 register const IV aiv = SvIVX(svl);
7dca457a
NC
1642 if (aiv >= 0) {
1643 auv = aiv;
1644 auvok = 1; /* Now acting as a sign flag. */
1645 } else { /* 2s complement assumption for IV_MIN */
1646 auv = (UV)-aiv;
28e5dec8 1647 }
7dca457a
NC
1648 }
1649 a_valid = 1;
1650 }
1651 }
1652 if (a_valid) {
1653 bool result_good = 0;
1654 UV result;
1655 register UV buv;
800401ee 1656 bool buvok = SvUOK(svr);
9041c2e3 1657
7dca457a 1658 if (buvok)
800401ee 1659 buv = SvUVX(svr);
7dca457a 1660 else {
800401ee 1661 register const IV biv = SvIVX(svr);
7dca457a
NC
1662 if (biv >= 0) {
1663 buv = biv;
1664 buvok = 1;
1665 } else
1666 buv = (UV)-biv;
1667 }
1668 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
602f51c4 1669 else "IV" now, independent of how it came in.
7dca457a
NC
1670 if a, b represents positive, A, B negative, a maps to -A etc
1671 a - b => (a - b)
1672 A - b => -(a + b)
1673 a - B => (a + b)
1674 A - B => -(a - b)
1675 all UV maths. negate result if A negative.
1676 subtract if signs same, add if signs differ. */
1677
1678 if (auvok ^ buvok) {
1679 /* Signs differ. */
1680 result = auv + buv;
1681 if (result >= auv)
1682 result_good = 1;
1683 } else {
1684 /* Signs same */
1685 if (auv >= buv) {
1686 result = auv - buv;
1687 /* Must get smaller */
1688 if (result <= auv)
1689 result_good = 1;
1690 } else {
1691 result = buv - auv;
1692 if (result <= buv) {
1693 /* result really should be -(auv-buv). as its negation
1694 of true value, need to swap our result flag */
1695 auvok = !auvok;
1696 result_good = 1;
28e5dec8 1697 }
28e5dec8
JH
1698 }
1699 }
7dca457a
NC
1700 if (result_good) {
1701 SP--;
1702 if (auvok)
1703 SETu( result );
1704 else {
1705 /* Negate result */
1706 if (result <= (UV)IV_MIN)
1707 SETi( -(IV)result );
1708 else {
1709 /* result valid, but out of range for IV. */
1710 SETn( -(NV)result );
1711 }
1712 }
1713 RETURN;
1714 } /* Overflow, drop through to NVs. */
28e5dec8
JH
1715 }
1716 }
1717#endif
a0d0e21e 1718 {
4efa5a16
RD
1719 NV value = SvNV(svr);
1720 (void)POPs;
1721
28e5dec8
JH
1722 if (!useleft) {
1723 /* left operand is undef, treat as zero - value */
1724 SETn(-value);
1725 RETURN;
1726 }
4efa5a16 1727 SETn( SvNV(svl) - value );
28e5dec8 1728 RETURN;
79072805 1729 }
a0d0e21e 1730}
79072805 1731
a0d0e21e
LW
1732PP(pp_left_shift)
1733{
97aff369 1734 dVAR; dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
a0d0e21e 1735 {
1b6737cc 1736 const IV shift = POPi;
d0ba1bd2 1737 if (PL_op->op_private & HINT_INTEGER) {
c445ea15 1738 const IV i = TOPi;
972b05a9 1739 SETi(i << shift);
d0ba1bd2
JH
1740 }
1741 else {
c445ea15 1742 const UV u = TOPu;
972b05a9 1743 SETu(u << shift);
d0ba1bd2 1744 }
55497cff 1745 RETURN;
79072805 1746 }
a0d0e21e 1747}
79072805 1748
a0d0e21e
LW
1749PP(pp_right_shift)
1750{
97aff369 1751 dVAR; dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
a0d0e21e 1752 {
1b6737cc 1753 const IV shift = POPi;
d0ba1bd2 1754 if (PL_op->op_private & HINT_INTEGER) {
0bd48802 1755 const IV i = TOPi;
972b05a9 1756 SETi(i >> shift);
d0ba1bd2
JH
1757 }
1758 else {
0bd48802 1759 const UV u = TOPu;
972b05a9 1760 SETu(u >> shift);
d0ba1bd2 1761 }
a0d0e21e 1762 RETURN;
93a17b20 1763 }
79072805
LW
1764}
1765
a0d0e21e 1766PP(pp_lt)
79072805 1767{
97aff369 1768 dVAR; dSP; tryAMAGICbinSET(lt,0);
28e5dec8
JH
1769#ifdef PERL_PRESERVE_IVUV
1770 SvIV_please(TOPs);
1771 if (SvIOK(TOPs)) {
1772 SvIV_please(TOPm1s);
1773 if (SvIOK(TOPm1s)) {
1774 bool auvok = SvUOK(TOPm1s);
1775 bool buvok = SvUOK(TOPs);
a227d84d 1776
28e5dec8 1777 if (!auvok && !buvok) { /* ## IV < IV ## */
1b6737cc
AL
1778 const IV aiv = SvIVX(TOPm1s);
1779 const IV biv = SvIVX(TOPs);
28e5dec8
JH
1780
1781 SP--;
1782 SETs(boolSV(aiv < biv));
1783 RETURN;
1784 }
1785 if (auvok && buvok) { /* ## UV < UV ## */
1b6737cc
AL
1786 const UV auv = SvUVX(TOPm1s);
1787 const UV buv = SvUVX(TOPs);
28e5dec8
JH
1788
1789 SP--;
1790 SETs(boolSV(auv < buv));
1791 RETURN;
1792 }
1793 if (auvok) { /* ## UV < IV ## */
1794 UV auv;
1b6737cc 1795 const IV biv = SvIVX(TOPs);
28e5dec8
JH
1796 SP--;
1797 if (biv < 0) {
1798 /* As (a) is a UV, it's >=0, so it cannot be < */
1799 SETs(&PL_sv_no);
1800 RETURN;
1801 }
1802 auv = SvUVX(TOPs);
28e5dec8
JH
1803 SETs(boolSV(auv < (UV)biv));
1804 RETURN;
1805 }
1806 { /* ## IV < UV ## */
1b6737cc 1807 const IV aiv = SvIVX(TOPm1s);
28e5dec8
JH
1808 UV buv;
1809
28e5dec8
JH
1810 if (aiv < 0) {
1811 /* As (b) is a UV, it's >=0, so it must be < */
1812 SP--;
1813 SETs(&PL_sv_yes);
1814 RETURN;
1815 }
1816 buv = SvUVX(TOPs);
1817 SP--;
28e5dec8
JH
1818 SETs(boolSV((UV)aiv < buv));
1819 RETURN;
1820 }
1821 }
1822 }
1823#endif
30de85b6 1824#ifndef NV_PRESERVES_UV
50fb3111
NC
1825#ifdef PERL_PRESERVE_IVUV
1826 else
1827#endif
0bdaccee
NC
1828 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1829 SP--;
1830 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1831 RETURN;
1832 }
30de85b6 1833#endif
a0d0e21e 1834 {
cab190d4
JD
1835#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1836 dPOPTOPnnrl;
1837 if (Perl_isnan(left) || Perl_isnan(right))
1838 RETSETNO;
1839 SETs(boolSV(left < right));
1840#else
a0d0e21e 1841 dPOPnv;
54310121 1842 SETs(boolSV(TOPn < value));
cab190d4 1843#endif
a0d0e21e 1844 RETURN;
79072805 1845 }
a0d0e21e 1846}
79072805 1847
a0d0e21e
LW
1848PP(pp_gt)
1849{
97aff369 1850 dVAR; dSP; tryAMAGICbinSET(gt,0);
28e5dec8
JH
1851#ifdef PERL_PRESERVE_IVUV
1852 SvIV_please(TOPs);
1853 if (SvIOK(TOPs)) {
1854 SvIV_please(TOPm1s);
1855 if (SvIOK(TOPm1s)) {
1856 bool auvok = SvUOK(TOPm1s);
1857 bool buvok = SvUOK(TOPs);
a227d84d 1858
28e5dec8 1859 if (!auvok && !buvok) { /* ## IV > IV ## */
1b6737cc
AL
1860 const IV aiv = SvIVX(TOPm1s);
1861 const IV biv = SvIVX(TOPs);
1862
28e5dec8
JH
1863 SP--;
1864 SETs(boolSV(aiv > biv));
1865 RETURN;
1866 }
1867 if (auvok && buvok) { /* ## UV > UV ## */
1b6737cc
AL
1868 const UV auv = SvUVX(TOPm1s);
1869 const UV buv = SvUVX(TOPs);
28e5dec8
JH
1870
1871 SP--;
1872 SETs(boolSV(auv > buv));
1873 RETURN;
1874 }
1875 if (auvok) { /* ## UV > IV ## */
1876 UV auv;
1b6737cc
AL
1877 const IV biv = SvIVX(TOPs);
1878
28e5dec8
JH
1879 SP--;
1880 if (biv < 0) {
1881 /* As (a) is a UV, it's >=0, so it must be > */
1882 SETs(&PL_sv_yes);
1883 RETURN;
1884 }
1885 auv = SvUVX(TOPs);
28e5dec8
JH
1886 SETs(boolSV(auv > (UV)biv));
1887 RETURN;
1888 }
1889 { /* ## IV > UV ## */
1b6737cc 1890 const IV aiv = SvIVX(TOPm1s);
28e5dec8
JH
1891 UV buv;
1892
28e5dec8
JH
1893 if (aiv < 0) {
1894 /* As (b) is a UV, it's >=0, so it cannot be > */
1895 SP--;
1896 SETs(&PL_sv_no);
1897 RETURN;
1898 }
1899 buv = SvUVX(TOPs);
1900 SP--;
28e5dec8
JH
1901 SETs(boolSV((UV)aiv > buv));
1902 RETURN;
1903 }
1904 }
1905 }
1906#endif
30de85b6 1907#ifndef NV_PRESERVES_UV
50fb3111
NC
1908#ifdef PERL_PRESERVE_IVUV
1909 else
1910#endif
0bdaccee 1911 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
30de85b6
NC
1912 SP--;
1913 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1914 RETURN;
1915 }
1916#endif
a0d0e21e 1917 {
cab190d4
JD
1918#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1919 dPOPTOPnnrl;
1920 if (Perl_isnan(left) || Perl_isnan(right))
1921 RETSETNO;
1922 SETs(boolSV(left > right));
1923#else
a0d0e21e 1924 dPOPnv;
54310121 1925 SETs(boolSV(TOPn > value));
cab190d4 1926#endif
a0d0e21e 1927 RETURN;
79072805 1928 }
a0d0e21e
LW
1929}
1930
1931PP(pp_le)
1932{
97aff369 1933 dVAR; dSP; tryAMAGICbinSET(le,0);
28e5dec8
JH
1934#ifdef PERL_PRESERVE_IVUV
1935 SvIV_please(TOPs);
1936 if (SvIOK(TOPs)) {
1937 SvIV_please(TOPm1s);
1938 if (SvIOK(TOPm1s)) {
1939 bool auvok = SvUOK(TOPm1s);
1940 bool buvok = SvUOK(TOPs);
a227d84d 1941
28e5dec8 1942 if (!auvok && !buvok) { /* ## IV <= IV ## */
1b6737cc
AL
1943 const IV aiv = SvIVX(TOPm1s);
1944 const IV biv = SvIVX(TOPs);
28e5dec8
JH
1945
1946 SP--;
1947 SETs(boolSV(aiv <= biv));
1948 RETURN;
1949 }
1950 if (auvok && buvok) { /* ## UV <= UV ## */
1951 UV auv = SvUVX(TOPm1s);
1952 UV buv = SvUVX(TOPs);
1953
1954 SP--;
1955 SETs(boolSV(auv <= buv));
1956 RETURN;
1957 }
1958 if (auvok) { /* ## UV <= IV ## */
1959 UV auv;
1b6737cc
AL
1960 const IV biv = SvIVX(TOPs);
1961
28e5dec8
JH
1962 SP--;
1963 if (biv < 0) {
1964 /* As (a) is a UV, it's >=0, so a cannot be <= */
1965 SETs(&PL_sv_no);
1966 RETURN;
1967 }
1968 auv = SvUVX(TOPs);
28e5dec8
JH
1969 SETs(boolSV(auv <= (UV)biv));
1970 RETURN;
1971 }
1972 { /* ## IV <= UV ## */
1b6737cc 1973 const IV aiv = SvIVX(TOPm1s);
28e5dec8 1974 UV buv;
1b6737cc 1975
28e5dec8
JH
1976 if (aiv < 0) {
1977 /* As (b) is a UV, it's >=0, so a must be <= */
1978 SP--;
1979 SETs(&PL_sv_yes);
1980 RETURN;
1981 }
1982 buv = SvUVX(TOPs);
1983 SP--;
28e5dec8
JH
1984 SETs(boolSV((UV)aiv <= buv));
1985 RETURN;
1986 }
1987 }
1988 }
1989#endif
30de85b6 1990#ifndef NV_PRESERVES_UV
50fb3111
NC
1991#ifdef PERL_PRESERVE_IVUV
1992 else
1993#endif
0bdaccee 1994 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
30de85b6
NC
1995 SP--;
1996 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1997 RETURN;
1998 }
1999#endif
a0d0e21e 2000 {
cab190d4
JD
2001#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2002 dPOPTOPnnrl;
2003 if (Perl_isnan(left) || Perl_isnan(right))
2004 RETSETNO;
2005 SETs(boolSV(left <= right));
2006#else
a0d0e21e 2007 dPOPnv;
54310121 2008 SETs(boolSV(TOPn <= value));
cab190d4 2009#endif
a0d0e21e 2010 RETURN;
79072805 2011 }
a0d0e21e
LW
2012}
2013
2014PP(pp_ge)
2015{
97aff369 2016 dVAR; dSP; tryAMAGICbinSET(ge,0);
28e5dec8
JH
2017#ifdef PERL_PRESERVE_IVUV
2018 SvIV_please(TOPs);
2019 if (SvIOK(TOPs)) {
2020 SvIV_please(TOPm1s);
2021 if (SvIOK(TOPm1s)) {
2022 bool auvok = SvUOK(TOPm1s);
2023 bool buvok = SvUOK(TOPs);
a227d84d 2024
28e5dec8 2025 if (!auvok && !buvok) { /* ## IV >= IV ## */
1b6737cc
AL
2026 const IV aiv = SvIVX(TOPm1s);
2027 const IV biv = SvIVX(TOPs);
2028
28e5dec8
JH
2029 SP--;
2030 SETs(boolSV(aiv >= biv));
2031 RETURN;
2032 }
2033 if (auvok && buvok) { /* ## UV >= UV ## */
1b6737cc
AL
2034 const UV auv = SvUVX(TOPm1s);
2035 const UV buv = SvUVX(TOPs);
2036
28e5dec8
JH
2037 SP--;
2038 SETs(boolSV(auv >= buv));
2039 RETURN;
2040 }
2041 if (auvok) { /* ## UV >= IV ## */
2042 UV auv;
1b6737cc
AL
2043 const IV biv = SvIVX(TOPs);
2044
28e5dec8
JH
2045 SP--;
2046 if (biv < 0) {
2047 /* As (a) is a UV, it's >=0, so it must be >= */
2048 SETs(&PL_sv_yes);
2049 RETURN;
2050 }
2051 auv = SvUVX(TOPs);
28e5dec8
JH
2052 SETs(boolSV(auv >= (UV)biv));
2053 RETURN;
2054 }
2055 { /* ## IV >= UV ## */
1b6737cc 2056 const IV aiv = SvIVX(TOPm1s);
28e5dec8 2057 UV buv;
1b6737cc 2058
28e5dec8
JH
2059 if (aiv < 0) {
2060 /* As (b) is a UV, it's >=0, so a cannot be >= */
2061 SP--;
2062 SETs(&PL_sv_no);
2063 RETURN;
2064 }
2065 buv = SvUVX(TOPs);
2066 SP--;
28e5dec8
JH
2067 SETs(boolSV((UV)aiv >= buv));
2068 RETURN;
2069 }
2070 }
2071 }
2072#endif
30de85b6 2073#ifndef NV_PRESERVES_UV
50fb3111
NC
2074#ifdef PERL_PRESERVE_IVUV
2075 else
2076#endif
0bdaccee 2077 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
30de85b6
NC
2078 SP--;
2079 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2080 RETURN;
2081 }
2082#endif
a0d0e21e 2083 {
cab190d4
JD
2084#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2085 dPOPTOPnnrl;
2086 if (Perl_isnan(left) || Perl_isnan(right))
2087 RETSETNO;
2088 SETs(boolSV(left >= right));
2089#else
a0d0e21e 2090 dPOPnv;
54310121 2091 SETs(boolSV(TOPn >= value));
cab190d4 2092#endif
a0d0e21e 2093 RETURN;
79072805 2094 }
a0d0e21e 2095}
79072805 2096
a0d0e21e
LW
2097PP(pp_ne)
2098{
97aff369 2099 dVAR; dSP; tryAMAGICbinSET(ne,0);
3bb2c415 2100#ifndef NV_PRESERVES_UV
0bdaccee 2101 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
e61d22ef
NC
2102 SP--;
2103 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
3bb2c415
JH
2104 RETURN;
2105 }
2106#endif
28e5dec8
JH
2107#ifdef PERL_PRESERVE_IVUV
2108 SvIV_please(TOPs);
2109 if (SvIOK(TOPs)) {
2110 SvIV_please(TOPm1s);
2111 if (SvIOK(TOPm1s)) {
0bd48802
AL
2112 const bool auvok = SvUOK(TOPm1s);
2113 const bool buvok = SvUOK(TOPs);
a227d84d 2114
30de85b6
NC
2115 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2116 /* Casting IV to UV before comparison isn't going to matter
2117 on 2s complement. On 1s complement or sign&magnitude
2118 (if we have any of them) it could make negative zero
2119 differ from normal zero. As I understand it. (Need to
2120 check - is negative zero implementation defined behaviour
2121 anyway?). NWC */
1b6737cc
AL
2122 const UV buv = SvUVX(POPs);
2123 const UV auv = SvUVX(TOPs);
2124
28e5dec8
JH
2125 SETs(boolSV(auv != buv));
2126 RETURN;
2127 }
2128 { /* ## Mixed IV,UV ## */
2129 IV iv;
2130 UV uv;
2131
2132 /* != is commutative so swap if needed (save code) */
2133 if (auvok) {
2134 /* swap. top of stack (b) is the iv */
2135 iv = SvIVX(TOPs);
2136 SP--;
2137 if (iv < 0) {
2138 /* As (a) is a UV, it's >0, so it cannot be == */
2139 SETs(&PL_sv_yes);
2140 RETURN;
2141 }
2142 uv = SvUVX(TOPs);
2143 } else {
2144 iv = SvIVX(TOPm1s);
2145 SP--;
2146 if (iv < 0) {
2147 /* As (b) is a UV, it's >0, so it cannot be == */
2148 SETs(&PL_sv_yes);
2149 RETURN;
2150 }
2151 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2152 }
28e5dec8
JH
2153 SETs(boolSV((UV)iv != uv));
2154 RETURN;
2155 }
2156 }
2157 }
2158#endif
a0d0e21e 2159 {
cab190d4
JD
2160#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2161 dPOPTOPnnrl;
2162 if (Perl_isnan(left) || Perl_isnan(right))
2163 RETSETYES;
2164 SETs(boolSV(left != right));
2165#else
a0d0e21e 2166 dPOPnv;
54310121 2167 SETs(boolSV(TOPn != value));
cab190d4 2168#endif
a0d0e21e
LW
2169 RETURN;
2170 }
79072805
LW
2171}
2172
a0d0e21e 2173PP(pp_ncmp)
79072805 2174{
97aff369 2175 dVAR; dSP; dTARGET; tryAMAGICbin(ncmp,0);
d8c7644e 2176#ifndef NV_PRESERVES_UV
0bdaccee 2177 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
0bd48802
AL
2178 const UV right = PTR2UV(SvRV(POPs));
2179 const UV left = PTR2UV(SvRV(TOPs));
e61d22ef 2180 SETi((left > right) - (left < right));
d8c7644e
JH
2181 RETURN;
2182 }
2183#endif
28e5dec8
JH
2184#ifdef PERL_PRESERVE_IVUV
2185 /* Fortunately it seems NaN isn't IOK */
2186 SvIV_please(TOPs);
2187 if (SvIOK(TOPs)) {
2188 SvIV_please(TOPm1s);
2189 if (SvIOK(TOPm1s)) {
1b6737cc
AL
2190 const bool leftuvok = SvUOK(TOPm1s);
2191 const bool rightuvok = SvUOK(TOPs);
28e5dec8
JH
2192 I32 value;
2193 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
1b6737cc
AL
2194 const IV leftiv = SvIVX(TOPm1s);
2195 const IV rightiv = SvIVX(TOPs);
28e5dec8
JH
2196
2197 if (leftiv > rightiv)
2198 value = 1;
2199 else if (leftiv < rightiv)
2200 value = -1;
2201 else
2202 value = 0;
2203 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
1b6737cc
AL
2204 const UV leftuv = SvUVX(TOPm1s);
2205 const UV rightuv = SvUVX(TOPs);
28e5dec8
JH
2206
2207 if (leftuv > rightuv)
2208 value = 1;
2209 else if (leftuv < rightuv)
2210 value = -1;
2211 else
2212 value = 0;
2213 } else if (leftuvok) { /* ## UV <=> IV ## */
1b6737cc 2214 const IV rightiv = SvIVX(TOPs);
28e5dec8
JH
2215 if (rightiv < 0) {
2216 /* As (a) is a UV, it's >=0, so it cannot be < */
2217 value = 1;
2218 } else {
1b6737cc 2219 const UV leftuv = SvUVX(TOPm1s);
83bac5dd 2220 if (leftuv > (UV)rightiv) {
28e5dec8
JH
2221 value = 1;
2222 } else if (leftuv < (UV)rightiv) {
2223 value = -1;
2224 } else {
2225 value = 0;
2226 }
2227 }
2228 } else { /* ## IV <=> UV ## */
1b6737cc 2229 const IV leftiv = SvIVX(TOPm1s);
28e5dec8
JH
2230 if (leftiv < 0) {
2231 /* As (b) is a UV, it's >=0, so it must be < */
2232 value = -1;
2233 } else {
1b6737cc 2234 const UV rightuv = SvUVX(TOPs);
83bac5dd 2235 if ((UV)leftiv > rightuv) {
28e5dec8 2236 value = 1;
83bac5dd 2237 } else if ((UV)leftiv < rightuv) {
28e5dec8
JH
2238 value = -1;
2239 } else {
2240 value = 0;
2241 }
2242 }
2243 }
2244 SP--;
2245 SETi(value);
2246 RETURN;
2247 }
2248 }
2249#endif
a0d0e21e
LW
2250 {
2251 dPOPTOPnnrl;
2252 I32 value;
79072805 2253
a3540c92 2254#ifdef Perl_isnan
1ad04cfd
JH
2255 if (Perl_isnan(left) || Perl_isnan(right)) {
2256 SETs(&PL_sv_undef);
2257 RETURN;
2258 }
2259 value = (left > right) - (left < right);
2260#else
ff0cee69 2261 if (left == right)
a0d0e21e 2262 value = 0;
a0d0e21e
LW
2263 else if (left < right)
2264 value = -1;
44a8e56a 2265 else if (left > right)
2266 value = 1;
2267 else {
3280af22 2268 SETs(&PL_sv_undef);
44a8e56a 2269 RETURN;
2270 }
1ad04cfd 2271#endif
a0d0e21e
LW
2272 SETi(value);
2273 RETURN;
79072805 2274 }
a0d0e21e 2275}
79072805 2276
afd9910b 2277PP(pp_sle)
a0d0e21e 2278{
97aff369 2279 dVAR; dSP;
79072805 2280
afd9910b
NC
2281 int amg_type = sle_amg;
2282 int multiplier = 1;
2283 int rhs = 1;
79072805 2284
afd9910b
NC
2285 switch (PL_op->op_type) {
2286 case OP_SLT:
2287 amg_type = slt_amg;
2288 /* cmp < 0 */
2289 rhs = 0;
2290 break;
2291 case OP_SGT:
2292 amg_type = sgt_amg;
2293 /* cmp > 0 */
2294 multiplier = -1;
2295 rhs = 0;
2296 break;
2297 case OP_SGE:
2298 amg_type = sge_amg;
2299 /* cmp >= 0 */
2300 multiplier = -1;
2301 break;
79072805 2302 }
79072805 2303
afd9910b 2304 tryAMAGICbinSET_var(amg_type,0);
a0d0e21e
LW
2305 {
2306 dPOPTOPssrl;
1b6737cc 2307 const int cmp = (IN_LOCALE_RUNTIME
bbce6d69 2308 ? sv_cmp_locale(left, right)
2309 : sv_cmp(left, right));
afd9910b 2310 SETs(boolSV(cmp * multiplier < rhs));
a0d0e21e
LW
2311 RETURN;
2312 }
2313}
79072805 2314
36477c24 2315PP(pp_seq)
2316{
97aff369 2317 dVAR; dSP; tryAMAGICbinSET(seq,0);
36477c24 2318 {
2319 dPOPTOPssrl;
54310121 2320 SETs(boolSV(sv_eq(left, right)));
a0d0e21e
LW
2321 RETURN;
2322 }
2323}
79072805 2324
a0d0e21e 2325PP(pp_sne)
79072805 2326{
97aff369 2327 dVAR; dSP; tryAMAGICbinSET(sne,0);
a0d0e21e
LW
2328 {
2329 dPOPTOPssrl;
54310121 2330 SETs(boolSV(!sv_eq(left, right)));
a0d0e21e 2331 RETURN;
463ee0b2 2332 }
79072805
LW
2333}
2334
a0d0e21e 2335PP(pp_scmp)
79072805 2336{
97aff369 2337 dVAR; dSP; dTARGET; tryAMAGICbin(scmp,0);
a0d0e21e
LW
2338 {
2339 dPOPTOPssrl;
1b6737cc 2340 const int cmp = (IN_LOCALE_RUNTIME
bbce6d69 2341 ? sv_cmp_locale(left, right)
2342 : sv_cmp(left, right));
2343 SETi( cmp );
a0d0e21e
LW
2344 RETURN;
2345 }
2346}
79072805 2347
55497cff 2348PP(pp_bit_and)
2349{
97aff369 2350 dVAR; dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
a0d0e21e
LW
2351 {
2352 dPOPTOPssrl;
5b295bef
RD
2353 SvGETMAGIC(left);
2354 SvGETMAGIC(right);
4633a7c4 2355 if (SvNIOKp(left) || SvNIOKp(right)) {
d0ba1bd2 2356 if (PL_op->op_private & HINT_INTEGER) {
1b6737cc 2357 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
972b05a9 2358 SETi(i);
d0ba1bd2
JH
2359 }
2360 else {
1b6737cc 2361 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
972b05a9 2362 SETu(u);
d0ba1bd2 2363 }
a0d0e21e
LW
2364 }
2365 else {
533c011a 2366 do_vop(PL_op->op_type, TARG, left, right);
a0d0e21e
LW
2367 SETTARG;
2368 }
2369 RETURN;
2370 }
2371}
79072805 2372
a0d0e21e
LW
2373PP(pp_bit_or)
2374{
3658c1f1
NC
2375 dVAR; dSP; dATARGET;
2376 const int op_type = PL_op->op_type;
2377
2378 tryAMAGICbin_var((op_type == OP_BIT_OR ? bor_amg : bxor_amg), opASSIGN);
a0d0e21e
LW
2379 {
2380 dPOPTOPssrl;
5b295bef
RD
2381 SvGETMAGIC(left);
2382 SvGETMAGIC(right);
4633a7c4 2383 if (SvNIOKp(left) || SvNIOKp(right)) {
d0ba1bd2 2384 if (PL_op->op_private & HINT_INTEGER) {
3658c1f1
NC
2385 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2386 const IV r = SvIV_nomg(right);
2387 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2388 SETi(result);
d0ba1bd2
JH
2389 }
2390 else {
3658c1f1
NC
2391 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2392 const UV r = SvUV_nomg(right);
2393 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2394 SETu(result);
d0ba1bd2 2395 }
a0d0e21e
LW
2396 }
2397 else {
3658c1f1 2398 do_vop(op_type, TARG, left, right);
a0d0e21e
LW
2399 SETTARG;
2400 }
2401 RETURN;
79072805 2402 }
a0d0e21e 2403}
79072805 2404
a0d0e21e
LW
2405PP(pp_negate)
2406{
97aff369 2407 dVAR; dSP; dTARGET; tryAMAGICun(neg);
a0d0e21e 2408 {
800401ee 2409 SV * const sv = sv_2num(TOPs);
1b6737cc 2410 const int flags = SvFLAGS(sv);
5b295bef 2411 SvGETMAGIC(sv);
28e5dec8
JH
2412 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2413 /* It's publicly an integer, or privately an integer-not-float */
2414 oops_its_an_int:
9b0e499b
GS
2415 if (SvIsUV(sv)) {
2416 if (SvIVX(sv) == IV_MIN) {
28e5dec8 2417 /* 2s complement assumption. */
9b0e499b
GS
2418 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2419 RETURN;
2420 }
2421 else if (SvUVX(sv) <= IV_MAX) {
beccb14c 2422 SETi(-SvIVX(sv));
9b0e499b
GS
2423 RETURN;
2424 }
2425 }
2426 else if (SvIVX(sv) != IV_MIN) {
2427 SETi(-SvIVX(sv));
2428 RETURN;
2429 }
28e5dec8
JH
2430#ifdef PERL_PRESERVE_IVUV
2431 else {
2432 SETu((UV)IV_MIN);
2433 RETURN;
2434 }
2435#endif
9b0e499b
GS
2436 }
2437 if (SvNIOKp(sv))
a0d0e21e 2438 SETn(-SvNV(sv));
4633a7c4 2439 else if (SvPOKp(sv)) {
a0d0e21e 2440 STRLEN len;
c445ea15 2441 const char * const s = SvPV_const(sv, len);
bbce6d69 2442 if (isIDFIRST(*s)) {
76f68e9b 2443 sv_setpvs(TARG, "-");
a0d0e21e 2444 sv_catsv(TARG, sv);
79072805 2445 }
a0d0e21e
LW
2446 else if (*s == '+' || *s == '-') {
2447 sv_setsv(TARG, sv);
2448 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
79072805 2449 }
8eb28a70
JH
2450 else if (DO_UTF8(sv)) {
2451 SvIV_please(sv);
2452 if (SvIOK(sv))
2453 goto oops_its_an_int;
2454 if (SvNOK(sv))
2455 sv_setnv(TARG, -SvNV(sv));
2456 else {
76f68e9b 2457 sv_setpvs(TARG, "-");
8eb28a70
JH
2458 sv_catsv(TARG, sv);
2459 }
834a4ddd 2460 }
28e5dec8 2461 else {
8eb28a70
JH
2462 SvIV_please(sv);
2463 if (SvIOK(sv))
2464 goto oops_its_an_int;
2465 sv_setnv(TARG, -SvNV(sv));
28e5dec8 2466 }
a0d0e21e 2467 SETTARG;
79072805 2468 }
4633a7c4
LW
2469 else
2470 SETn(-SvNV(sv));
79072805 2471 }
a0d0e21e 2472 RETURN;
79072805
LW
2473}
2474
a0d0e21e 2475PP(pp_not)
79072805 2476{
97aff369 2477 dVAR; dSP; tryAMAGICunSET(not);
3280af22 2478 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
a0d0e21e 2479 return NORMAL;
79072805
LW
2480}
2481
a0d0e21e 2482PP(pp_complement)
79072805 2483{
97aff369 2484 dVAR; dSP; dTARGET; tryAMAGICun(compl);
a0d0e21e
LW
2485 {
2486 dTOPss;
5b295bef 2487 SvGETMAGIC(sv);
4633a7c4 2488 if (SvNIOKp(sv)) {
d0ba1bd2 2489 if (PL_op->op_private & HINT_INTEGER) {
1b6737cc 2490 const IV i = ~SvIV_nomg(sv);
972b05a9 2491 SETi(i);
d0ba1bd2
JH
2492 }
2493 else {
1b6737cc 2494 const UV u = ~SvUV_nomg(sv);
972b05a9 2495 SETu(u);
d0ba1bd2 2496 }
a0d0e21e
LW
2497 }
2498 else {
51723571 2499 register U8 *tmps;
55497cff 2500 register I32 anum;
a0d0e21e
LW
2501 STRLEN len;
2502
10516c54 2503 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
891f9566 2504 sv_setsv_nomg(TARG, sv);
51723571 2505 tmps = (U8*)SvPV_force(TARG, len);
a0d0e21e 2506 anum = len;
1d68d6cd 2507 if (SvUTF8(TARG)) {
a1ca4561 2508 /* Calculate exact length, let's not estimate. */
1d68d6cd 2509 STRLEN targlen = 0;
ba210ebe 2510 STRLEN l;
a1ca4561
YST
2511 UV nchar = 0;
2512 UV nwide = 0;
01f6e806 2513 U8 * const send = tmps + len;
74d49cd0
TS
2514 U8 * const origtmps = tmps;
2515 const UV utf8flags = UTF8_ALLOW_ANYUV;
1d68d6cd 2516
1d68d6cd 2517 while (tmps < send) {
74d49cd0
TS
2518 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2519 tmps += l;
5bbb0b5a 2520 targlen += UNISKIP(~c);
a1ca4561
YST
2521 nchar++;
2522 if (c > 0xff)
2523 nwide++;
1d68d6cd
SC
2524 }
2525
2526 /* Now rewind strings and write them. */
74d49cd0 2527 tmps = origtmps;
a1ca4561
YST
2528
2529 if (nwide) {
01f6e806
AL
2530 U8 *result;
2531 U8 *p;
2532
74d49cd0 2533 Newx(result, targlen + 1, U8);
01f6e806 2534 p = result;
a1ca4561 2535 while (tmps < send) {
74d49cd0
TS
2536 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2537 tmps += l;
01f6e806 2538 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
a1ca4561 2539 }
01f6e806 2540 *p = '\0';
c1c21316
NC
2541 sv_usepvn_flags(TARG, (char*)result, targlen,
2542 SV_HAS_TRAILING_NUL);
a1ca4561
YST
2543 SvUTF8_on(TARG);
2544 }
2545 else {
01f6e806
AL
2546 U8 *result;
2547 U8 *p;
2548
74d49cd0 2549 Newx(result, nchar + 1, U8);
01f6e806 2550 p = result;
a1ca4561 2551 while (tmps < send) {
74d49cd0
TS
2552 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2553 tmps += l;
01f6e806 2554 *p++ = ~c;
a1ca4561 2555 }
01f6e806 2556 *p = '\0';
c1c21316 2557 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
d0a21e00 2558 SvUTF8_off(TARG);
1d68d6cd 2559 }
ec93b65f 2560 SETTARG;
1d68d6cd
SC
2561 RETURN;
2562 }
a0d0e21e 2563#ifdef LIBERAL
51723571
JH
2564 {
2565 register long *tmpl;
2566 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2567 *tmps = ~*tmps;
2568 tmpl = (long*)tmps;
bb7a0f54 2569 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
51723571
JH
2570 *tmpl = ~*tmpl;
2571 tmps = (U8*)tmpl;
2572 }
a0d0e21e
LW
2573#endif
2574 for ( ; anum > 0; anum--, tmps++)
2575 *tmps = ~*tmps;
ec93b65f 2576 SETTARG;
a0d0e21e
LW
2577 }
2578 RETURN;
2579 }
79072805
LW
2580}
2581
a0d0e21e
LW
2582/* integer versions of some of the above */
2583
a0d0e21e 2584PP(pp_i_multiply)
79072805 2585{
97aff369 2586 dVAR; dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
a0d0e21e
LW
2587 {
2588 dPOPTOPiirl;
2589 SETi( left * right );
2590 RETURN;
2591 }
79072805
LW
2592}
2593
a0d0e21e 2594PP(pp_i_divide)
79072805 2595{
ece1bcef 2596 IV num;
97aff369 2597 dVAR; dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
a0d0e21e
LW
2598 {
2599 dPOPiv;
2600 if (value == 0)
ece1bcef
SP
2601 DIE(aTHX_ "Illegal division by zero");
2602 num = POPi;
a0cec769
YST
2603
2604 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2605 if (value == -1)
2606 value = - num;
2607 else
2608 value = num / value;
a0d0e21e
LW
2609 PUSHi( value );
2610 RETURN;
2611 }
79072805
LW
2612}
2613
befad5d1 2614#if defined(__GLIBC__) && IVSIZE == 8
224ec323
JH
2615STATIC
2616PP(pp_i_modulo_0)
befad5d1
NC
2617#else
2618PP(pp_i_modulo)
2619#endif
224ec323
JH
2620{
2621 /* This is the vanilla old i_modulo. */
27da23d5 2622 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
224ec323
JH
2623 {
2624 dPOPTOPiirl;
2625 if (!right)
2626 DIE(aTHX_ "Illegal modulus zero");
a0cec769
YST
2627 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2628 if (right == -1)
2629 SETi( 0 );
2630 else
2631 SETi( left % right );
224ec323
JH
2632 RETURN;
2633 }
2634}
2635
11010fa3 2636#if defined(__GLIBC__) && IVSIZE == 8
224ec323
JH
2637STATIC
2638PP(pp_i_modulo_1)
befad5d1 2639
224ec323 2640{
224ec323 2641 /* This is the i_modulo with the workaround for the _moddi3 bug
fce2b89e 2642 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
224ec323 2643 * See below for pp_i_modulo. */
5186cc12 2644 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
224ec323
JH
2645 {
2646 dPOPTOPiirl;
2647 if (!right)
2648 DIE(aTHX_ "Illegal modulus zero");
a0cec769
YST
2649 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2650 if (right == -1)
2651 SETi( 0 );
2652 else
2653 SETi( left % PERL_ABS(right) );
224ec323
JH
2654 RETURN;
2655 }
224ec323
JH
2656}
2657
a0d0e21e 2658PP(pp_i_modulo)
79072805 2659{
27da23d5 2660 dVAR; dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
224ec323
JH
2661 {
2662 dPOPTOPiirl;
2663 if (!right)
2664 DIE(aTHX_ "Illegal modulus zero");
2665 /* The assumption is to use hereafter the old vanilla version... */
2666 PL_op->op_ppaddr =
2667 PL_ppaddr[OP_I_MODULO] =
1c127fab 2668 Perl_pp_i_modulo_0;
224ec323
JH
2669 /* .. but if we have glibc, we might have a buggy _moddi3
2670 * (at least glicb 2.2.5 is known to have this bug), in other
2671 * words our integer modulus with negative quad as the second
2672 * argument might be broken. Test for this and re-patch the
2673 * opcode dispatch table if that is the case, remembering to
2674 * also apply the workaround so that this first round works
2675 * right, too. See [perl #9402] for more information. */
224ec323
JH
2676 {
2677 IV l = 3;
2678 IV r = -10;
2679 /* Cannot do this check with inlined IV constants since
2680 * that seems to work correctly even with the buggy glibc. */
2681 if (l % r == -3) {
2682 /* Yikes, we have the bug.
2683 * Patch in the workaround version. */
2684 PL_op->op_ppaddr =
2685 PL_ppaddr[OP_I_MODULO] =
2686 &Perl_pp_i_modulo_1;
2687 /* Make certain we work right this time, too. */
32fdb065 2688 right = PERL_ABS(right);
224ec323
JH
2689 }
2690 }
a0cec769
YST
2691 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2692 if (right == -1)
2693 SETi( 0 );
2694 else
2695 SETi( left % right );
224ec323
JH
2696 RETURN;
2697 }
79072805 2698}
befad5d1 2699#endif
79072805 2700
a0d0e21e 2701PP(pp_i_add)
79072805 2702{
97aff369 2703 dVAR; dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
a0d0e21e 2704 {
5e66d4f1 2705 dPOPTOPiirl_ul;
a0d0e21e
LW
2706 SETi( left + right );
2707 RETURN;
79072805 2708 }
79072805
LW
2709}
2710
a0d0e21e 2711PP(pp_i_subtract)
79072805 2712{
97aff369 2713 dVAR; dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
a0d0e21e 2714 {
5e66d4f1 2715 dPOPTOPiirl_ul;
a0d0e21e
LW
2716 SETi( left - right );
2717 RETURN;
79072805 2718 }
79072805
LW
2719}
2720
a0d0e21e 2721PP(pp_i_lt)
79072805 2722{
97aff369 2723 dVAR; dSP; tryAMAGICbinSET(lt,0);
a0d0e21e
LW
2724 {
2725 dPOPTOPiirl;
54310121 2726 SETs(boolSV(left < right));
a0d0e21e
LW
2727 RETURN;
2728 }
79072805
LW
2729}
2730
a0d0e21e 2731PP(pp_i_gt)
79072805 2732{
97aff369 2733 dVAR; dSP; tryAMAGICbinSET(gt,0);
a0d0e21e
LW
2734 {
2735 dPOPTOPiirl;
54310121 2736 SETs(boolSV(left > right));
a0d0e21e
LW
2737 RETURN;
2738 }
79072805
LW
2739}
2740
a0d0e21e 2741PP(pp_i_le)
79072805 2742{
97aff369 2743 dVAR; dSP; tryAMAGICbinSET(le,0);
a0d0e21e
LW
2744 {
2745 dPOPTOPiirl;
54310121 2746 SETs(boolSV(left <= right));
a0d0e21e 2747 RETURN;
85e6fe83 2748 }
79072805
LW
2749}
2750
a0d0e21e 2751PP(pp_i_ge)
79072805 2752{
97aff369 2753 dVAR; dSP; tryAMAGICbinSET(ge,0);
a0d0e21e
LW
2754 {
2755 dPOPTOPiirl;
54310121 2756 SETs(boolSV(left >= right));
a0d0e21e
LW
2757 RETURN;
2758 }
79072805
LW
2759}
2760
a0d0e21e 2761PP(pp_i_eq)
79072805 2762{
97aff369 2763 dVAR; dSP; tryAMAGICbinSET(eq,0);
a0d0e21e
LW
2764 {
2765 dPOPTOPiirl;
54310121 2766 SETs(boolSV(left == right));
a0d0e21e
LW
2767 RETURN;
2768 }
79072805
LW
2769}
2770
a0d0e21e 2771PP(pp_i_ne)
79072805 2772{
97aff369 2773 dVAR; dSP; tryAMAGICbinSET(ne,0);
a0d0e21e
LW
2774 {
2775 dPOPTOPiirl;
54310121 2776 SETs(boolSV(left != right));
a0d0e21e
LW
2777 RETURN;
2778 }
79072805
LW
2779}
2780
a0d0e21e 2781PP(pp_i_ncmp)
79072805 2782{
97aff369 2783 dVAR; dSP; dTARGET; tryAMAGICbin(ncmp,0);
a0d0e21e
LW
2784 {
2785 dPOPTOPiirl;
2786 I32 value;
79072805 2787
a0d0e21e 2788 if (left > right)
79072805 2789 value = 1;
a0d0e21e 2790 else if (left < right)
79072805 2791 value = -1;
a0d0e21e 2792 else
79072805 2793 value = 0;
a0d0e21e
LW
2794 SETi(value);
2795 RETURN;
79072805 2796 }
85e6fe83
LW
2797}
2798
2799PP(pp_i_negate)
2800{
97aff369 2801 dVAR; dSP; dTARGET; tryAMAGICun(neg);
85e6fe83
LW
2802 SETi(-TOPi);
2803 RETURN;
2804}
2805
79072805
LW
2806/* High falutin' math. */
2807
2808PP(pp_atan2)
2809{
97aff369 2810 dVAR; dSP; dTARGET; tryAMAGICbin(atan2,0);
a0d0e21e
LW
2811 {
2812 dPOPTOPnnrl;
a1021d57 2813 SETn(Perl_atan2(left, right));
a0d0e21e
LW
2814 RETURN;
2815 }
79072805
LW
2816}
2817
2818PP(pp_sin)
2819{
71302fe3
NC
2820 dVAR; dSP; dTARGET;
2821 int amg_type = sin_amg;
2822 const char *neg_report = NULL;
bc81784a 2823 NV (*func)(NV) = Perl_sin;
71302fe3
NC
2824 const int op_type = PL_op->op_type;
2825
2826 switch (op_type) {
2827 case OP_COS:
2828 amg_type = cos_amg;
bc81784a 2829 func = Perl_cos;
71302fe3
NC
2830 break;
2831 case OP_EXP:
2832 amg_type = exp_amg;
bc81784a 2833 func = Perl_exp;
71302fe3
NC
2834 break;
2835 case OP_LOG:
2836 amg_type = log_amg;
bc81784a 2837 func = Perl_log;
71302fe3
NC
2838 neg_report = "log";
2839 break;
2840 case OP_SQRT:
2841 amg_type = sqrt_amg;
bc81784a 2842 func = Perl_sqrt;
71302fe3
NC
2843 neg_report = "sqrt";
2844 break;
a0d0e21e 2845 }
79072805 2846
71302fe3 2847 tryAMAGICun_var(amg_type);
a0d0e21e 2848 {
1b6737cc 2849 const NV value = POPn;
71302fe3
NC
2850 if (neg_report) {
2851 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2852 SET_NUMERIC_STANDARD();
2853 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2854 }
2855 }
2856 XPUSHn(func(value));
a0d0e21e
LW
2857 RETURN;
2858 }
79072805
LW
2859}
2860
56cb0a1c
AD
2861/* Support Configure command-line overrides for rand() functions.
2862 After 5.005, perhaps we should replace this by Configure support
2863 for drand48(), random(), or rand(). For 5.005, though, maintain
2864 compatibility by calling rand() but allow the user to override it.
2865 See INSTALL for details. --Andy Dougherty 15 July 1998
2866*/
85ab1d1d
JH
2867/* Now it's after 5.005, and Configure supports drand48() and random(),
2868 in addition to rand(). So the overrides should not be needed any more.
2869 --Jarkko Hietaniemi 27 September 1998
2870 */
2871
2872#ifndef HAS_DRAND48_PROTO
20ce7b12 2873extern double drand48 (void);
56cb0a1c
AD
2874#endif
2875
79072805
LW
2876PP(pp_rand)
2877{
97aff369 2878 dVAR; dSP; dTARGET;
65202027 2879 NV value;
79072805
LW
2880 if (MAXARG < 1)
2881 value = 1.0;
2882 else
2883 value = POPn;
2884 if (value == 0.0)
2885 value = 1.0;
80252599 2886 if (!PL_srand_called) {
85ab1d1d 2887 (void)seedDrand01((Rand_seed_t)seed());
80252599 2888 PL_srand_called = TRUE;
93dc8474 2889 }
85ab1d1d 2890 value *= Drand01();
79072805
LW
2891 XPUSHn(value);
2892 RETURN;
2893}
2894
2895PP(pp_srand)
2896{
97aff369 2897 dVAR; dSP;
0bd48802 2898 const UV anum = (MAXARG < 1) ? seed() : POPu;
85ab1d1d 2899 (void)seedDrand01((Rand_seed_t)anum);
80252599 2900 PL_srand_called = TRUE;
79072805
LW
2901 EXTEND(SP, 1);
2902 RETPUSHYES;
2903}
2904
79072805
LW
2905PP(pp_int)
2906{
97aff369 2907 dVAR; dSP; dTARGET; tryAMAGICun(int);
774d564b 2908 {
800401ee
JH
2909 SV * const sv = sv_2num(TOPs);
2910 const IV iv = SvIV(sv);
28e5dec8
JH
2911 /* XXX it's arguable that compiler casting to IV might be subtly
2912 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2913 else preferring IV has introduced a subtle behaviour change bug. OTOH
2914 relying on floating point to be accurate is a bug. */
2915
c781a409 2916 if (!SvOK(sv)) {
922c4365 2917 SETu(0);
c781a409
RD
2918 }
2919 else if (SvIOK(sv)) {
2920 if (SvIsUV(sv))
2921 SETu(SvUV(sv));
2922 else
28e5dec8 2923 SETi(iv);
c781a409 2924 }
c781a409
RD
2925 else {
2926 const NV value = SvNV(sv);
1048ea30 2927 if (value >= 0.0) {
28e5dec8
JH
2928 if (value < (NV)UV_MAX + 0.5) {
2929 SETu(U_V(value));
2930 } else {
059a1014 2931 SETn(Perl_floor(value));
28e5dec8 2932 }
1048ea30 2933 }
28e5dec8
JH
2934 else {
2935 if (value > (NV)IV_MIN - 0.5) {
2936 SETi(I_V(value));
2937 } else {
1bbae031 2938 SETn(Perl_ceil(value));
28e5dec8
JH
2939 }
2940 }
774d564b 2941 }
79072805 2942 }
79072805
LW
2943 RETURN;
2944}
2945
463ee0b2
LW
2946PP(pp_abs)
2947{
97aff369 2948 dVAR; dSP; dTARGET; tryAMAGICun(abs);
a0d0e21e 2949 {
800401ee 2950 SV * const sv = sv_2num(TOPs);
28e5dec8 2951 /* This will cache the NV value if string isn't actually integer */
800401ee 2952 const IV iv = SvIV(sv);
a227d84d 2953
800401ee 2954 if (!SvOK(sv)) {
922c4365 2955 SETu(0);
800401ee
JH
2956 }
2957 else if (SvIOK(sv)) {
28e5dec8 2958 /* IVX is precise */
800401ee
JH
2959 if (SvIsUV(sv)) {
2960 SETu(SvUV(sv)); /* force it to be numeric only */
28e5dec8
JH
2961 } else {
2962 if (iv >= 0) {
2963 SETi(iv);
2964 } else {
2965 if (iv != IV_MIN) {
2966 SETi(-iv);
2967 } else {
2968 /* 2s complement assumption. Also, not really needed as
2969 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2970 SETu(IV_MIN);
2971 }
a227d84d 2972 }
28e5dec8
JH
2973 }
2974 } else{
800401ee 2975 const NV value = SvNV(sv);
774d564b 2976 if (value < 0.0)
1b6737cc 2977 SETn(-value);
a4474c9e
DD
2978 else
2979 SETn(value);
774d564b 2980 }
a0d0e21e 2981 }
774d564b 2982 RETURN;
463ee0b2
LW
2983}
2984
79072805
LW
2985PP(pp_oct)
2986{
97aff369 2987 dVAR; dSP; dTARGET;
5c144d81 2988 const char *tmps;
53305cf1 2989 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
6f894ead 2990 STRLEN len;
53305cf1
NC
2991 NV result_nv;
2992 UV result_uv;
1b6737cc 2993 SV* const sv = POPs;
79072805 2994
349d4f2f 2995 tmps = (SvPV_const(sv, len));
2bc69dc4
NIS
2996 if (DO_UTF8(sv)) {
2997 /* If Unicode, try to downgrade
2998 * If not possible, croak. */
1b6737cc 2999 SV* const tsv = sv_2mortal(newSVsv(sv));
2bc69dc4
NIS
3000
3001 SvUTF8_on(tsv);
3002 sv_utf8_downgrade(tsv, FALSE);
349d4f2f 3003 tmps = SvPV_const(tsv, len);
2bc69dc4 3004 }
daa2adfd
NC
3005 if (PL_op->op_type == OP_HEX)
3006 goto hex;
3007
6f894ead 3008 while (*tmps && len && isSPACE(*tmps))
53305cf1 3009 tmps++, len--;
9e24b6e2 3010 if (*tmps == '0')
53305cf1 3011 tmps++, len--;
daa2adfd
NC
3012 if (*tmps == 'x') {
3013 hex:
53305cf1 3014 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
daa2adfd 3015 }
9e24b6e2 3016 else if (*tmps == 'b')
53305cf1 3017 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
464e2e8a 3018 else
53305cf1
NC
3019 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3020
3021 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3022 XPUSHn(result_nv);
3023 }
3024 else {
3025 XPUSHu(result_uv);
3026 }
79072805
LW
3027 RETURN;
3028}
3029
3030/* String stuff. */
3031
3032PP(pp_length)
3033{
97aff369 3034 dVAR; dSP; dTARGET;
0bd48802 3035 SV * const sv = TOPs;
a0ed51b3 3036
656266fc 3037 if (SvGAMAGIC(sv)) {
9f621bb0
NC
3038 /* For an overloaded or magic scalar, we can't know in advance if
3039 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
3040 it likes to cache the length. Maybe that should be a documented
3041 feature of it.
92331800
NC
3042 */
3043 STRLEN len;
9f621bb0
NC
3044 const char *const p
3045 = sv_2pv_flags(sv, &len,
3046 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
92331800 3047
9f621bb0
NC
3048 if (!p)
3049 SETs(&PL_sv_undef);
3050 else if (DO_UTF8(sv)) {
899be101 3051 SETi(utf8_length((U8*)p, (U8*)p + len));
92331800
NC
3052 }
3053 else
3054 SETi(len);
656266fc 3055 } else if (SvOK(sv)) {
9f621bb0
NC
3056 /* Neither magic nor overloaded. */
3057 if (DO_UTF8(sv))
3058 SETi(sv_len_utf8(sv));
3059 else
3060 SETi(sv_len(sv));
656266fc
NC
3061 } else {
3062 SETs(&PL_sv_undef);
92331800 3063 }
79072805
LW
3064 RETURN;
3065}
3066
3067PP(pp_substr)
3068{
97aff369 3069 dVAR; dSP; dTARGET;
79072805 3070 SV *sv;
9c5ffd7c 3071 I32 len = 0;
463ee0b2 3072 STRLEN curlen;
9402d6ed 3073 STRLEN utf8_curlen;
79072805
LW
3074 I32 pos;
3075 I32 rem;
84902520 3076 I32 fail;
050e6362 3077 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
e1ec3a88 3078 const char *tmps;
fc15ae8f 3079 const I32 arybase = CopARYBASE_get(PL_curcop);
9402d6ed 3080 SV *repl_sv = NULL;
cbbf8932 3081 const char *repl = NULL;
7b8d334a 3082 STRLEN repl_len;
050e6362 3083 const int num_args = PL_op->op_private & 7;
13e30c65 3084 bool repl_need_utf8_upgrade = FALSE;
9402d6ed 3085 bool repl_is_utf8 = FALSE;
79072805 3086
20408e3c 3087 SvTAINTED_off(TARG); /* decontaminate */
7e2040f0 3088 SvUTF8_off(TARG); /* decontaminate */
78f9721b
SM
3089 if (num_args > 2) {
3090 if (num_args > 3) {
9402d6ed 3091 repl_sv = POPs;
83003860 3092 repl = SvPV_const(repl_sv, repl_len);
9402d6ed 3093 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
7b8d334a 3094 }
79072805 3095 len = POPi;
5d82c453 3096 }
84902520 3097 pos = POPi;
79072805 3098 sv = POPs;
849ca7ee 3099 PUTBACK;
9402d6ed
JH
3100 if (repl_sv) {
3101 if (repl_is_utf8) {
3102 if (!DO_UTF8(sv))
3103 sv_utf8_upgrade(sv);
3104 }
13e30c65
JH
3105 else if (DO_UTF8(sv))
3106 repl_need_utf8_upgrade = TRUE;
9402d6ed 3107 }
5c144d81 3108 tmps = SvPV_const(sv, curlen);
7e2040f0 3109 if (DO_UTF8(sv)) {
9402d6ed
JH
3110 utf8_curlen = sv_len_utf8(sv);
3111 if (utf8_curlen == curlen)
3112 utf8_curlen = 0;
a0ed51b3 3113 else
9402d6ed 3114 curlen = utf8_curlen;
a0ed51b3 3115 }
d1c2b58a 3116 else
9402d6ed 3117 utf8_curlen = 0;
a0ed51b3 3118
84902520
TB
3119 if (pos >= arybase) {
3120 pos -= arybase;
3121 rem = curlen-pos;
3122 fail = rem;
78f9721b 3123 if (num_args > 2) {
5d82c453
GA
3124 if (len < 0) {
3125 rem += len;
3126 if (rem < 0)
3127 rem = 0;
3128 }
3129 else if (rem > len)
3130 rem = len;
3131 }
68dc0745 3132 }
84902520 3133 else {
5d82c453 3134 pos += curlen;
78f9721b 3135 if (num_args < 3)
5d82c453
GA
3136 rem = curlen;
3137 else if (len >= 0) {
3138 rem = pos+len;
3139 if (rem > (I32)curlen)
3140 rem = curlen;
3141 }
3142 else {
3143 rem = curlen+len;
3144 if (rem < pos)
3145 rem = pos;
3146 }
3147 if (pos < 0)
3148 pos = 0;
3149 fail = rem;
3150 rem -= pos;
84902520
TB
3151 }
3152 if (fail < 0) {
e476b1b5
GS
3153 if (lvalue || repl)
3154 Perl_croak(aTHX_ "substr outside of string");
a2a5de95 3155 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
2304df62
AD
3156 RETPUSHUNDEF;
3157 }
79072805 3158 else {
1b6737cc
AL
3159 const I32 upos = pos;
3160 const I32 urem = rem;
9402d6ed 3161 if (utf8_curlen)
a0ed51b3 3162 sv_pos_u2b(sv, &pos, &rem);
79072805 3163 tmps += pos;
781e7547
DM
3164 /* we either return a PV or an LV. If the TARG hasn't been used
3165 * before, or is of that type, reuse it; otherwise use a mortal
3166 * instead. Note that LVs can have an extended lifetime, so also
3167 * dont reuse if refcount > 1 (bug #20933) */
3168 if (SvTYPE(TARG) > SVt_NULL) {
3169 if ( (SvTYPE(TARG) == SVt_PVLV)
3170 ? (!lvalue || SvREFCNT(TARG) > 1)
3171 : lvalue)
3172 {
3173 TARG = sv_newmortal();
3174 }
3175 }
3176
050e6362 3177 sv_setpvn(TARG, tmps, rem);
12aa1545 3178#ifdef USE_LOCALE_COLLATE
14befaf4 3179 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
12aa1545 3180#endif
9402d6ed 3181 if (utf8_curlen)
7f66633b 3182 SvUTF8_on(TARG);
f7928d6c 3183 if (repl) {
13e30c65
JH
3184 SV* repl_sv_copy = NULL;
3185
3186 if (repl_need_utf8_upgrade) {
3187 repl_sv_copy = newSVsv(repl_sv);
3188 sv_utf8_upgrade(repl_sv_copy);
349d4f2f 3189 repl = SvPV_const(repl_sv_copy, repl_len);
13e30c65
JH
3190 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3191 }
502d9230
VP
3192 if (!SvOK(sv))
3193 sv_setpvs(sv, "");
c0dd94a0 3194 sv_insert_flags(sv, pos, rem, repl, repl_len, 0);
9402d6ed 3195 if (repl_is_utf8)
f7928d6c 3196 SvUTF8_on(sv);
9402d6ed
JH
3197 if (repl_sv_copy)
3198 SvREFCNT_dec(repl_sv_copy);
f7928d6c 3199 }
c8faf1c5 3200 else if (lvalue) { /* it's an lvalue! */
dedeecda 3201 if (!SvGMAGICAL(sv)) {
3202 if (SvROK(sv)) {
13c5b33c 3203 SvPV_force_nolen(sv);
a2a5de95
NC
3204 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3205 "Attempt to use reference as lvalue in substr");
dedeecda 3206 }
f7877b28
NC
3207 if (isGV_with_GP(sv))
3208 SvPV_force_nolen(sv);
3209 else if (SvOK(sv)) /* is it defined ? */
7f66633b 3210 (void)SvPOK_only_UTF8(sv);
dedeecda 3211 else
523f125d 3212 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
dedeecda 3213 }
5f05dabc 3214
a0d0e21e
LW
3215 if (SvTYPE(TARG) < SVt_PVLV) {
3216 sv_upgrade(TARG, SVt_PVLV);
c445ea15 3217 sv_magic(TARG, NULL, PERL_MAGIC_substr, NULL, 0);
ed6116ce 3218 }
a0d0e21e 3219
5f05dabc 3220 LvTYPE(TARG) = 'x';
6ff81951
GS
3221 if (LvTARG(TARG) != sv) {
3222 if (LvTARG(TARG))
3223 SvREFCNT_dec(LvTARG(TARG));
b37c2d43 3224 LvTARG(TARG) = SvREFCNT_inc_simple(sv);
6ff81951 3225 }
9aa983d2
JH
3226 LvTARGOFF(TARG) = upos;
3227 LvTARGLEN(TARG) = urem;
79072805
LW
3228 }
3229 }
849ca7ee 3230 SPAGAIN;
79072805
LW
3231 PUSHs(TARG); /* avoid SvSETMAGIC here */
3232 RETURN;
3233}
3234
3235PP(pp_vec)
3236{
97aff369 3237 dVAR; dSP; dTARGET;
1b6737cc
AL
3238 register const IV size = POPi;
3239 register const IV offset = POPi;
3240 register SV * const src = POPs;
3241 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
a0d0e21e 3242
81e118e0
JH
3243 SvTAINTED_off(TARG); /* decontaminate */
3244 if (lvalue) { /* it's an lvalue! */
24aef97f
HS
3245 if (SvREFCNT(TARG) > 1) /* don't share the TARG (#20933) */
3246 TARG = sv_newmortal();
81e118e0
JH
3247 if (SvTYPE(TARG) < SVt_PVLV) {
3248 sv_upgrade(TARG, SVt_PVLV);
c445ea15 3249 sv_magic(TARG, NULL, PERL_MAGIC_vec, NULL, 0);
79072805 3250 }
81e118e0
JH
3251 LvTYPE(TARG) = 'v';
3252 if (LvTARG(TARG) != src) {
3253 if (LvTARG(TARG))
3254 SvREFCNT_dec(LvTARG(TARG));
b37c2d43 3255 LvTARG(TARG) = SvREFCNT_inc_simple(src);
79072805 3256 }
81e118e0
JH
3257 LvTARGOFF(TARG) = offset;
3258 LvTARGLEN(TARG) = size;
79072805
LW
3259 }
3260
81e118e0 3261 sv_setuv(TARG, do_vecget(src, offset, size));
79072805
LW
3262 PUSHs(TARG);
3263 RETURN;
3264}
3265
3266PP(pp_index)
3267{
97aff369 3268 dVAR; dSP; dTARGET;
79072805
LW
3269 SV *big;
3270 SV *little;
c445ea15 3271 SV *temp = NULL;
ad66a58c 3272 STRLEN biglen;
2723d216 3273 STRLEN llen = 0;
79072805
LW
3274 I32 offset;
3275 I32 retval;
73ee8be2
NC
3276 const char *big_p;
3277 const char *little_p;
fc15ae8f 3278 const I32 arybase = CopARYBASE_get(PL_curcop);
2f040f7f
NC
3279 bool big_utf8;
3280 bool little_utf8;
2723d216 3281 const bool is_index = PL_op->op_type == OP_INDEX;
79072805 3282
2723d216
NC
3283 if (MAXARG >= 3) {
3284 /* arybase is in characters, like offset, so combine prior to the
3285 UTF-8 to bytes calculation. */
79072805 3286 offset = POPi - arybase;
2723d216 3287 }
79072805
LW
3288 little = POPs;
3289 big = POPs;
73ee8be2
NC
3290 big_p = SvPV_const(big, biglen);
3291 little_p = SvPV_const(little, llen);
3292
e609e586
NC
3293 big_utf8 = DO_UTF8(big);
3294 little_utf8 = DO_UTF8(little);
3295 if (big_utf8 ^ little_utf8) {
3296 /* One needs to be upgraded. */
2f040f7f
NC
3297 if (little_utf8 && !PL_encoding) {
3298 /* Well, maybe instead we might be able to downgrade the small
3299 string? */
1eced8f8 3300 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
2f040f7f
NC
3301 &little_utf8);
3302 if (little_utf8) {
3303 /* If the large string is ISO-8859-1, and it's not possible to
3304 convert the small string to ISO-8859-1, then there is no
3305 way that it could be found anywhere by index. */
3306 retval = -1;
3307 goto fail;
3308 }
e609e586 3309
2f040f7f
NC
3310 /* At this point, pv is a malloc()ed string. So donate it to temp
3311 to ensure it will get free()d */
3312 little = temp = newSV(0);
73ee8be2
NC
3313 sv_usepvn(temp, pv, llen);
3314 little_p = SvPVX(little);
e609e586 3315 } else {
73ee8be2
NC
3316 temp = little_utf8
3317 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
2f040f7f
NC
3318
3319 if (PL_encoding) {
3320 sv_recode_to_utf8(temp, PL_encoding);
3321 } else {
3322 sv_utf8_upgrade(temp);
3323 }
3324 if (little_utf8) {
3325 big = temp;
3326 big_utf8 = TRUE;
73ee8be2 3327 big_p = SvPV_const(big, biglen);
2f040f7f
NC
3328 } else {
3329 little = temp;
73ee8be2 3330 little_p = SvPV_const(little, llen);
2f040f7f 3331 }
e609e586
NC
3332 }
3333 }
73ee8be2
NC
3334 if (SvGAMAGIC(big)) {
3335 /* Life just becomes a lot easier if I use a temporary here.
3336 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3337 will trigger magic and overloading again, as will fbm_instr()
3338 */
59cd0e26
NC
3339 big = newSVpvn_flags(big_p, biglen,
3340 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
73ee8be2
NC
3341 big_p = SvPVX(big);
3342 }
e4e44778 3343 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
73ee8be2
NC
3344 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3345 warn on undef, and we've already triggered a warning with the
3346 SvPV_const some lines above. We can't remove that, as we need to
3347 call some SvPV to trigger overloading early and find out if the
3348 string is UTF-8.
3349 This is all getting to messy. The API isn't quite clean enough,
3350 because data access has side effects.
3351 */
59cd0e26
NC
3352 little = newSVpvn_flags(little_p, llen,
3353 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
73ee8be2
NC
3354 little_p = SvPVX(little);
3355 }
e609e586 3356
79072805 3357 if (MAXARG < 3)
2723d216 3358 offset = is_index ? 0 : biglen;
a0ed51b3 3359 else {
ad66a58c 3360 if (big_utf8 && offset > 0)
a0ed51b3 3361 sv_pos_u2b(big, &offset, 0);
73ee8be2
NC
3362 if (!is_index)
3363 offset += llen;
a0ed51b3 3364 }
79072805
LW
3365 if (offset < 0)
3366 offset = 0;
ad66a58c
NC
3367 else if (offset > (I32)biglen)
3368 offset = biglen;
73ee8be2
NC
3369 if (!(little_p = is_index
3370 ? fbm_instr((unsigned char*)big_p + offset,
3371 (unsigned char*)big_p + biglen, little, 0)
3372 : rninstr(big_p, big_p + offset,
3373 little_p, little_p + llen)))
a0ed51b3 3374 retval = -1;
ad66a58c 3375 else {
73ee8be2 3376 retval = little_p - big_p;
ad66a58c
NC
3377 if (retval > 0 && big_utf8)
3378 sv_pos_b2u(big, &retval);
3379 }
e609e586
NC
3380 if (temp)
3381 SvREFCNT_dec(temp);
2723d216 3382 fail:
a0ed51b3 3383 PUSHi(retval + arybase);
79072805
LW
3384 RETURN;
3385}
3386
3387PP(pp_sprintf)
3388{
97aff369 3389 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
20ee07fb
RGS
3390 if (SvTAINTED(MARK[1]))
3391 TAINT_PROPER("sprintf");
79072805 3392 do_sprintf(TARG, SP-MARK, MARK+1);
bbce6d69 3393 TAINT_IF(SvTAINTED(TARG));
79072805
LW
3394 SP = ORIGMARK;
3395 PUSHTARG;
3396 RETURN;
3397}
3398
79072805
LW
3399PP(pp_ord)
3400{
97aff369 3401 dVAR; dSP; dTARGET;
1eced8f8 3402
7df053ec 3403 SV *argsv = POPs;
ba210ebe 3404 STRLEN len;
349d4f2f 3405 const U8 *s = (U8*)SvPV_const(argsv, len);
121910a4 3406
799ef3cb 3407 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
1eced8f8 3408 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
799ef3cb 3409 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
121910a4
JH
3410 argsv = tmpsv;
3411 }
79072805 3412
872c91ae 3413 XPUSHu(DO_UTF8(argsv) ?
89ebb4a3 3414 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
5fc32dea 3415 (UV)(*s & 0xff));
68795e93 3416
79072805
LW
3417 RETURN;
3418}
3419
463ee0b2
LW
3420PP(pp_chr)
3421{
97aff369 3422 dVAR; dSP; dTARGET;
463ee0b2 3423 char *tmps;
8a064bd6
JH
3424 UV value;
3425
3426 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3427 ||
3428 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3429 if (IN_BYTES) {
3430 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3431 } else {
3432 (void) POPs; /* Ignore the argument value. */
3433 value = UNICODE_REPLACEMENT;
3434 }
3435 } else {
3436 value = POPu;
3437 }
463ee0b2 3438
862a34c6 3439 SvUPGRADE(TARG,SVt_PV);
a0ed51b3 3440
0064a8a9 3441 if (value > 255 && !IN_BYTES) {
eb160463 3442 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
62961d2e 3443 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
349d4f2f 3444 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
a0ed51b3
LW
3445 *tmps = '\0';
3446 (void)SvPOK_only(TARG);
aa6ffa16 3447 SvUTF8_on(TARG);
a0ed51b3
LW
3448 XPUSHs(TARG);
3449 RETURN;
3450 }
3451
748a9306 3452 SvGROW(TARG,2);
463ee0b2
LW
3453 SvCUR_set(TARG, 1);
3454 tmps = SvPVX(TARG);
eb160463 3455 *tmps++ = (char)value;
748a9306 3456 *tmps = '\0';
a0d0e21e 3457 (void)SvPOK_only(TARG);
4c5ed6e2 3458
88632417 3459 if (PL_encoding && !IN_BYTES) {
799ef3cb 3460 sv_recode_to_utf8(TARG, PL_encoding);
88632417
JH
3461 tmps = SvPVX(TARG);
3462 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
4c5ed6e2
TS
3463 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3464 SvGROW(TARG, 2);
d5a15ac2 3465 tmps = SvPVX(TARG);
4c5ed6e2
TS
3466 SvCUR_set(TARG, 1);
3467 *tmps++ = (char)value;
88632417 3468 *tmps = '\0';
4c5ed6e2 3469 SvUTF8_off(TARG);
88632417
JH
3470 }
3471 }
4c5ed6e2 3472
463ee0b2
LW
3473 XPUSHs(TARG);
3474 RETURN;
3475}
3476
79072805
LW
3477PP(pp_crypt)
3478{
79072805 3479#ifdef HAS_CRYPT
97aff369 3480 dVAR; dSP; dTARGET;
5f74f29c 3481 dPOPTOPssrl;
85c16d83 3482 STRLEN len;
10516c54 3483 const char *tmps = SvPV_const(left, len);
2bc69dc4 3484
85c16d83 3485 if (DO_UTF8(left)) {
2bc69dc4 3486 /* If Unicode, try to downgrade.
f2791508
JH
3487 * If not possible, croak.
3488 * Yes, we made this up. */
1b6737cc 3489 SV* const tsv = sv_2mortal(newSVsv(left));
2bc69dc4 3490
f2791508 3491 SvUTF8_on(tsv);
2bc69dc4 3492 sv_utf8_downgrade(tsv, FALSE);
349d4f2f 3493 tmps = SvPV_const(tsv, len);
85c16d83 3494 }
05404ffe
JH
3495# ifdef USE_ITHREADS
3496# ifdef HAS_CRYPT_R
3497 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3498 /* This should be threadsafe because in ithreads there is only
3499 * one thread per interpreter. If this would not be true,
3500 * we would need a mutex to protect this malloc. */
3501 PL_reentrant_buffer->_crypt_struct_buffer =
3502 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3503#if defined(__GLIBC__) || defined(__EMX__)
3504 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3505 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3506 /* work around glibc-2.2.5 bug */
3507 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3508 }
05404ffe 3509#endif
6ab58e4d 3510 }
05404ffe
JH
3511# endif /* HAS_CRYPT_R */
3512# endif /* USE_ITHREADS */
5f74f29c 3513# ifdef FCRYPT
83003860 3514 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
5f74f29c 3515# else
83003860 3516 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
5f74f29c 3517# endif
ec93b65f 3518 SETTARG;
4808266b 3519 RETURN;
79072805 3520#else
b13b2135 3521 DIE(aTHX_
79072805
LW
3522 "The crypt() function is unimplemented due to excessive paranoia.");
3523#endif
79072805
LW
3524}
3525
3526PP(pp_ucfirst)
3527{
97aff369 3528 dVAR;
39644a26 3529 dSP;
d54190f6 3530 SV *source = TOPs;
a0ed51b3 3531 STRLEN slen;
d54190f6
NC
3532 STRLEN need;
3533 SV *dest;
3534 bool inplace = TRUE;
3535 bool doing_utf8;
12e9c124 3536 const int op_type = PL_op->op_type;
d54190f6
NC
3537 const U8 *s;
3538 U8 *d;
3539 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3540 STRLEN ulen;
3541 STRLEN tculen;
3542
3543 SvGETMAGIC(source);
3544 if (SvOK(source)) {
3545 s = (const U8*)SvPV_nomg_const(source, slen);
3546 } else {
0a0ffbce
RGS
3547 if (ckWARN(WARN_UNINITIALIZED))
3548 report_uninit(source);
1eced8f8 3549 s = (const U8*)"";
d54190f6
NC
3550 slen = 0;
3551 }
a0ed51b3 3552
d54190f6
NC
3553 if (slen && DO_UTF8(source) && UTF8_IS_START(*s)) {
3554 doing_utf8 = TRUE;
44bc797b 3555 utf8_to_uvchr(s, &ulen);
12e9c124
NC
3556 if (op_type == OP_UCFIRST) {
3557 toTITLE_utf8(s, tmpbuf, &tculen);
3558 } else {
3559 toLOWER_utf8(s, tmpbuf, &tculen);
3560 }
d54190f6 3561 /* If the two differ, we definately cannot do inplace. */
1eced8f8 3562 inplace = (ulen == tculen);
d54190f6
NC
3563 need = slen + 1 - ulen + tculen;
3564 } else {
3565 doing_utf8 = FALSE;
3566 need = slen + 1;
3567 }
3568
17fa0776 3569 if (SvPADTMP(source) && !SvREADONLY(source) && inplace && SvTEMP(source)) {
d54190f6
NC
3570 /* We can convert in place. */
3571
3572 dest = source;
3573 s = d = (U8*)SvPV_force_nomg(source, slen);
3574 } else {
3575 dTARGET;
3576
3577 dest = TARG;
3578
3579 SvUPGRADE(dest, SVt_PV);
3b416f41 3580 d = (U8*)SvGROW(dest, need);
d54190f6
NC
3581 (void)SvPOK_only(dest);
3582
3583 SETs(dest);
3584
3585 inplace = FALSE;
3586 }
44bc797b 3587
d54190f6
NC
3588 if (doing_utf8) {
3589 if(!inplace) {
3a2263fe
RGS
3590 /* slen is the byte length of the whole SV.
3591 * ulen is the byte length of the original Unicode character
3592 * stored as UTF-8 at s.
12e9c124
NC
3593 * tculen is the byte length of the freshly titlecased (or
3594 * lowercased) Unicode character stored as UTF-8 at tmpbuf.
3595 * We first set the result to be the titlecased (/lowercased)
3596 * character, and then append the rest of the SV data. */
d54190f6 3597 sv_setpvn(dest, (char*)tmpbuf, tculen);
3a2263fe 3598 if (slen > ulen)
d54190f6
NC
3599 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3600 SvUTF8_on(dest);
a0ed51b3
LW
3601 }
3602 else {
d54190f6
NC
3603 Copy(tmpbuf, d, tculen, U8);
3604 SvCUR_set(dest, need - 1);
a0ed51b3 3605 }
a0ed51b3 3606 }
626727d5 3607 else {
d54190f6 3608 if (*s) {
2de3dbcc 3609 if (IN_LOCALE_RUNTIME) {
31351b04 3610 TAINT;
d54190f6
NC
3611 SvTAINTED_on(dest);
3612 *d = (op_type == OP_UCFIRST)
3613 ? toUPPER_LC(*s) : toLOWER_LC(*s);
31351b04
JS
3614 }
3615 else
d54190f6
NC
3616 *d = (op_type == OP_UCFIRST) ? toUPPER(*s) : toLOWER(*s);
3617 } else {
3618 /* See bug #39028 */
3619 *d = *s;
3620 }
3621
3622 if (SvUTF8(source))
3623 SvUTF8_on(dest);
3624
3625 if (!inplace) {
3626 /* This will copy the trailing NUL */
3627 Copy(s + 1, d + 1, slen, U8);
3628 SvCUR_set(dest, need - 1);
bbce6d69 3629 }
bbce6d69 3630 }
d54190f6 3631 SvSETMAGIC(dest);
79072805
LW
3632 RETURN;
3633}
3634
67306194
NC
3635/* There's so much setup/teardown code common between uc and lc, I wonder if
3636 it would be worth merging the two, and just having a switch outside each
3637 of the three tight loops. */
79072805
LW
3638PP(pp_uc)
3639{
97aff369 3640 dVAR;
39644a26 3641 dSP;
67306194 3642 SV *source = TOPs;
463ee0b2 3643 STRLEN len;
67306194
NC
3644 STRLEN min;
3645 SV *dest;
3646 const U8 *s;
3647 U8 *d;
79072805 3648
67306194
NC
3649 SvGETMAGIC(source);
3650
3651 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
17fa0776 3652 && SvTEMP(source) && !DO_UTF8(source)) {
67306194
NC
3653 /* We can convert in place. */
3654
3655 dest = source;
3656 s = d = (U8*)SvPV_force_nomg(source, len);
3657 min = len + 1;
3658 } else {
a0ed51b3 3659 dTARGET;
a0ed51b3 3660
67306194 3661 dest = TARG;
128c9517 3662
67306194
NC
3663 /* The old implementation would copy source into TARG at this point.
3664 This had the side effect that if source was undef, TARG was now
3665 an undefined SV with PADTMP set, and they don't warn inside
3666 sv_2pv_flags(). However, we're now getting the PV direct from
3667 source, which doesn't have PADTMP set, so it would warn. Hence the
3668 little games. */
3669
3670 if (SvOK(source)) {
3671 s = (const U8*)SvPV_nomg_const(source, len);
3672 } else {
0a0ffbce
RGS
3673 if (ckWARN(WARN_UNINITIALIZED))
3674 report_uninit(source);
1eced8f8 3675 s = (const U8*)"";
67306194 3676 len = 0;
a0ed51b3 3677 }
67306194
NC
3678 min = len + 1;
3679
3680 SvUPGRADE(dest, SVt_PV);
3b416f41 3681 d = (U8*)SvGROW(dest, min);
67306194
NC
3682 (void)SvPOK_only(dest);
3683
3684 SETs(dest);
a0ed51b3 3685 }
31351b04 3686
67306194
NC
3687 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3688 to check DO_UTF8 again here. */
3689
3690 if (DO_UTF8(source)) {
3691 const U8 *const send = s + len;
3692 U8 tmpbuf[UTF8_MAXBYTES+1];
3693
3694 while (s < send) {
3695 const STRLEN u = UTF8SKIP(s);
3696 STRLEN ulen;
3697
3698 toUPPER_utf8(s, tmpbuf, &ulen);
3699 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3700 /* If the eventually required minimum size outgrows
3701 * the available space, we need to grow. */
3702 const UV o = d - (U8*)SvPVX_const(dest);
3703
3704 /* If someone uppercases one million U+03B0s we SvGROW() one
3705 * million times. Or we could try guessing how much to
3706 allocate without allocating too much. Such is life. */
3707 SvGROW(dest, min);
3708 d = (U8*)SvPVX(dest) + o;
3709 }
3710 Copy(tmpbuf, d, ulen, U8);
3711 d += ulen;
3712 s += u;
3713 }
3714 SvUTF8_on(dest);
3715 *d = '\0';
3716 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3717 } else {
3718 if (len) {
3719 const U8 *const send = s + len;
2de3dbcc 3720 if (IN_LOCALE_RUNTIME) {
31351b04 3721 TAINT;
67306194
NC
3722 SvTAINTED_on(dest);
3723 for (; s < send; d++, s++)
3724 *d = toUPPER_LC(*s);
31351b04
JS
3725 }
3726 else {
67306194
NC
3727 for (; s < send; d++, s++)
3728 *d = toUPPER(*s);
31351b04 3729 }
bbce6d69 3730 }
67306194
NC
3731 if (source != dest) {
3732 *d = '\0';
3733 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3734 }
79072805 3735 }
67306194 3736 SvSETMAGIC(dest);
79072805
LW
3737 RETURN;
3738}
3739
3740PP(pp_lc)
3741{
97aff369 3742 dVAR;
39644a26 3743 dSP;
ec9af7d4 3744 SV *source = TOPs;
463ee0b2 3745 STRLEN len;
ec9af7d4
NC
3746 STRLEN min;
3747 SV *dest;
3748 const U8 *s;
3749 U8 *d;
79072805 3750
ec9af7d4
NC
3751 SvGETMAGIC(source);
3752
3753 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
17fa0776 3754 && SvTEMP(source) && !DO_UTF8(source)) {
ec9af7d4
NC
3755 /* We can convert in place. */
3756
3757 dest = source;
3758 s = d = (U8*)SvPV_force_nomg(source, len);
3759 min = len + 1;
3760 } else {
a0ed51b3 3761 dTARGET;
a0ed51b3 3762
ec9af7d4
NC
3763 dest = TARG;
3764
3765 /* The old implementation would copy source into TARG at this point.
3766 This had the side effect that if source was undef, TARG was now
3767 an undefined SV with PADTMP set, and they don't warn inside
3768 sv_2pv_flags(). However, we're now getting the PV direct from
3769 source, which doesn't have PADTMP set, so it would warn. Hence the
3770 little games. */
3771
3772 if (SvOK(source)) {
3773 s = (const U8*)SvPV_nomg_const(source, len);
3774 } else {
0a0ffbce
RGS
3775 if (ckWARN(WARN_UNINITIALIZED))
3776 report_uninit(source);
1eced8f8 3777 s = (const U8*)"";
ec9af7d4 3778 len = 0;
a0ed51b3 3779 }
ec9af7d4 3780 min = len + 1;
128c9517 3781
ec9af7d4 3782 SvUPGRADE(dest, SVt_PV);
3b416f41 3783 d = (U8*)SvGROW(dest, min);
ec9af7d4
NC
3784 (void)SvPOK_only(dest);
3785
3786 SETs(dest);
3787 }
3788
3789 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3790 to check DO_UTF8 again here. */
3791
3792 if (DO_UTF8(source)) {
3793 const U8 *const send = s + len;
3794 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3795
3796 while (s < send) {
3797 const STRLEN u = UTF8SKIP(s);
3798 STRLEN ulen;
3799 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
89ebb4a3
JH
3800
3801#define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode U+03A3 */
ec9af7d4
NC
3802 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3803 NOOP;
3804 /*
3805 * Now if the sigma is NOT followed by
3806 * /$ignorable_sequence$cased_letter/;
3807 * and it IS preceded by /$cased_letter$ignorable_sequence/;
3808 * where $ignorable_sequence is [\x{2010}\x{AD}\p{Mn}]*
3809 * and $cased_letter is [\p{Ll}\p{Lo}\p{Lt}]
3810 * then it should be mapped to 0x03C2,
3811 * (GREEK SMALL LETTER FINAL SIGMA),
3812 * instead of staying 0x03A3.
3813 * "should be": in other words, this is not implemented yet.
3814 * See lib/unicore/SpecialCasing.txt.
3815 */
3816 }
3817 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3818 /* If the eventually required minimum size outgrows
3819 * the available space, we need to grow. */
3820 const UV o = d - (U8*)SvPVX_const(dest);
89ebb4a3 3821
ec9af7d4
NC
3822 /* If someone lowercases one million U+0130s we SvGROW() one
3823 * million times. Or we could try guessing how much to
3824 allocate without allocating too much. Such is life. */
3825 SvGROW(dest, min);
3826 d = (U8*)SvPVX(dest) + o;
a0ed51b3 3827 }
ec9af7d4
NC
3828 Copy(tmpbuf, d, ulen, U8);
3829 d += ulen;
3830 s += u;
a0ed51b3 3831 }
ec9af7d4
NC
3832 SvUTF8_on(dest);
3833 *d = '\0';
3834 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3835 } else {
31351b04 3836 if (len) {
ec9af7d4 3837 const U8 *const send = s + len;
2de3dbcc 3838 if (IN_LOCALE_RUNTIME) {
31351b04 3839 TAINT;
ec9af7d4
NC
3840 SvTAINTED_on(dest);
3841 for (; s < send; d++, s++)
3842 *d = toLOWER_LC(*s);
31351b04
JS
3843 }
3844 else {
ec9af7d4
NC
3845 for (; s < send; d++, s++)
3846 *d = toLOWER(*s);
31351b04 3847 }
bbce6d69 3848 }
ec9af7d4
NC
3849 if (source != dest) {
3850 *d = '\0';
3851 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3852 }
79072805 3853 }
ec9af7d4 3854 SvSETMAGIC(dest);
79072805
LW
3855 RETURN;
3856}
3857
a0d0e21e 3858PP(pp_quotemeta)
79072805 3859{
97aff369 3860 dVAR; dSP; dTARGET;
1b6737cc 3861 SV * const sv = TOPs;
a0d0e21e 3862 STRLEN len;
0d46e09a 3863 register const char *s = SvPV_const(sv,len);
79072805 3864
7e2040f0 3865 SvUTF8_off(TARG); /* decontaminate */
a0d0e21e 3866 if (len) {
1b6737cc 3867 register char *d;
862a34c6 3868 SvUPGRADE(TARG, SVt_PV);
c07a80fd 3869 SvGROW(TARG, (len * 2) + 1);
a0d0e21e 3870 d = SvPVX(TARG);
7e2040f0 3871 if (DO_UTF8(sv)) {
0dd2cdef 3872 while (len) {
fd400ab9 3873 if (UTF8_IS_CONTINUED(*s)) {
0dd2cdef
LW
3874 STRLEN ulen = UTF8SKIP(s);
3875 if (ulen > len)
3876 ulen = len;
3877 len -= ulen;
3878 while (ulen--)
3879 *d++ = *s++;
3880 }
3881 else {
3882 if (!isALNUM(*s))
3883 *d++ = '\\';
3884 *d++ = *s++;
3885 len--;
3886 }
3887 }
7e2040f0 3888 SvUTF8_on(TARG);
0dd2cdef
LW
3889 }
3890 else {
3891 while (len--) {
3892 if (!isALNUM(*s))
3893 *d++ = '\\';
3894 *d++ = *s++;
3895 }
79072805 3896 }
a0d0e21e 3897 *d = '\0';
349d4f2f 3898 SvCUR_set(TARG, d - SvPVX_const(TARG));
3aa33fe5 3899 (void)SvPOK_only_UTF8(TARG);
79072805 3900 }
a0d0e21e
LW
3901 else
3902 sv_setpvn(TARG, s, len);
ec93b65f 3903 SETTARG;
79072805
LW
3904 RETURN;
3905}
3906
a0d0e21e 3907/* Arrays. */
79072805 3908
a0d0e21e 3909PP(pp_aslice)
79072805 3910{
97aff369 3911 dVAR; dSP; dMARK; dORIGMARK;
502c6561 3912 register AV *const av = MUTABLE_AV(POPs);
1b6737cc 3913 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
79072805 3914
a0d0e21e 3915 if (SvTYPE(av) == SVt_PVAV) {
fc15ae8f 3916 const I32 arybase = CopARYBASE_get(PL_curcop);
4ad10a0b
VP
3917 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
3918 bool can_preserve = FALSE;
3919
3920 if (localizing) {
3921 MAGIC *mg;
3922 HV *stash;
3923
3924 can_preserve = SvCANEXISTDELETE(av);
3925 }
3926
3927 if (lval && localizing) {
1b6737cc 3928 register SV **svp;
748a9306 3929 I32 max = -1;
924508f0 3930 for (svp = MARK + 1; svp <= SP; svp++) {
4ea561bc 3931 const I32 elem = SvIV(*svp);
748a9306
LW
3932 if (elem > max)
3933 max = elem;
3934 }
3935 if (max > AvMAX(av))
3936 av_extend(av, max);
3937 }
4ad10a0b 3938
a0d0e21e 3939 while (++MARK <= SP) {
1b6737cc 3940 register SV **svp;
4ea561bc 3941 I32 elem = SvIV(*MARK);
4ad10a0b 3942 bool preeminent = TRUE;
a0d0e21e 3943
748a9306
LW
3944 if (elem > 0)
3945 elem -= arybase;
4ad10a0b
VP
3946 if (localizing && can_preserve) {
3947 /* If we can determine whether the element exist,
3948 * Try to preserve the existenceness of a tied array
3949 * element by using EXISTS and DELETE if possible.
3950 * Fallback to FETCH and STORE otherwise. */
3951 preeminent = av_exists(av, elem);
3952 }
3953
a0d0e21e
LW
3954 svp = av_fetch(av, elem, lval);
3955 if (lval) {
3280af22 3956 if (!svp || *svp == &PL_sv_undef)
cea2e8a9 3957 DIE(aTHX_ PL_no_aelem, elem);
4ad10a0b
VP
3958 if (localizing) {
3959 if (preeminent)
3960 save_aelem(av, elem, svp);
3961 else
3962 SAVEADELETE(av, elem);
3963 }
79072805 3964 }
3280af22 3965 *MARK = svp ? *svp : &PL_sv_undef;
79072805
LW
3966 }
3967 }
748a9306 3968 if (GIMME != G_ARRAY) {
a0d0e21e 3969 MARK = ORIGMARK;
04ab2c87 3970 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
a0d0e21e
LW
3971 SP = MARK;
3972 }
79072805
LW
3973 RETURN;
3974}
3975
878d132a
NC
3976PP(pp_aeach)
3977{
3978 dVAR;
3979 dSP;
502c6561 3980 AV *array = MUTABLE_AV(POPs);
878d132a 3981 const I32 gimme = GIMME_V;
453d94a9 3982 IV *iterp = Perl_av_iter_p(aTHX_ array);
878d132a
NC
3983 const IV current = (*iterp)++;
3984
3985 if (current > av_len(array)) {
3986 *iterp = 0;
3987 if (gimme == G_SCALAR)
3988 RETPUSHUNDEF;
3989 else
3990 RETURN;
3991 }
3992
3993 EXTEND(SP, 2);
3994 mPUSHi(CopARYBASE_get(PL_curcop) + current);
3995 if (gimme == G_ARRAY) {
3996 SV **const element = av_fetch(array, current, 0);
3997 PUSHs(element ? *element : &PL_sv_undef);
3998 }
3999 RETURN;
4000}
4001
4002PP(pp_akeys)
4003{
4004 dVAR;
4005 dSP;
502c6561 4006 AV *array = MUTABLE_AV(POPs);
878d132a
NC
4007 const I32 gimme = GIMME_V;
4008
4009 *Perl_av_iter_p(aTHX_ array) = 0;
4010
4011 if (gimme == G_SCALAR) {
4012 dTARGET;
4013 PUSHi(av_len(array) + 1);
4014 }
4015 else if (gimme == G_ARRAY) {
4016 IV n = Perl_av_len(aTHX_ array);
4017 IV i = CopARYBASE_get(PL_curcop);
4018
4019 EXTEND(SP, n + 1);
4020
4021 if (PL_op->op_type == OP_AKEYS) {
4022 n += i;
4023 for (; i <= n; i++) {
4024 mPUSHi(i);
4025 }
4026 }
4027 else {
4028 for (i = 0; i <= n; i++) {
4029 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4030 PUSHs(elem ? *elem : &PL_sv_undef);
4031 }
4032 }
4033 }
4034 RETURN;
4035}
4036
79072805
LW
4037/* Associative arrays. */
4038
4039PP(pp_each)
4040{
97aff369 4041 dVAR;
39644a26 4042 dSP;
85fbaab2 4043 HV * hash = MUTABLE_HV(POPs);
c07a80fd 4044 HE *entry;
f54cb97a 4045 const I32 gimme = GIMME_V;
8ec5e241 4046
c07a80fd 4047 PUTBACK;
c750a3ec 4048 /* might clobber stack_sp */
6d822dc4 4049 entry = hv_iternext(hash);
c07a80fd 4050 SPAGAIN;
79072805 4051
79072805
LW
4052 EXTEND(SP, 2);
4053 if (entry) {
1b6737cc 4054 SV* const sv = hv_iterkeysv(entry);
574c8022 4055 PUSHs(sv); /* won't clobber stack_sp */
54310121 4056 if (gimme == G_ARRAY) {
59af0135 4057 SV *val;
c07a80fd 4058 PUTBACK;
c750a3ec 4059 /* might clobber stack_sp */
6d822dc4 4060 val = hv_iterval(hash, entry);
c07a80fd 4061 SPAGAIN;
59af0135 4062 PUSHs(val);
79072805 4063 }
79072805 4064 }
54310121 4065 else if (gimme == G_SCALAR)
79072805
LW
4066 RETPUSHUNDEF;
4067
4068 RETURN;
4069}
4070
7332a6c4
VP
4071STATIC OP *
4072S_do_delete_local(pTHX)
79072805 4073{
97aff369 4074 dVAR;
39644a26 4075 dSP;
f54cb97a 4076 const I32 gimme = GIMME_V;
7332a6c4
VP
4077 const MAGIC *mg;
4078 HV *stash;
4079
4080 if (PL_op->op_private & OPpSLICE) {
4081 dMARK; dORIGMARK;
4082 SV * const osv = POPs;
4083 const bool tied = SvRMAGICAL(osv)
4084 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4085 const bool can_preserve = SvCANEXISTDELETE(osv)
4086 || mg_find((const SV *)osv, PERL_MAGIC_env);
4087 const U32 type = SvTYPE(osv);
4088 if (type == SVt_PVHV) { /* hash element */
4089 HV * const hv = MUTABLE_HV(osv);
4090 while (++MARK <= SP) {
4091 SV * const keysv = *MARK;
4092 SV *sv = NULL;
4093 bool preeminent = TRUE;
4094 if (can_preserve)
4095 preeminent = hv_exists_ent(hv, keysv, 0);
4096 if (tied) {
4097 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4098 if (he)
4099 sv = HeVAL(he);
4100 else
4101 preeminent = FALSE;
4102 }
4103 else {
4104 sv = hv_delete_ent(hv, keysv, 0, 0);
4105 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4106 }
4107 if (preeminent) {
4108 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4109 if (tied) {
4110 *MARK = sv_mortalcopy(sv);
4111 mg_clear(sv);
4112 } else
4113 *MARK = sv;
4114 }
4115 else {
4116 SAVEHDELETE(hv, keysv);
4117 *MARK = &PL_sv_undef;
4118 }
4119 }
4120 }
4121 else if (type == SVt_PVAV) { /* array element */
4122 if (PL_op->op_flags & OPf_SPECIAL) {
4123 AV * const av = MUTABLE_AV(osv);
4124 while (++MARK <= SP) {
4125 I32 idx = SvIV(*MARK);
4126 SV *sv = NULL;
4127 bool preeminent = TRUE;
4128 if (can_preserve)
4129 preeminent = av_exists(av, idx);
4130 if (tied) {
4131 SV **svp = av_fetch(av, idx, 1);
4132 if (svp)
4133 sv = *svp;
4134 else
4135 preeminent = FALSE;
4136 }
4137 else {
4138 sv = av_delete(av, idx, 0);
4139 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4140 }
4141 if (preeminent) {
4142 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4143 if (tied) {
4144 *MARK = sv_mortalcopy(sv);
4145 mg_clear(sv);
4146 } else
4147 *MARK = sv;
4148 }
4149 else {
4150 SAVEADELETE(av, idx);
4151 *MARK = &PL_sv_undef;
4152 }
4153 }
4154 }
4155 }
4156 else
4157 DIE(aTHX_ "Not a HASH reference");
4158 if (gimme == G_VOID)
4159 SP = ORIGMARK;
4160 else if (gimme == G_SCALAR) {
4161 MARK = ORIGMARK;
4162 if (SP > MARK)
4163 *++MARK = *SP;
4164 else
4165 *++MARK = &PL_sv_undef;
4166 SP = MARK;
4167 }
4168 }
4169 else {
4170 SV * const keysv = POPs;
4171 SV * const osv = POPs;
4172 const bool tied = SvRMAGICAL(osv)
4173 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4174 const bool can_preserve = SvCANEXISTDELETE(osv)
4175 || mg_find((const SV *)osv, PERL_MAGIC_env);
4176 const U32 type = SvTYPE(osv);
4177 SV *sv = NULL;
4178 if (type == SVt_PVHV) {
4179 HV * const hv = MUTABLE_HV(osv);
4180 bool preeminent = TRUE;
4181 if (can_preserve)
4182 preeminent = hv_exists_ent(hv, keysv, 0);
4183 if (tied) {
4184 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4185 if (he)
4186 sv = HeVAL(he);
4187 else
4188 preeminent = FALSE;
4189 }
4190 else {
4191 sv = hv_delete_ent(hv, keysv, 0, 0);
4192 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4193 }
4194 if (preeminent) {
4195 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4196 if (tied) {
4197 SV *nsv = sv_mortalcopy(sv);
4198 mg_clear(sv);
4199 sv = nsv;
4200 }
4201 }
4202 else
4203 SAVEHDELETE(hv, keysv);
4204 }
4205 else if (type == SVt_PVAV) {
4206 if (PL_op->op_flags & OPf_SPECIAL) {
4207 AV * const av = MUTABLE_AV(osv);
4208 I32 idx = SvIV(keysv);
4209 bool preeminent = TRUE;
4210 if (can_preserve)
4211 preeminent = av_exists(av, idx);
4212 if (tied) {
4213 SV **svp = av_fetch(av, idx, 1);
4214 if (svp)
4215 sv = *svp;
4216 else
4217 preeminent = FALSE;
4218 }
4219 else {
4220 sv = av_delete(av, idx, 0);
4221 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4222 }
4223 if (preeminent) {
4224 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4225 if (tied) {
4226 SV *nsv = sv_mortalcopy(sv);
4227 mg_clear(sv);
4228 sv = nsv;
4229 }
4230 }
4231 else
4232 SAVEADELETE(av, idx);
4233 }
4234 else
4235 DIE(aTHX_ "panic: avhv_delete no longer supported");
4236 }
4237 else
4238 DIE(aTHX_ "Not a HASH reference");
4239 if (!sv)
4240 sv = &PL_sv_undef;
4241 if (gimme != G_VOID)
4242 PUSHs(sv);
4243 }
4244
4245 RETURN;
4246}
4247
4248PP(pp_delete)
4249{
4250 dVAR;
4251 dSP;
4252 I32 gimme;
4253 I32 discard;
4254
4255 if (PL_op->op_private & OPpLVAL_INTRO)
4256 return do_delete_local();
4257
4258 gimme = GIMME_V;
4259 discard = (gimme == G_VOID) ? G_DISCARD : 0;
5f05dabc 4260
533c011a 4261 if (PL_op->op_private & OPpSLICE) {
5f05dabc 4262 dMARK; dORIGMARK;
85fbaab2 4263 HV * const hv = MUTABLE_HV(POPs);
1b6737cc 4264 const U32 hvtype = SvTYPE(hv);
01020589
GS
4265 if (hvtype == SVt_PVHV) { /* hash element */
4266 while (++MARK <= SP) {
1b6737cc 4267 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
01020589
GS
4268 *MARK = sv ? sv : &PL_sv_undef;
4269 }
5f05dabc 4270 }
6d822dc4
MS
4271 else if (hvtype == SVt_PVAV) { /* array element */
4272 if (PL_op->op_flags & OPf_SPECIAL) {
4273 while (++MARK <= SP) {
502c6561 4274 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
6d822dc4
MS
4275 *MARK = sv ? sv : &PL_sv_undef;
4276 }
4277 }
01020589
GS
4278 }
4279 else
4280 DIE(aTHX_ "Not a HASH reference");
54310121 4281 if (discard)
4282 SP = ORIGMARK;
4283 else if (gimme == G_SCALAR) {
5f05dabc 4284 MARK = ORIGMARK;
9111c9c0
DM
4285 if (SP > MARK)
4286 *++MARK = *SP;
4287 else
4288 *++MARK = &PL_sv_undef;
5f05dabc 4289 SP = MARK;
4290 }
4291 }
4292 else {
4293 SV *keysv = POPs;
85fbaab2 4294 HV * const hv = MUTABLE_HV(POPs);
1b6737cc 4295 SV *sv;
97fcbf96
MB
4296 if (SvTYPE(hv) == SVt_PVHV)
4297 sv = hv_delete_ent(hv, keysv, discard, 0);
01020589
GS
4298 else if (SvTYPE(hv) == SVt_PVAV) {
4299 if (PL_op->op_flags & OPf_SPECIAL)
502c6561 4300 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
af288a60
HS
4301 else
4302 DIE(aTHX_ "panic: avhv_delete no longer supported");
01020589 4303 }
97fcbf96 4304 else
cea2e8a9 4305 DIE(aTHX_ "Not a HASH reference");
5f05dabc 4306 if (!sv)
3280af22 4307 sv = &PL_sv_undef;
54310121 4308 if (!discard)
4309 PUSHs(sv);
79072805 4310 }
79072805
LW
4311 RETURN;
4312}
4313
a0d0e21e 4314PP(pp_exists)
79072805 4315{
97aff369 4316 dVAR;
39644a26 4317 dSP;
afebc493
GS
4318 SV *tmpsv;
4319 HV *hv;
4320
4321 if (PL_op->op_private & OPpEXISTS_SUB) {
4322 GV *gv;
0bd48802 4323 SV * const sv = POPs;
f2c0649b 4324 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
afebc493
GS
4325 if (cv)
4326 RETPUSHYES;
4327 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4328 RETPUSHYES;
4329 RETPUSHNO;
4330 }
4331 tmpsv = POPs;
85fbaab2 4332 hv = MUTABLE_HV(POPs);
c750a3ec 4333 if (SvTYPE(hv) == SVt_PVHV) {
ae77835f 4334 if (hv_exists_ent(hv, tmpsv, 0))
c750a3ec 4335 RETPUSHYES;
ef54e1a4
JH
4336 }
4337 else if (SvTYPE(hv) == SVt_PVAV) {
01020589 4338 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
502c6561 4339 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
01020589
GS
4340 RETPUSHYES;
4341 }
ef54e1a4
JH
4342 }
4343 else {
cea2e8a9 4344 DIE(aTHX_ "Not a HASH reference");
a0d0e21e 4345 }
a0d0e21e
LW
4346 RETPUSHNO;
4347}
79072805 4348
a0d0e21e
LW
4349PP(pp_hslice)
4350{
97aff369 4351 dVAR; dSP; dMARK; dORIGMARK;
85fbaab2 4352 register HV * const hv = MUTABLE_HV(POPs);
1b6737cc
AL
4353 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4354 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
d30e492c 4355 bool can_preserve = FALSE;
79072805 4356
eb85dfd3
DM
4357 if (localizing) {
4358 MAGIC *mg;
4359 HV *stash;
4360
d30e492c
VP
4361 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
4362 can_preserve = TRUE;
eb85dfd3
DM
4363 }
4364
6d822dc4 4365 while (++MARK <= SP) {
1b6737cc 4366 SV * const keysv = *MARK;
6d822dc4
MS
4367 SV **svp;
4368 HE *he;
d30e492c
VP
4369 bool preeminent = TRUE;
4370
4371 if (localizing && can_preserve) {
4372 /* If we can determine whether the element exist,
4373 * try to preserve the existenceness of a tied hash
4374 * element by using EXISTS and DELETE if possible.
4375 * Fallback to FETCH and STORE otherwise. */
4376 preeminent = hv_exists_ent(hv, keysv, 0);
6d822dc4 4377 }
eb85dfd3 4378
6d822dc4 4379 he = hv_fetch_ent(hv, keysv, lval, 0);
fe5bfecd 4380 svp = he ? &HeVAL(he) : NULL;
eb85dfd3 4381
6d822dc4
MS
4382 if (lval) {
4383 if (!svp || *svp == &PL_sv_undef) {
be2597df 4384 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
6d822dc4
MS
4385 }
4386 if (localizing) {
7a2e501a 4387 if (HvNAME_get(hv) && isGV(*svp))
159b6efe 4388 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
47cfc530
VP
4389 else if (preeminent)
4390 save_helem_flags(hv, keysv, svp,
4391 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4392 else
4393 SAVEHDELETE(hv, keysv);
6d822dc4
MS
4394 }
4395 }
4396 *MARK = svp ? *svp : &PL_sv_undef;
79072805 4397 }
a0d0e21e
LW
4398 if (GIMME != G_ARRAY) {
4399 MARK = ORIGMARK;
04ab2c87 4400 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
a0d0e21e 4401 SP = MARK;
79072805 4402 }
a0d0e21e
LW
4403 RETURN;
4404}
4405
4406/* List operators. */
4407
4408PP(pp_list)
4409{
97aff369 4410 dVAR; dSP; dMARK;
a0d0e21e
LW
4411 if (GIMME != G_ARRAY) {
4412 if (++MARK <= SP)
4413 *MARK = *SP; /* unwanted list, return last item */
8990e307 4414 else
3280af22 4415 *MARK = &PL_sv_undef;
a0d0e21e 4416 SP = MARK;
79072805 4417 }
a0d0e21e 4418 RETURN;
79072805
LW
4419}
4420
a0d0e21e 4421PP(pp_lslice)
79072805 4422{
97aff369 4423 dVAR;
39644a26 4424 dSP;
1b6737cc
AL
4425 SV ** const lastrelem = PL_stack_sp;
4426 SV ** const lastlelem = PL_stack_base + POPMARK;
4427 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4428 register SV ** const firstrelem = lastlelem + 1;
fc15ae8f 4429 const I32 arybase = CopARYBASE_get(PL_curcop);
42e73ed0 4430 I32 is_something_there = FALSE;
1b6737cc
AL
4431
4432 register const I32 max = lastrelem - lastlelem;
a0d0e21e 4433 register SV **lelem;
a0d0e21e
LW
4434
4435 if (GIMME != G_ARRAY) {
4ea561bc 4436 I32 ix = SvIV(*lastlelem);
748a9306
LW
4437 if (ix < 0)
4438 ix += max;
4439 else
4440 ix -= arybase;
a0d0e21e 4441 if (ix < 0 || ix >= max)
3280af22 4442 *firstlelem = &PL_sv_undef;
a0d0e21e
LW
4443 else
4444 *firstlelem = firstrelem[ix];
4445 SP = firstlelem;
4446 RETURN;
4447 }
4448
4449 if (max == 0) {
4450 SP = firstlelem - 1;
4451 RETURN;
4452 }
4453
4454 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4ea561bc 4455 I32 ix = SvIV(*lelem);
c73bf8e3 4456 if (ix < 0)
a0d0e21e 4457 ix += max;
b13b2135 4458 else
748a9306 4459 ix -= arybase;
c73bf8e3
HS
4460 if (ix < 0 || ix >= max)
4461 *lelem = &PL_sv_undef;
4462 else {
4463 is_something_there = TRUE;
4464 if (!(*lelem = firstrelem[ix]))
3280af22 4465 *lelem = &PL_sv_undef;
748a9306 4466 }
79072805 4467 }
4633a7c4
LW
4468 if (is_something_there)
4469 SP = lastlelem;
4470 else
4471 SP = firstlelem - 1;
79072805
LW
4472 RETURN;
4473}
4474
a0d0e21e
LW
4475PP(pp_anonlist)
4476{
97aff369 4477 dVAR; dSP; dMARK; dORIGMARK;
1b6737cc 4478 const I32 items = SP - MARK;
ad64d0ec 4479 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
44a8e56a 4480 SP = ORIGMARK; /* av_make() might realloc stack_sp */
6e449a3a
MHM
4481 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4482 ? newRV_noinc(av) : av);
a0d0e21e
LW
4483 RETURN;
4484}
4485
4486PP(pp_anonhash)
79072805 4487{
97aff369 4488 dVAR; dSP; dMARK; dORIGMARK;
78c72037 4489 HV* const hv = newHV();
a0d0e21e
LW
4490
4491 while (MARK < SP) {
1b6737cc 4492 SV * const key = *++MARK;
561b68a9 4493 SV * const val = newSV(0);
a0d0e21e
LW
4494 if (MARK < SP)
4495 sv_setsv(val, *++MARK);
a2a5de95
NC
4496 else
4497 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
f12c7020 4498 (void)hv_store_ent(hv,key,val,0);
79072805 4499 }
a0d0e21e 4500 SP = ORIGMARK;
6e449a3a 4501 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
ad64d0ec 4502 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
79072805
LW
4503 RETURN;
4504}
4505
a0d0e21e 4506PP(pp_splice)
79072805 4507{
27da23d5 4508 dVAR; dSP; dMARK; dORIGMARK;
502c6561 4509 register AV *ary = MUTABLE_AV(*++MARK);
a0d0e21e
LW
4510 register SV **src;
4511 register SV **dst;
4512 register I32 i;
4513 register I32 offset;
4514 register I32 length;
4515 I32 newlen;
4516 I32 after;
4517 I32 diff;
ad64d0ec 4518 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
93965878 4519
1b6737cc 4520 if (mg) {
ad64d0ec 4521 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
93965878 4522 PUSHMARK(MARK);
8ec5e241 4523 PUTBACK;
a60c0954 4524 ENTER;
864dbfa3 4525 call_method("SPLICE",GIMME_V);
a60c0954 4526 LEAVE;
93965878
NIS
4527 SPAGAIN;
4528 RETURN;
4529 }
79072805 4530
a0d0e21e 4531 SP++;
79072805 4532
a0d0e21e 4533 if (++MARK < SP) {
4ea561bc 4534 offset = i = SvIV(*MARK);
a0d0e21e 4535 if (offset < 0)
93965878 4536 offset += AvFILLp(ary) + 1;
a0d0e21e 4537 else
fc15ae8f 4538 offset -= CopARYBASE_get(PL_curcop);
84902520 4539 if (offset < 0)
cea2e8a9 4540 DIE(aTHX_ PL_no_aelem, i);
a0d0e21e
LW
4541 if (++MARK < SP) {
4542 length = SvIVx(*MARK++);
48cdf507
GA
4543 if (length < 0) {
4544 length += AvFILLp(ary) - offset + 1;
4545 if (length < 0)
4546 length = 0;
4547 }
79072805
LW
4548 }
4549 else
a0d0e21e 4550 length = AvMAX(ary) + 1; /* close enough to infinity */
79072805 4551 }
a0d0e21e
LW
4552 else {
4553 offset = 0;
4554 length = AvMAX(ary) + 1;
4555 }
8cbc2e3b 4556 if (offset > AvFILLp(ary) + 1) {
a2a5de95 4557 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
93965878 4558 offset = AvFILLp(ary) + 1;
8cbc2e3b 4559 }
93965878 4560 after = AvFILLp(ary) + 1 - (offset + length);
a0d0e21e
LW
4561 if (after < 0) { /* not that much array */
4562 length += after; /* offset+length now in array */
4563 after = 0;
4564 if (!AvALLOC(ary))
4565 av_extend(ary, 0);
4566 }
4567
4568 /* At this point, MARK .. SP-1 is our new LIST */
4569
4570 newlen = SP - MARK;
4571 diff = newlen - length;
13d7cbc1
GS
4572 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4573 av_reify(ary);
a0d0e21e 4574
50528de0
WL
4575 /* make new elements SVs now: avoid problems if they're from the array */
4576 for (dst = MARK, i = newlen; i; i--) {
1b6737cc 4577 SV * const h = *dst;
f2b990bf 4578 *dst++ = newSVsv(h);
50528de0
WL
4579 }
4580
a0d0e21e 4581 if (diff < 0) { /* shrinking the area */
95b63a38 4582 SV **tmparyval = NULL;
a0d0e21e 4583 if (newlen) {
a02a5408 4584 Newx(tmparyval, newlen, SV*); /* so remember insertion */
a0d0e21e 4585 Copy(MARK, tmparyval, newlen, SV*);
79072805 4586 }
a0d0e21e
LW
4587
4588 MARK = ORIGMARK + 1;
4589 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4590 MEXTEND(MARK, length);
4591 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4592 if (AvREAL(ary)) {
bbce6d69 4593 EXTEND_MORTAL(length);
36477c24 4594 for (i = length, dst = MARK; i; i--) {
d689ffdd 4595 sv_2mortal(*dst); /* free them eventualy */
36477c24 4596 dst++;
4597 }
a0d0e21e
LW
4598 }
4599 MARK += length - 1;
79072805 4600 }
a0d0e21e
LW
4601 else {
4602 *MARK = AvARRAY(ary)[offset+length-1];
4603 if (AvREAL(ary)) {
d689ffdd 4604 sv_2mortal(*MARK);
a0d0e21e
LW
4605 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4606 SvREFCNT_dec(*dst++); /* free them now */
79072805 4607 }
a0d0e21e 4608 }
93965878 4609 AvFILLp(ary) += diff;
a0d0e21e
LW
4610
4611 /* pull up or down? */
4612
4613 if (offset < after) { /* easier to pull up */
4614 if (offset) { /* esp. if nothing to pull */
4615 src = &AvARRAY(ary)[offset-1];
4616 dst = src - diff; /* diff is negative */
4617 for (i = offset; i > 0; i--) /* can't trust Copy */
4618 *dst-- = *src--;
79072805 4619 }
a0d0e21e 4620 dst = AvARRAY(ary);
9c6bc640 4621 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
a0d0e21e
LW
4622 AvMAX(ary) += diff;
4623 }
4624 else {
4625 if (after) { /* anything to pull down? */
4626 src = AvARRAY(ary) + offset + length;
4627 dst = src + diff; /* diff is negative */
4628 Move(src, dst, after, SV*);
79072805 4629 }
93965878 4630 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
a0d0e21e
LW
4631 /* avoid later double free */
4632 }
4633 i = -diff;
4634 while (i)
3280af22 4635 dst[--i] = &PL_sv_undef;
a0d0e21e
LW
4636
4637 if (newlen) {
50528de0 4638 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
a0d0e21e
LW
4639 Safefree(tmparyval);
4640 }
4641 }
4642 else { /* no, expanding (or same) */
d3961450 4643 SV** tmparyval = NULL;
a0d0e21e 4644 if (length) {
a02a5408 4645 Newx(tmparyval, length, SV*); /* so remember deletion */
a0d0e21e
LW
4646 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4647 }
4648
4649 if (diff > 0) { /* expanding */
a0d0e21e 4650 /* push up or down? */
a0d0e21e
LW
4651 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4652 if (offset) {
4653 src = AvARRAY(ary);
4654 dst = src - diff;
4655 Move(src, dst, offset, SV*);
79072805 4656 }
9c6bc640 4657 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
a0d0e21e 4658 AvMAX(ary) += diff;
93965878 4659 AvFILLp(ary) += diff;
79072805
LW
4660 }
4661 else {
93965878
NIS
4662 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4663 av_extend(ary, AvFILLp(ary) + diff);
4664 AvFILLp(ary) += diff;
a0d0e21e
LW
4665
4666 if (after) {
93965878 4667 dst = AvARRAY(ary) + AvFILLp(ary);
a0d0e21e
LW
4668 src = dst - diff;
4669 for (i = after; i; i--) {
4670 *dst-- = *src--;
4671 }
79072805
LW
4672 }
4673 }
a0d0e21e
LW
4674 }
4675
50528de0
WL
4676 if (newlen) {
4677 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
a0d0e21e 4678 }
50528de0 4679
a0d0e21e
LW
4680 MARK = ORIGMARK + 1;
4681 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4682 if (length) {
4683 Copy(tmparyval, MARK, length, SV*);
4684 if (AvREAL(ary)) {
bbce6d69 4685 EXTEND_MORTAL(length);
36477c24 4686 for (i = length, dst = MARK; i; i--) {
d689ffdd 4687 sv_2mortal(*dst); /* free them eventualy */
36477c24 4688 dst++;
4689 }
79072805
LW
4690 }
4691 }
a0d0e21e
LW
4692 MARK += length - 1;
4693 }
4694 else if (length--) {
4695 *MARK = tmparyval[length];
4696 if (AvREAL(ary)) {
d689ffdd 4697 sv_2mortal(*MARK);
a0d0e21e
LW
4698 while (length-- > 0)
4699 SvREFCNT_dec(tmparyval[length]);
79072805 4700 }
79072805 4701 }
a0d0e21e 4702 else
3280af22 4703 *MARK = &PL_sv_undef;
d3961450 4704 Safefree(tmparyval);
79072805 4705 }
a0d0e21e 4706 SP = MARK;
79072805
LW
4707 RETURN;
4708}
4709
a0d0e21e 4710PP(pp_push)
79072805 4711{
27da23d5 4712 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
502c6561 4713 register AV * const ary = MUTABLE_AV(*++MARK);
ad64d0ec 4714 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
79072805 4715
1b6737cc 4716 if (mg) {
ad64d0ec 4717 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
93965878
NIS
4718 PUSHMARK(MARK);
4719 PUTBACK;
a60c0954 4720 ENTER;
864dbfa3 4721 call_method("PUSH",G_SCALAR|G_DISCARD);
a60c0954 4722 LEAVE;
93965878 4723 SPAGAIN;
93965878 4724 }
a60c0954 4725 else {
89c14e2e 4726 PL_delaymagic = DM_DELAY;
a60c0954 4727 for (++MARK; MARK <= SP; MARK++) {
561b68a9 4728 SV * const sv = newSV(0);
a60c0954
NIS
4729 if (*MARK)
4730 sv_setsv(sv, *MARK);
0a75904b 4731 av_store(ary, AvFILLp(ary)+1, sv);
a60c0954 4732 }
89c14e2e 4733 if (PL_delaymagic & DM_ARRAY)
ad64d0ec 4734 mg_set(MUTABLE_SV(ary));
89c14e2e
BB
4735
4736 PL_delaymagic = 0;
6eeabd23
VP
4737 }
4738 SP = ORIGMARK;
4739 if (OP_GIMME(PL_op, 0) != G_VOID) {
4740 PUSHi( AvFILL(ary) + 1 );
79072805 4741 }
79072805
LW
4742 RETURN;
4743}
4744
a0d0e21e 4745PP(pp_shift)
79072805 4746{
97aff369 4747 dVAR;
39644a26 4748 dSP;
502c6561 4749 AV * const av = MUTABLE_AV(POPs);
789b4bc9 4750 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
79072805 4751 EXTEND(SP, 1);
c2b4a044 4752 assert (sv);
d689ffdd 4753 if (AvREAL(av))
a0d0e21e
LW
4754 (void)sv_2mortal(sv);
4755 PUSHs(sv);
79072805 4756 RETURN;
79072805
LW
4757}
4758
a0d0e21e 4759PP(pp_unshift)
79072805 4760{
27da23d5 4761 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
502c6561 4762 register AV *ary = MUTABLE_AV(*++MARK);
ad64d0ec 4763 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
93965878 4764
1b6737cc 4765 if (mg) {
ad64d0ec 4766 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
7fd66d9d 4767 PUSHMARK(MARK);
93965878 4768 PUTBACK;
a60c0954 4769 ENTER;
864dbfa3 4770 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
a60c0954 4771 LEAVE;
93965878 4772 SPAGAIN;
93965878 4773 }
a60c0954 4774 else {
1b6737cc 4775 register I32 i = 0;
a60c0954
NIS
4776 av_unshift(ary, SP - MARK);
4777 while (MARK < SP) {
1b6737cc 4778 SV * const sv = newSVsv(*++MARK);
a60c0954
NIS
4779 (void)av_store(ary, i++, sv);
4780 }
79072805 4781 }
a0d0e21e 4782 SP = ORIGMARK;
6eeabd23 4783 if (OP_GIMME(PL_op, 0) != G_VOID) {
5658d0a9
LR
4784 PUSHi( AvFILL(ary) + 1 );
4785 }
79072805 4786 RETURN;
79072805
LW
4787}
4788
a0d0e21e 4789PP(pp_reverse)
79072805 4790{
97aff369 4791 dVAR; dSP; dMARK;
1b6737cc 4792 SV ** const oldsp = SP;
79072805 4793
a0d0e21e
LW
4794 if (GIMME == G_ARRAY) {
4795 MARK++;
4796 while (MARK < SP) {
1b6737cc 4797 register SV * const tmp = *MARK;
a0d0e21e
LW
4798 *MARK++ = *SP;
4799 *SP-- = tmp;
4800 }
dd58a1ab 4801 /* safe as long as stack cannot get extended in the above */
a0d0e21e 4802 SP = oldsp;
79072805
LW
4803 }
4804 else {
a0d0e21e
LW
4805 register char *up;
4806 register char *down;
4807 register I32 tmp;
4808 dTARGET;
4809 STRLEN len;
9f7d9405 4810 PADOFFSET padoff_du;
79072805 4811
7e2040f0 4812 SvUTF8_off(TARG); /* decontaminate */
a0d0e21e 4813 if (SP - MARK > 1)
3280af22 4814 do_join(TARG, &PL_sv_no, MARK, SP);
1e21d011 4815 else {
e1f795dc
RGS
4816 sv_setsv(TARG, (SP > MARK)
4817 ? *SP
29289021 4818 : (padoff_du = find_rundefsvoffset(),
00b1698f
NC
4819 (padoff_du == NOT_IN_PAD
4820 || PAD_COMPNAME_FLAGS_isOUR(padoff_du))
e1f795dc 4821 ? DEFSV : PAD_SVl(padoff_du)));
1e21d011
B
4822
4823 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
4824 report_uninit(TARG);
4825 }
4826
a0d0e21e
LW
4827 up = SvPV_force(TARG, len);
4828 if (len > 1) {
7e2040f0 4829 if (DO_UTF8(TARG)) { /* first reverse each character */
dfe13c55 4830 U8* s = (U8*)SvPVX(TARG);
349d4f2f 4831 const U8* send = (U8*)(s + len);
a0ed51b3 4832 while (s < send) {
d742c382 4833 if (UTF8_IS_INVARIANT(*s)) {
a0ed51b3
LW
4834 s++;
4835 continue;
4836 }
4837 else {
9041c2e3 4838 if (!utf8_to_uvchr(s, 0))
a0dbb045 4839 break;
dfe13c55 4840 up = (char*)s;
a0ed51b3 4841 s += UTF8SKIP(s);
dfe13c55 4842 down = (char*)(s - 1);
a0dbb045 4843 /* reverse this character */
a0ed51b3
LW
4844 while (down > up) {
4845 tmp = *up;
4846 *up++ = *down;
eb160463 4847 *down-- = (char)tmp;
a0ed51b3
LW
4848 }
4849 }
4850 }
4851 up = SvPVX(TARG);
4852 }
a0d0e21e
LW
4853 down = SvPVX(TARG) + len - 1;
4854 while (down > up) {
4855 tmp = *up;
4856 *up++ = *down;
eb160463 4857 *down-- = (char)tmp;
a0d0e21e 4858 }
3aa33fe5 4859 (void)SvPOK_only_UTF8(TARG);
79072805 4860 }
a0d0e21e
LW
4861 SP = MARK + 1;
4862 SETTARG;
79072805 4863 }
a0d0e21e 4864 RETURN;
79072805
LW
4865}
4866
a0d0e21e 4867PP(pp_split)
79072805 4868{
27da23d5 4869 dVAR; dSP; dTARG;
a0d0e21e 4870 AV *ary;
467f0320 4871 register IV limit = POPi; /* note, negative is forever */
1b6737cc 4872 SV * const sv = POPs;
a0d0e21e 4873 STRLEN len;
727b7506 4874 register const char *s = SvPV_const(sv, len);
1b6737cc 4875 const bool do_utf8 = DO_UTF8(sv);
727b7506 4876 const char *strend = s + len;
44a8e56a 4877 register PMOP *pm;
d9f97599 4878 register REGEXP *rx;
a0d0e21e 4879 register SV *dstr;
727b7506 4880 register const char *m;
a0d0e21e 4881 I32 iters = 0;
bb7a0f54 4882 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
792b2c16 4883 I32 maxiters = slen + 10;
c1a7495a 4884 I32 trailing_empty = 0;
727b7506 4885 const char *orig;
1b6737cc 4886 const I32 origlimit = limit;
a0d0e21e
LW
4887 I32 realarray = 0;
4888 I32 base;
f54cb97a 4889 const I32 gimme = GIMME_V;
c1a7495a 4890 const bool gimme_scalar = (GIMME_V == G_SCALAR);
f54cb97a 4891 const I32 oldsave = PL_savestack_ix;
437d3b4e 4892 U32 make_mortal = SVs_TEMP;
7fba1cd6 4893 bool multiline = 0;
b37c2d43 4894 MAGIC *mg = NULL;
79072805 4895
44a8e56a 4896#ifdef DEBUGGING
4897 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4898#else
4899 pm = (PMOP*)POPs;
4900#endif
a0d0e21e 4901 if (!pm || !s)
2269b42e 4902 DIE(aTHX_ "panic: pp_split");
aaa362c4 4903 rx = PM_GETRE(pm);
bbce6d69 4904
07bc277f
NC
4905 TAINT_IF((RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) &&
4906 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
bbce6d69 4907
a30b2f1f 4908 RX_MATCH_UTF8_set(rx, do_utf8);
d9f424b2 4909
971a9dd3 4910#ifdef USE_ITHREADS
20e98b0f 4911 if (pm->op_pmreplrootu.op_pmtargetoff) {
159b6efe 4912 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
20e98b0f 4913 }
971a9dd3 4914#else
20e98b0f
NC
4915 if (pm->op_pmreplrootu.op_pmtargetgv) {
4916 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
971a9dd3 4917 }
20e98b0f 4918#endif
79072805 4919 else
7d49f689 4920 ary = NULL;
a0d0e21e
LW
4921 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4922 realarray = 1;
8ec5e241 4923 PUTBACK;
a0d0e21e
LW
4924 av_extend(ary,0);
4925 av_clear(ary);
8ec5e241 4926 SPAGAIN;
ad64d0ec 4927 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
8ec5e241 4928 PUSHMARK(SP);
ad64d0ec 4929 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
8ec5e241
NIS
4930 }
4931 else {
1c0b011c 4932 if (!AvREAL(ary)) {
1b6737cc 4933 I32 i;
1c0b011c 4934 AvREAL_on(ary);
abff13bb 4935 AvREIFY_off(ary);
1c0b011c 4936 for (i = AvFILLp(ary); i >= 0; i--)
3280af22 4937 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
1c0b011c
NIS
4938 }
4939 /* temporarily switch stacks */
8b7059b1 4940 SAVESWITCHSTACK(PL_curstack, ary);
8ec5e241 4941 make_mortal = 0;
1c0b011c 4942 }
79072805 4943 }
3280af22 4944 base = SP - PL_stack_base;
a0d0e21e 4945 orig = s;
07bc277f 4946 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
613f191e
TS
4947 if (do_utf8) {
4948 while (*s == ' ' || is_utf8_space((U8*)s))
4949 s += UTF8SKIP(s);
4950 }
07bc277f 4951 else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
bbce6d69 4952 while (isSPACE_LC(*s))
4953 s++;
4954 }
4955 else {
4956 while (isSPACE(*s))
4957 s++;
4958 }
a0d0e21e 4959 }
07bc277f 4960 if (RX_EXTFLAGS(rx) & PMf_MULTILINE) {
7fba1cd6 4961 multiline = 1;
c07a80fd 4962 }
4963
a0d0e21e
LW
4964 if (!limit)
4965 limit = maxiters + 2;
07bc277f 4966 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
a0d0e21e 4967 while (--limit) {
bbce6d69 4968 m = s;
8727f688
YO
4969 /* this one uses 'm' and is a negative test */
4970 if (do_utf8) {
613f191e
TS
4971 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
4972 const int t = UTF8SKIP(m);
4973 /* is_utf8_space returns FALSE for malform utf8 */
4974 if (strend - m < t)
4975 m = strend;
4976 else
4977 m += t;
4978 }
07bc277f 4979 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
8727f688
YO
4980 while (m < strend && !isSPACE_LC(*m))
4981 ++m;
4982 } else {
4983 while (m < strend && !isSPACE(*m))
4984 ++m;
4985 }
a0d0e21e
LW
4986 if (m >= strend)
4987 break;
bbce6d69 4988
c1a7495a
BB
4989 if (gimme_scalar) {
4990 iters++;
4991 if (m-s == 0)
4992 trailing_empty++;
4993 else
4994 trailing_empty = 0;
4995 } else {
4996 dstr = newSVpvn_flags(s, m-s,
4997 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
4998 XPUSHs(dstr);
4999 }
bbce6d69 5000
613f191e
TS
5001 /* skip the whitespace found last */
5002 if (do_utf8)
5003 s = m + UTF8SKIP(m);
5004 else
5005 s = m + 1;
5006
8727f688
YO
5007 /* this one uses 's' and is a positive test */
5008 if (do_utf8) {
613f191e 5009 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
8727f688 5010 s += UTF8SKIP(s);
07bc277f 5011 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
8727f688
YO
5012 while (s < strend && isSPACE_LC(*s))
5013 ++s;
5014 } else {
5015 while (s < strend && isSPACE(*s))
5016 ++s;
5017 }
79072805
LW
5018 }
5019 }
07bc277f 5020 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
a0d0e21e 5021 while (--limit) {
a6e20a40
AL
5022 for (m = s; m < strend && *m != '\n'; m++)
5023 ;
a0d0e21e
LW
5024 m++;
5025 if (m >= strend)
5026 break;
c1a7495a
BB
5027
5028 if (gimme_scalar) {
5029 iters++;
5030 if (m-s == 0)
5031 trailing_empty++;
5032 else
5033 trailing_empty = 0;
5034 } else {
5035 dstr = newSVpvn_flags(s, m-s,
5036 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5037 XPUSHs(dstr);
5038 }
a0d0e21e
LW
5039 s = m;
5040 }
5041 }
07bc277f 5042 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
640f820d
AB
5043 /*
5044 Pre-extend the stack, either the number of bytes or
5045 characters in the string or a limited amount, triggered by:
5046
5047 my ($x, $y) = split //, $str;
5048 or
5049 split //, $str, $i;
5050 */
c1a7495a
BB
5051 if (!gimme_scalar) {
5052 const U32 items = limit - 1;
5053 if (items < slen)
5054 EXTEND(SP, items);
5055 else
5056 EXTEND(SP, slen);
5057 }
640f820d 5058
e9515b0f
AB
5059 if (do_utf8) {
5060 while (--limit) {
5061 /* keep track of how many bytes we skip over */
5062 m = s;
640f820d 5063 s += UTF8SKIP(s);
c1a7495a
BB
5064 if (gimme_scalar) {
5065 iters++;
5066 if (s-m == 0)
5067 trailing_empty++;
5068 else
5069 trailing_empty = 0;
5070 } else {
5071 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
640f820d 5072
c1a7495a
BB
5073 PUSHs(dstr);
5074 }
640f820d 5075
e9515b0f
AB
5076 if (s >= strend)
5077 break;
5078 }
5079 } else {
5080 while (--limit) {
c1a7495a
BB
5081 if (gimme_scalar) {
5082 iters++;
5083 } else {
5084 dstr = newSVpvn(s, 1);
e9515b0f 5085
e9515b0f 5086
c1a7495a
BB
5087 if (make_mortal)
5088 sv_2mortal(dstr);
640f820d 5089
c1a7495a
BB
5090 PUSHs(dstr);
5091 }
5092
5093 s++;
e9515b0f
AB
5094
5095 if (s >= strend)
5096 break;
5097 }
640f820d
AB
5098 }
5099 }
3c8556c3 5100 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
07bc277f
NC
5101 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5102 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5103 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5104 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
f9f4320a 5105 SV * const csv = CALLREG_INTUIT_STRING(rx);
cf93c79d 5106
07bc277f 5107 len = RX_MINLENRET(rx);
3c8556c3 5108 if (len == 1 && !RX_UTF8(rx) && !tail) {
1b6737cc 5109 const char c = *SvPV_nolen_const(csv);
a0d0e21e 5110 while (--limit) {
a6e20a40
AL
5111 for (m = s; m < strend && *m != c; m++)
5112 ;
a0d0e21e
LW
5113 if (m >= strend)
5114 break;
c1a7495a
BB
5115 if (gimme_scalar) {
5116 iters++;
5117 if (m-s == 0)
5118 trailing_empty++;
5119 else
5120 trailing_empty = 0;
5121 } else {
5122 dstr = newSVpvn_flags(s, m-s,
5123 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5124 XPUSHs(dstr);
5125 }
93f04dac
JH
5126 /* The rx->minlen is in characters but we want to step
5127 * s ahead by bytes. */
1aa99e6b
IH
5128 if (do_utf8)
5129 s = (char*)utf8_hop((U8*)m, len);
5130 else
5131 s = m + len; /* Fake \n at the end */
a0d0e21e
LW
5132 }
5133 }
5134 else {
a0d0e21e 5135 while (s < strend && --limit &&
f722798b 5136 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
7fba1cd6 5137 csv, multiline ? FBMrf_MULTILINE : 0)) )
a0d0e21e 5138 {
c1a7495a
BB
5139 if (gimme_scalar) {
5140 iters++;
5141 if (m-s == 0)
5142 trailing_empty++;
5143 else
5144 trailing_empty = 0;
5145 } else {
5146 dstr = newSVpvn_flags(s, m-s,
5147 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5148 XPUSHs(dstr);
5149 }
93f04dac
JH
5150 /* The rx->minlen is in characters but we want to step
5151 * s ahead by bytes. */
1aa99e6b
IH
5152 if (do_utf8)
5153 s = (char*)utf8_hop((U8*)m, len);
5154 else
5155 s = m + len; /* Fake \n at the end */
a0d0e21e 5156 }
463ee0b2 5157 }
463ee0b2 5158 }
a0d0e21e 5159 else {
07bc277f 5160 maxiters += slen * RX_NPARENS(rx);
080c2dec 5161 while (s < strend && --limit)
bbce6d69 5162 {
1b6737cc 5163 I32 rex_return;
080c2dec 5164 PUTBACK;
f9f4320a 5165 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
727b7506 5166 sv, NULL, 0);
080c2dec 5167 SPAGAIN;
1b6737cc 5168 if (rex_return == 0)
080c2dec 5169 break;
d9f97599 5170 TAINT_IF(RX_MATCH_TAINTED(rx));
07bc277f 5171 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
a0d0e21e
LW
5172 m = s;
5173 s = orig;
07bc277f 5174 orig = RX_SUBBEG(rx);
a0d0e21e
LW
5175 s = orig + (m - s);
5176 strend = s + (strend - m);
5177 }
07bc277f 5178 m = RX_OFFS(rx)[0].start + orig;
c1a7495a
BB
5179
5180 if (gimme_scalar) {
5181 iters++;
5182 if (m-s == 0)
5183 trailing_empty++;
5184 else
5185 trailing_empty = 0;
5186 } else {
5187 dstr = newSVpvn_flags(s, m-s,
5188 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5189 XPUSHs(dstr);
5190 }
07bc277f 5191 if (RX_NPARENS(rx)) {
1b6737cc 5192 I32 i;
07bc277f
NC
5193 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5194 s = RX_OFFS(rx)[i].start + orig;
5195 m = RX_OFFS(rx)[i].end + orig;
6de67870
JP
5196
5197 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5198 parens that didn't match -- they should be set to
5199 undef, not the empty string */
c1a7495a
BB
5200 if (gimme_scalar) {
5201 iters++;
5202 if (m-s == 0)
5203 trailing_empty++;
5204 else
5205 trailing_empty = 0;
5206 } else {
5207 if (m >= orig && s >= orig) {
5208 dstr = newSVpvn_flags(s, m-s,
5209 (do_utf8 ? SVf_UTF8 : 0)
5210 | make_mortal);
5211 }
5212 else
5213 dstr = &PL_sv_undef; /* undef, not "" */
5214 XPUSHs(dstr);
748a9306 5215 }
c1a7495a 5216
a0d0e21e
LW
5217 }
5218 }
07bc277f 5219 s = RX_OFFS(rx)[0].end + orig;
a0d0e21e 5220 }
79072805 5221 }
8ec5e241 5222
c1a7495a
BB
5223 if (!gimme_scalar) {
5224 iters = (SP - PL_stack_base) - base;
5225 }
a0d0e21e 5226 if (iters > maxiters)
cea2e8a9 5227 DIE(aTHX_ "Split loop");
8ec5e241 5228
a0d0e21e
LW
5229 /* keep field after final delim? */
5230 if (s < strend || (iters && origlimit)) {
c1a7495a
BB
5231 if (!gimme_scalar) {
5232 const STRLEN l = strend - s;
5233 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5234 XPUSHs(dstr);
5235 }
a0d0e21e 5236 iters++;
79072805 5237 }
a0d0e21e 5238 else if (!origlimit) {
c1a7495a
BB
5239 if (gimme_scalar) {
5240 iters -= trailing_empty;
5241 } else {
5242 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5243 if (TOPs && !make_mortal)
5244 sv_2mortal(TOPs);
5245 *SP-- = &PL_sv_undef;
5246 iters--;
5247 }
89900bd3 5248 }
a0d0e21e 5249 }
8ec5e241 5250
8b7059b1
DM
5251 PUTBACK;
5252 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5253 SPAGAIN;
a0d0e21e 5254 if (realarray) {
8ec5e241 5255 if (!mg) {
1c0b011c
NIS
5256 if (SvSMAGICAL(ary)) {
5257 PUTBACK;
ad64d0ec 5258 mg_set(MUTABLE_SV(ary));
1c0b011c
NIS
5259 SPAGAIN;
5260 }
5261 if (gimme == G_ARRAY) {
5262 EXTEND(SP, iters);
5263 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5264 SP += iters;
5265 RETURN;
5266 }
8ec5e241 5267 }
1c0b011c 5268 else {
fb73857a 5269 PUTBACK;
8ec5e241 5270 ENTER;
864dbfa3 5271 call_method("PUSH",G_SCALAR|G_DISCARD);
8ec5e241 5272 LEAVE;
fb73857a 5273 SPAGAIN;
8ec5e241 5274 if (gimme == G_ARRAY) {
1b6737cc 5275 I32 i;
8ec5e241
NIS
5276 /* EXTEND should not be needed - we just popped them */
5277 EXTEND(SP, iters);
5278 for (i=0; i < iters; i++) {
5279 SV **svp = av_fetch(ary, i, FALSE);
3280af22 5280 PUSHs((svp) ? *svp : &PL_sv_undef);
8ec5e241 5281 }
1c0b011c
NIS
5282 RETURN;
5283 }
a0d0e21e
LW
5284 }
5285 }
5286 else {
5287 if (gimme == G_ARRAY)
5288 RETURN;
5289 }
7f18b612
YST
5290
5291 GETTARGET;
5292 PUSHi(iters);
5293 RETURN;
79072805 5294}
85e6fe83 5295
c5917253
NC
5296PP(pp_once)
5297{
5298 dSP;
5299 SV *const sv = PAD_SVl(PL_op->op_targ);
5300
5301 if (SvPADSTALE(sv)) {
5302 /* First time. */
5303 SvPADSTALE_off(sv);
5304 RETURNOP(cLOGOP->op_other);
5305 }
5306 RETURNOP(cLOGOP->op_next);
5307}
5308
c0329465
MB
5309PP(pp_lock)
5310{
97aff369 5311 dVAR;
39644a26 5312 dSP;
c0329465 5313 dTOPss;
e55aaa0e 5314 SV *retsv = sv;
076a2a80 5315 assert(SvTYPE(retsv) != SVt_PVCV);
68795e93 5316 SvLOCK(sv);
076a2a80 5317 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV) {
e55aaa0e
MB
5318 retsv = refto(retsv);
5319 }
5320 SETs(retsv);
c0329465
MB
5321 RETURN;
5322}
a863c7d1 5323
65bca31a
NC
5324
5325PP(unimplemented_op)
5326{
97aff369 5327 dVAR;
65bca31a
NC
5328 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
5329 PL_op->op_type);
5330}
5331
867fa1e2
YO
5332PP(pp_boolkeys)
5333{
5334 dVAR;
5335 dSP;
5336 HV * const hv = (HV*)POPs;
5337
5338 if (SvRMAGICAL(hv)) {
5339 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5340 if (mg) {
5341 XPUSHs(magic_scalarpack(hv, mg));
5342 RETURN;
5343 }
5344 }
5345
5346 XPUSHs(boolSV(HvKEYS(hv) != 0));
5347 RETURN;
5348}
5349
e609e586
NC
5350/*
5351 * Local variables:
5352 * c-indentation-style: bsd
5353 * c-basic-offset: 4
5354 * indent-tabs-mode: t
5355 * End:
5356 *
37442d52
RGS
5357 * ex: set ts=8 sts=4 sw=4 noet:
5358 */