Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Another newb question

by dasgar (Priest)
on Aug 08, 2012 at 06:01 UTC ( #986150=note: print w/replies, xml ) Need Help??

in reply to Another newb question

The line with the print statement is missing the semicolon at the end. That should fix the error.

Out of curiosity, why are you trying to from STDIN into an array? Unless I'm mistaken, you'll always end up with just one element in your array.

Replies are listed 'Best First'.
Re^2: Another newb question
by davido (Archbishop) on Aug 08, 2012 at 06:14 UTC

    Great advice on the semicolon. Perl doesn't always recognize when it's missing (if it could recognize reliably, it would be an unnecessary bit of syntax). Consequently, the error messages that show up (or errant behavior) when one is missing don't always seem to gybe with what the problem actually turns out to be.

    Unless I'm mistaken...


    perl -e 'my @array = <STDIN>; print "$_\n" for @array;'

    Even if the input were routed to STDIN by shell redirection it's still quite possible for the input record separator to be a part of the input stream.

    Those are always famous last words, aren't they? ;)


      Thanks for the correction. After reading your explanation, that makes sense.

      Learned something new.

      (Note to self: Be careful when posting when you're extremely sleepy and not thinking straight.)

        My personal experience: Be careful when posting when you think you're thinking straight because often you're not.

      But all the Perls I have in captivity recognize a syntax error for the line missing the semicolon, although they make no mention of the semicolon itself.

      >perl -le "use warnings ; use strict ; print \"This program will display ... when done.\n\" my @userin = <STDIN> ; printf \"%-20s\", @userin ; " syntax error at -e line 1, near ""This program will display ... when d +one.\n" my " Global symbol "@userin" requires explicit package name at -e line 1. Global symbol "@userin" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

      If my own experience is any guide, what happened is that Parmenides, momentarily unable to interpret the first (and critical) error message, was drawn to concentrate on the subsequent 'error' message unaware that it was entirely an artifact of the preceding error. A very common oversight (again, IME). The lesson: Pay strict attention to the point at which the compiler first stumbles and don't be distracted by the point at which it finally bites the dust.

        @everybody, Thanks for all the help!

        @#333 I don't think I missed an error but it's certainly possible

        Hopefully, this is my final question. When I print my array with printf each string should be printed in a right justifified 20 character column (i.e. the last letter should be indented 20 characters sort of like align left in word.) As per the book's request I created a ruler line to determine how far the last character was actually being indented and it is only being indented 19 characters. I can not figure out why. Any help would be appreciated. Thanks!

        #!/usr/bin/perl use warnings ; use strict ; print "This program will display what you type in right justified 20 c +haracter columns.\nEnter text and press ctrl + d when done.\n" ; my @userin = <STDIN> ; print "1234567890" x3 ; foreach (@userin) { printf "%20s", $_ ; }

        Well, I didn't actually run the code myself. Thought I'd share what I did in case it helps others.

        I just cut and pasted the OP's code into Padre. It marked the last two lines of code as having issues. When I moved my mouse over the next to last line, Padre showed the same error message that the OP listed. However, that line looked correct to me.

        Here's the key part. From past experience, I figured that if the line that is indicated as having issues by the Perl interpreter, then I needed to check the line(s) before it. In my code, the cause has usually been missing semi-colon or I had opened a closure (single quotes, double quotes, square bracket, etc.) and failed to close the closure. Those two kinds of mistakes usually has caused the Perl interpreter to complain about an incorrect line number in my code.

        Anyways, when I started checking the other lines of code, that's when I noticed the missing semi-colon.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2018-03-22 07:07 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (273 votes). Check out past polls.