| hennessy 2004-03-27, 11:58 pm |
| Hi,
I thought it'd be clever to have a script read itself to find
references to external tools (like ping, traceroute, etc) and then build a
tool hash to contain them.. but it's not working :p The script seems to
be able to read the document and parse the variables but when I attempt to
use those variables I get nada.. script below:
open (IN, $0) || die "$0: can't open [$0]!\n";
while (<IN> ) {
next if $_ !~ m#\$tools-\>\{(\S+)\}#;
next if m#\$tools-\>\{\$tool\}#;
next if m#\$tools-\>\{\$1\}#;
chomp;
s#\$tools->\{(\S+)\}#chomp($tools->{$1} = `which $1`)#eg;
}
close (IN);
foreach my $tool (sort keys %$tools) {
print STDOUT "toollist: [$tool]: [$tools->{$tool}]\n";
}
print STDOUT "foo: [$tools->{'traceroute'}]\n";
results;
toollist: ['traceroute']: [/usr/sbin/traceroute]
foo: []
So it actually goes and builds the $tools hashref, but when I specify
values outside of a foreach the values are undefined..
Why would this work within a foreach and not when called directly? Is it
a single-quote thing?
Cheers,
- Matt
--
"When in doubt, use brute force."
- Ken Thompson
|