Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

'while', 'foreach' not in perlfunc

by throop (Chaplain)
on Jun 13, 2008 at 20:29 UTC ( [id://692009]=perlquestion: print w/replies, xml ) Need Help??

throop has asked for the wisdom of the Perl Monks concerning the following question:

Brethren,

Trying to look online for exactly how while binds $_. Oddly enough, searching at perl.org in index-functions does not turn up documentation for while, nor for foreach. The other Perl iterative constructs, grep and map are there, though.

I realize that grep and map return values, where while and foreach don't. Still, seems like they should be documented in the same place. Or am I thinking about it wrong?

throop

Replies are listed 'Best First'.
Re: 'while', 'foreach' not in perlfunc
by pc88mxer (Vicar) on Jun 13, 2008 at 20:57 UTC
    Because they are considered statements and not functions, while and foreach are documented in perldoc perlsyn.

    while doesn't do any binding except when calling readline or glob (either by name or by using the <...> syntax):

    while (<STDIN>) { ... } while (readline(STDIN)) { ...} are equivalent to: while (1) { last unless defined($_ = <STDIN>); ... +} while (glob("*.c")) { ... } while (<*.c>) { ... } are equivalent to: my @list = glob("*.c"); for (@list) { ... };
Re: 'while', 'foreach' not in perlfunc
by dragonchild (Archbishop) on Jun 13, 2008 at 20:36 UTC
    while and foreach and syntax, not functions. grep and map are also, technically, syntax, but they behave like functions. while and foreach do not, so they aren't documented in the same place.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Yeah, though it would be nice if perldoc -f word worked for any perl keyword.

      Good Day,
          Dean

        perlbug still works, as does supplying patches to p5p.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: 'while', 'foreach' not in perlfunc
by psini (Deacon) on Jun 13, 2008 at 20:41 UTC

    I don't understand your question. while and foreach are documented in perlsyn as they are statement modifiers.

    Update: I of course meant compound statements

    Careful with that hash Eugene.

Re: 'while', 'foreach' not in perlfunc
by Anonymous Monk on Jun 14, 2008 at 08:51 UTC
Re: 'while', 'foreach' not in perlfunc
by blazar (Canon) on Jun 14, 2008 at 14:11 UTC
    Oddly enough, searching at perl.org in index-functions does not turn up documentation for while, nor for foreach. The other Perl iterative constructs, grep and map are there, though.

    I personally believe it's not odd at all: it is to be expected, since map and grep are not simply "iterative constructs" but actual functions, whereas while and for are flow control statements or statement modifiers. I believe that in Perl 6 they will become (more akin to) real functions, but that's not the case as of now.

    --
    If you can't understand the incipit, then please check the IPB Campaign.
      I believe that in Perl 6 they will become (more akin to) real functions, but that's not the case as of now.
      They will never be quite like ordinary functions, because they violate the "no two terms in a row":
      # while: while $cond { $statements } # ordinary funtion: my_while $cond, { $statements }; # allowed in ordinary functions: my_while $cond, &block; # not allowed in builtin while: while $cond &block;

      while is parsed like this in STD.pm:

      rule statement_control:while {\ <sym> [ <?before '(' [[my]? '$'\w+ '=']? '<' '$'?\w+ '>' ')'> #' <.panic: "This appears to be Perl 5 code"> ]? <EXPR> {*} #= exp +r <pblock> {*} #= blo +ck {*} }

      So it's still special-cased. (It would be even without the check for Perl 5 constructs)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://692009]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-24 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found