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

*Please don't debug the entire program. I haven't gotten to play with it yet and I want to figure it out for myself. I asked a question about a chapter exercize in a book that I am teaching myself perl from, and somebody suggested that I declare my variables using my when I first used them, rather than at the top of my program like I would with c. I tried to take that advice but my program is giving me this error: "@userin" requires explicit package name at ./rulerline.plx line 6." As I am sure you know, this is the error that perl gives when use strict is on and you haven't declared your varriabled. I put a my before the array. What am I doing wrong?
#!/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> ; printf "%-20s", @userin ;

Replies are listed 'Best First'.
Re: Another newb question
by dasgar (Priest) on Aug 08, 2012 at 06:01 UTC

    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.

      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...

      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? ;)


      Dave

        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.)

        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.