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

Why doesn't <STDIN> initialize and fill $_ in the following control block?

until(<STDIN> =~ /exit|quit|(^\s*$)/i) { }

I ended up leaving until(<STDIN>) by it's lonesome, then assigning $scalar = $_, then matching it to the above regex. Logic would dictate that there would be a more elegant solution. Thanks in advance!

Replies are listed 'Best First'.
Re: $_ unitialized
by ikegami (Patriarch) on Aug 23, 2009 at 14:41 UTC

    Why doesn't <STDIN> initialize and fill $_ in the following control block?

    Because <STDIN> never changes $_.

    while (<$fh>) { ... }
    is short for
    while (defined($_ = <$fh>)) { ... }
    But since you don't have something of the form
    while (<$fh>) { ... }
    you never assign anything to $_.
    while (<STDIN>) { s/\s+$//; last if /^(?:exit|quit)?$/i; ... }
Re: $_ unitialized
by Anonymous Monk on Aug 23, 2009 at 00:43 UTC
    Because you are using =~
    until(!defined($_ = <STDIN>) and /exit|quit|(^\s*$)/i) { }
    or
    while(<STDIN>){ last if /exit|quit|(^\s*$)/i; }
      Did you mean "or" ( s/and/or ) in your first example?

      It's pretty hard for $_ to be both (!defined and anything).

      Also because you are using until block