Please help me understand a passage from Perl Programming

I read Perl Programming by Tom Christiansen, brian d foy, Larry Wall, Jon Orwant. There is the following text that I did not understand (the exact places that I do not understand are in bold):

What do you really want to know, which operators provide which context to their operands. As it happens, you can easily tell which ones are in the context of the supply list, because they all have a LIST in their description syntax . Everything else provides a scalar context. In general, this is pretty intuitive. If necessary, you can force the scalar context to an argument in the middle of the LIST using scalar pseudofunction. Perl makes it impossible to force a context context to be displayed in a context, because wherever you need a list context , it is already provided using a LIST to some control function.

For convenience, I would like to formulate the following questions:

  • What does LIST mean in a fragment?

  • What is a syntax description ? (looks like something like documentation)

  • What does the following text mean:

you can force the scalar context to an argument in the middle of the list

+4
source share
1 answer

It is quite simple, as the text says. Take a look at perldoc -f print , for example:

 print FILEHANDLE LIST print FILEHANDLE print LIST 

As they say right there, print accepts LIST arguments, which means anything published after print in a list context. This is the same for any function where the argument is denoted as LIST.

With the scalar function scalar you can override this list context so that your argument is not evaluated in the list context. For example, an instruction to read a file descriptor line, for example:

 my $line = <$fh>; 

It is calculated in a scalar context because $line is a scalar. This means that only one line is read and placed in a variable. However, if you were to do:

 print <$fh>; 

The read line is in the context of the list, which means that all remaining lines in the file will be read. You can override this by setting the readline operator in a scalar context:

 print scalar <$fh>; 

And then you just read one line. To be more precise, you can provide a scalar context in the middle of the list:

 print @list, scalar <$fh>, @list2; 

Which, apparently, is what this quote says.

+10
source

All Articles