sub extract_comments { my $filename = shift; local (*F, $/); open F, "< $filename" or warn("can't read $filename: $!"), return; @hello1=grep defined, =~ m{ ( \# .*? \n ) # extract a comment starting in # #and finishing in an enter | " (?: [^"\#]* | \#. )* " # skip over "..." | ' (?: [^'\#]* | \#. )* ' # skip over '...' | . [^\#"']* # skip over non-comments-or-quotes }xgs; $tam=@h...; print "Tam: $tam\n"; return @hello; } $file="ex1.pl"; @ola=extract_comments($file); foreach $line (@ola){ print"$line\n"; }