use strict; use warnings; my %scalars; # This is the long and wordy version of the line that follows this while loop. while ( my $line = <> ) { my @scalars_on_this_line = ( $line =~ /(\$\w+)/g ); foreach my $scalar ( @scalars_on_this_line ) { $scalars{ $scalar } = 1; } } # This is the faster (because of the hash slice )and smaller (undef values instead of lots of '1' values) method. I'd write it this way. @scalars{ /(\$\w+)/g } = () while <>; $\ = $, = "\n"; print sort keys %scalars;