in reply to Can anyone tell me what this is doing?

In addition to what others have pointed out, another thing to notice about this code is that when the pattern doesn't match (i.e, the string "\nend\n" never occurs within $pages[$PG]), then $pages[$PG] will be set to undef whatever the previous value of $1 was. This may or may not be is probably not what you want. , but I point it out just in case it's not.

If you want to leave $pages[$PG] alone if the pattern doesn't match, use a substitution instead:

$pages[$PG] =~ s/(.*\nend\n).*/$1/s;

-- Mike

--
just,my${.02}