Exercise: parse perl file


Parse your perl files and print out the names of your variables. In the first version print out the scalar variables only. In the second version show all variables.

The user gives the name of the file on the command line.

Input file:


examples/regex-perl/code_to_parse.pl
#!/usr/bin/perl
use strict;
use warnings;

my $filename = shift or die "Usage: $0 filename\n";
open(my $fh, "<", $filename) or die "Could not open '$filename'\n";

my @snmps = <$fh>;
chomp @snmps;

print join "\n", @snmps;
print "\n------------------\n";
my @sorted_snmps = sort by_snmp_number @snmps;
print join "\n", @sorted_snmps;

sub by_snmp_number {
    return 0 if $a eq $b;
    my @a = split /\./, $a;
    my @b = split /\./, $b;
    foreach my $i (0..@a-1) { 
        return 1 if $i >= @b;
        next if $a[$i] == $b[$i];
        return $a[$i] <=> $b[$i];
    }
    return -1;
}

print "\n------------------\n";

Output:


$filename
$0
$fh
@snmps
@sorted_snmps
$a
$b
@a
@b
$i