Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: while(<>) { ... } considered harmful

by converter (Priest)
on Sep 08, 2002 at 17:00 UTC ( #196050=note: print w/replies, xml ) Need Help??

in reply to while(<>) { ... } considered harmful

The perlsub man page mentions localization of $_ in the "When to Still Use local()" section, and says:

In particular, it's important to "local"ize $_ in any routine that assigns to it. Look out for implicit assignments in "while" conditionals.

With all due respect, both you and the authors of the modules you mention are ignoring the sound advice offered in the Perl documentation and should consider making the appropriate changes to make your code "play well with others".

  • Comment on Re: while(<>) { ... } considered harmful

Replies are listed 'Best First'.
Re: Re: while(<>) { ... } considered harmful
by IlyaM (Parson) on Sep 08, 2002 at 19:48 UTC
    IMHO it just too easy to do such mistake. Most of Perl operators that automagically assign to $_ (for/foreach, map, grep) do localize it so it doesn't affect outer scope. On the other hand magic in while(<>) construct doesn't work same way while newbie to Perl can expect Perl to do it. Frankly I've never though about such side effect of this construct before and I think I can say that I'm seasoned Perl programmer.

    Anyway what I did wrong? I just have simple map construct (I've posted snipplet of real code in other reply to this thread) and that error was very confusing. Most of the time Perl does right thing with scopes and badly written external to my code module cannot affect my code so destructively. Usually I can treat them as black boxes.

    Now instead of just writting use Net::DNS in my modules I will have to patch them to have something like { local $_; require Net::DNS } (until Net::DNS is fixed).

    Ilya Martynov (

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://196050]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2018-06-18 23:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.