For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > script reading itself for variables?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author script reading itself for variables?
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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com