This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.0 alpha 6
[perl5.git] / forop
1 OP *
2 newFOROP(label,forline,scalar,expr,block,cont)
3 char *label;
4 line_t forline;
5 OP *scalar;
6 OP *expr
7 OP *block
8 OP *cont;
9 {
10     OP *newop;
11
12     copline = forline;
13
14     /*
15      * The following gobbledygook catches EXPRs that
16      * aren't explicit array refs and translates
17      *          foreach VAR (EXPR)
18      * into
19      *          @ary = EXPR;
20      *          foreach VAR (@ary)
21      * where @ary is a hidden array made by newGVgen().
22      * (Note that @ary may become a local array if
23      * it is determined that it might be called
24      * recursively.  See cmd_tosave().)
25      */
26     if (expr->op_type != OP_ARRAY) {
27         scrstab = gv_AVadd(newGVgen());
28         newop = append_elem(OP_LINESEQ,
29             newSTATEOP(label,
30               newBINOP(OP_ASSIGN,
31                 listref(newUNOP(OP_ARRAY,
32                   gv_to_op(A_STAB,scrstab))),
33                 forcelist(expr))),
34             loopscope(over(scalar,newSTATEOP(label,
35               newLOOPOP( 0,
36                 newUNOP(OP_ARRAY,
37                   gv_to_op(A_STAB,scrstab)),
38                 block,cont)))));
39         newop->cop_line = forline;
40         newop->cop_head->cop_line = forline;
41     }
42     else {
43         newop = loopscope(over(scalar,newSTATEOP(label,
44         newLOOPOP(1,expr,block,cont) )));
45     }
46     return newop;
47 }