Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Problem with <STDIN>

by sivaramanm (Acolyte)
on Jun 28, 2006 at 07:08 UTC ( [id://557948]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have a problem in <STDIN>. When I use it inside a foreach loop, it doesn't terminate in command prompt for the first iteration.

Thanks

foreach (grep(/^.*\.xml$/, readdir(DIR))) { open (FIN, "<$_") or die ("$_ is not opened\n"); undef $/; print "Pls enter Section level value for $_ : "; chop ($seclev = <STDIN>) }

Replies are listed 'Best First'.
Re: Problem with <STDIN>
by borisz (Canon) on Jun 28, 2006 at 07:27 UTC
    The problem is about undef $/; delay it until you want to slurp the file
    foreach ( grep( /^.*\.xml$/, readdir(DIR) ) ) { print "Pls enter Section level value for $_ : "; chop( $seclev = <STDIN> ); { local $/; open( FIN, "<$_" ) or die("$_ is not opened\n"); .. } }
    Boris
Re: Problem with <STDIN>
by shmem (Chancellor) on Jun 28, 2006 at 07:27 UTC
    It can't. It doesn't know how to. You did undef $/, so it reads from STDIN forever ;-)
    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Problem with <STDIN>
by strat (Canon) on Jun 28, 2006 at 09:18 UTC

    Better don't use chop for removing newlines; chop just removes the last char which is ok for unix and linux but not for windows, because there the newline has two chars: CR + LF. Better use chomp which is aware of the different types of line endings

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      The important distinction with chomp is that it removes $/ from the end of each element in the list passed to it (nothing happens if an element is not terminated with $/).

      Chop removes the last character without regard to what the character is - chop always alters a non-empty element.

      Note in particular that line end differences between file systems have been papered over by this stage and the default value for $/ is \n (which may or may not be a line feed character; you don't need to know). Neither chop nor chomp know or care about file system line end differences.


      DWIM is Perl's answer to Gödel

      No, that's completely wrong. chomp removes what's in $/ from the end of its arguments. $/ is equal to \n (by default) in Windows, so chomp only removes \n (by default) on Windows too.

      Perl translates CR+LF to LF for you when you read from a file in Windows (unless you use binmode on that handle). Similarly, Perl translates LF to CR+LF when writting to a file handle in Windows (unless you use binmode on that handle).

      The reason to use chomp is to handle the case where a line without the line terminator is read in. chop would fail to perform as desired there, but chomp wouldn't. That has nothing to do with CR+LF vs LF.

      Update: Added underlined text to clarify my intended message, at GrandFather's recommendation.

        It's not that chop fails, as it removes the last character regardless of what that character is. You could interpret that to mean that chop "fails to DWIM", but it hasn't failed, just not met the programmer's expectations.


        DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-09-19 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (25 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.