With
perl -le '...' filename you get the open/close dance for free. In a more complex script, you probably don't want that. If you have the script set up to read the file in question then you can skip the open/close, and simply do
my @f = <> like in your original one-liner.
If you already have the file's contents in a variable $string, then you can count the newlines in there with a simple
$lines = () = $string =~ m/\n/g -- this is a cryptic shorthand for a rather complex series of commands which would again take multiple lines in longhand, unobfuscated form.