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

Hi Everyone I just installed strawberry, perl version 5.14.3. With this version, I have not been able to obtain input using '<>'. The script was working fine with an active perl version. Please let me know why it will not work with strawberry perl 5.14.3 install. Thank you in advance. I will write the script here: #!/usr/bin/perl -w use strict; my $input=''; my @raw=(); my $first=''; while(defined ($input=<>)){ chomp($input); @raw=$input; } print "@raw\n";

Replies are listed 'Best First'.
Re: Input files <>
by davido (Cardinal) on Apr 03, 2012 at 15:21 UTC

    That shebang line isn't doing anything for you (aside from enabling warnings). I doubt your installation of strawberry Perl's executable resides at /usr/bin/.

    Now for the issue: Each time through the loop you are assigning the value of $input to $array[0]. I assume the last line of the file is either blank or invisible (whitespace, or non-printing control characters), which is why you're seeing no output when you print @raw. You should change @raw=$input; to push @raw, $input;. Also, no need to pre-initialize $input to '', $first to '', and @raw to (). But the real issue is your assignment to @raw inside the loop; each iteration clobbers the previous value stored in @raw and assigns a new one to the first element.

    In fact, if all you're doing is printing all the lines of the file with newlines stripped and one newline at the end, you don't need to push the file into an array. Just go straight to print.

    while( <> ) { chomp; print; } print "\n";

    If the script worked for you under a previous version of Perl as posted, your build of that previous version of Perl was broken; demand you money back! :)


    Dave

Re: Input files <>
by VinsWorldcom (Prior) on Apr 03, 2012 at 15:25 UTC

    What does "will not work" mean? What output are you expecting?

    It "works" fine for me on Windows 7 64-bit / Strawberry 64-bit 5.12.3. "Works" as in does what I expect it to do - maybe not what you want?

    VinsWorldcom@C:\Users\VinsWorldcom\tmp> ver Microsoft Windows [Version 6.1.7601] VinsWorldcom@C:\Users\VinsWorldcom\tmp> perl -v This is perl 5, version 12, subversion 3 (v5.12.3) built for MSWin32-x +64-multi-t hread [...] VinsWorldcom@C:\Users\VinsWorldcom\tmp> test.pl help me understand what you want ^Z want
      Thank you VinsWorld, I expected to see the contents of @raw. Which sould be two sentences. The file.txt that was the source input contained two sentences. On the command line I type in the pl script blank space and file.txt. I hit return.The curser moves to next line on command screen and stays there forever. ostra
        As far as I can deduce from your program (which isn't inside of <code> tags, so very hard to read), @raw will be assigned the current line. @raw will never contain more than one line. Finally, when all input is seen, @raw is printed. And hence, will print the final line of the input.

        Can you please show us the exact command you are using to invoke the script?


        Dave