Reputation: 459
I'm new to Perl and someone suggested that I use this snippet for a Perl script that I was trying to write. My main problem in the script was that I used <STDIN>
twice and the first time I used it as an assignment to an array in the manner of @array =<STDIN>
. It turns out that I can't use in another variable after doing this. I was told to use this to get the "names" I needed for my array, and that doing this would allow me to use again. I would very much like it if someone could explain to me a few things, and the what the code does line-by line.
The code snippet:
my @names;
while (my $name = <STDIN>) {
chomp($name);
last unless $name;
push @names, $name; }
My Questions
Why does assigning to an array like @array = stop me from being able to use another after CTRL+D-ing out of the array?
Line 2 - aren't while loops generally used with some condition like (X > 7) to evaluate when to terminate?
Line 3 - why would I need to chomp every line entered into ?
Line 4 - I really have no idea what is going on in this line. Doesn't the unless operator need to evaluate the evaluate the truth of a following statement and have a block of code in {} following it that it executes? What is this line really doing? What is unless evaluating here?
Upvotes: 1
Views: 3716
Reputation: 69244
Why does assigning to an array like @array = stop me from being able to use another after CTRL+D-ing out of the array?
Every file handle has a current file pointer associated with it. This pointer indicates where the next file read will start from. The pointer starts at the beginning of the file and is moved each time the file handle is read. When you read all of the file into an array as you've done, the pointer is moved to the end of the file. So the next read will return no data.
You can move the file pointer to a different position using the seek function.
Line 2 - aren't while loops generally used with some condition like (X > 7) to evaluate when to terminate?
Ah, but this while
look does have a condition. The condition is my $name = <STDIN>
. This returns a true value while there is more data to read from the file. When you get to the end of the file, the file input operator (< ... >
) returns a false value and the loop terminates.
Line 3 - why would I need to chomp every line entered into ?
You don't need to. It depends what you are doing. Each line you read will come with a newline on the end if it. The chomp
removes that newline. If you don't care about the newline then you don't need to remove it.
It's also possible to remove all of the newlines in one go once you have put all of the lines in the array. You can just call chomp()
like this:
chomp(@names);
Line 4 - I really have no idea what is going on in this line.
last unless $name;
is exactly the same as:
unless ($name) {
last;
}
Which is perhaps what you're more used to seeing. But Perl allows you to move simple conditions like this to the end of the line. It can often make code more concise and easier to read.
Upvotes: 1
Reputation: 6578
Your program will do the following:
my @names;
Declare an array called names
while (my $name = <STDIN>) {
read in multiple lines from the command line. See here
chomp($name);
chomp removes any trailing newline from command line input. See here
last unless $name;
This line ensures that the program stays within the while loop unless a blank line is entered. See here
push @names, $name;
For each line entered, add to the top of an array called names. See here
}
Upvotes: 1
Reputation: 790
So what your code does line by line:
my @names;
creates an array variable called names
while(my $name = <STDIN>) {
Start a while loop and assign $name variable input from standard input
chomp($name);
this removes newline characters (\n) from the input aka the ENTER / Return key
last unless $name;
is saying "break" unless $name is not null essentially. last
is the equivalent of break in perl
push @names, $name;
is appending the $name given in standard input onto your array of @names
Perl does not require typing when it comes to creating a variable like other programming languages do. What I mean by this is if I want to assign my variable to be a number I can just my simply saying:
my $line = 5;
I can also say $line is a string:
my $line = "Hello";
Upvotes: 0