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

Hi I does if code in the below format mean "if @array is the size of one"? Or does it mean "if $array[0] exists"?
my @array = <ARRAY>; if ($array[0]){
Also for the below code I get that the loop is supposed to last as long as you are iterating through FILE but I'm not sure what the bits with LINE mean. Does it just to move through lines in FILE?
LINE: while (<FILE>) { shift @array; unless ($array[0]){ last LINE; } next LINE; }

Replies are listed 'Best First'.
Re: array code interpretation
by choroba (Cardinal) on Apr 09, 2013 at 23:53 UTC
    The first if condition means "if the first element in array has a true value". To test whether the size of the array is at least one Perl uses
    if (@array) {
    and to test a particular size, you can use
    if ($size == scalar @array) {
    which can be shortened to
    if ($size == @array) {
    because the scalar context is imposed by the comparison operator.

    <FILE> is equivalent to readline FILE, see readline. The LINE: at the beginning of a line is a label, it names the loop. last LINE exits the loop, while next LINE runs the next iteration of the loop. See next and last.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: array code interpretation
by Rahul6990 (Beadle) on Apr 10, 2013 at 06:17 UTC
    The code pasted is doing the following steps:

    1. Create a block named 'LINE'.
    2. Read the content of 'FILE' line by line.
    3. Storing the current line in '@array'.
    4. Checking if array's first element is true.
    4.1. If so, then exiting the block.
    5. Reading the next line.(next LINE;)

    Here the 'next' part can be ignored as the while loop reads the file line by line, till it(FILE) has data. -------------------------------------------------------------

      I would quarrel with a couple of steps.

      3. Storing the current line in '@array'.

      The current line read from the file handle is completely ignored in the code given in the OP (which I admit is probably cut down from some real code). Instead, an element is 'popped' from the front of @array, i.e., its element 0 is removed (see shift), and this element is discarded.

      4. Checking if array's first element is true.
      4.1. If so, then exiting the block.

      The block is exited via last unless element 0 of @array is true, i.e., if it is false. The pitfall of interpreting such double-negativity is a good reason to avoid conditionals based on unless, unless, IMHO, they are dead simple and in the statement modifier form:
          last LINE unless some_condition();
      See  unless in Compound Statements and Statement Modifiers in perldocperlsyn.