jamgill has asked for the wisdom of the Perl Monks concerning the following question:

If the content of one scalar variable is the name of another scalar variable, how can I use the first scalar variable to provide the name to the program?

Or, to say it another way:
if
$foo = meow;
$boo = foo;
then does $$boo = meow;?
or `${$boo}` or `${boo}`?

Here's a little more precise version of the question using the code I'm currently grappling with.

#!/usr/bin/perl -w use strict; ## usage information # $0 UPSNAME HOSTNAME DATAPOINT # where DATAPOINT can be any of # utility - the number VAC utility power is providing # loadpct - the percentage of the maximum load # battpct - percent of total battery charge available # runtime - approx. number of minutes of battery life my $lookfor = $ARGV[2]; my @upsinfo = (`/usr/local/bin/upsc $ARGV[0]\@$ARGV[1]`); my ($junk, $utility) = split /\s+/, $upsinfo[10]; my ($kunk, $loadpct) = split /\s+/, $upsinfo[14]; my ($lunk, $battpct) = split /\s+/, $upsinfo[21]; my ($munk, $runtime) = split /\s+/, $upsinfo[23]; if ($lookfor) { print $$lookfor; } else { print "utility is $utility\n"; print "loadpct is $loadpct\n"; print "battpct is $battpct\n"; print "runtime is $runtime\n"; };
Why do I get the error:
Can't use string ("loadpct") as a SCALAR ref while "strict refs" in use... line 20 ?

Now, Page 58 of the Camel book (v3) says that $bert and ${ some_expression() } are the same, where some_expression() 'returns a reference to variable $bert (or even the string, "bert")'

I know that references are a concept around which my grasp is not firm, perhaps I've got it all mistaken. Please someone show me how to understand this problem.

Apologies if this is covered somewhere but I have not found it, a link and an RTFM is a generous answer.

By the way, this $0 is what I'm cobbling together to use NUT the Network UPS Tools and MRTG to chart the usage and performance of my UPSes over time.

Replies are listed 'Best First'.
Re: scalar dereferencing
by snowcrash (Friar) on Sep 11, 2001 at 10:42 UTC

      thank you to both of you for guiding and enlightning replies.

      I have learned quite a bit through this, and have found the thrill of my first real hash.

      Here is how the code currently stands (as always, a work in progress):

      #!/usr/bin/perl -w use strict; ## usage information # $0 UPSNAME HOSTNAME DATAPOINT # where DATAPOINT can be any of # UTILITY - the number VAC utility power is providing # LOADPCT - the percentage of the maximum load # BATTPCT - percent of total battery charge available # RUNTIME - approx. number of minutes of battery life # note that DATAPOINTs *must* be specified in all caps my ($key, $val, %upsdata); foreach (`/usr/local/bin/upsc $ARGV[0]\@$ARGV[1]`) { ($key, $val) = /(\w+):\s(.*)(\n)?/; $upsdata{$key} = $val; }; if ($ARGV[2]) { print "$upsdata{$ARGV[2]}\n"; } else { print "utility is $upsdata{UTILITY}\n"; print "loadpct is $upsdata{LOADPCT}\n"; print "battpct is $upsdata{BATTPCT}\n"; print "runtime is $upsdata{RUNTIME}\n"; };
Re: scalar dereferencing
by busunsl (Vicar) on Sep 11, 2001 at 10:18 UTC
    Well, as the errormessage says: can't use ... while "strict refs" in use.

    You want to use symbolic references, which are not allowed when you use strict.

    You could add the line no strict refs; to your program, which would make it work.
    But the better solution would be to use a hash for things like that.

    Use Super Search with 'strict' and 'refs', it will give you loads of nodes on that topic.