dereference question
Randal L. Schwartz
merlyn at stonehenge.com
Sun Mar 9 10:56:27 CST 2003
~sdpm~
>>>>> "Joel" == Joel Fentin <joel at fentin.com> writes:
Joel> In my application, both line 1 and line 2 seem to do the same job,
Joel> thus
Joel> revealing the value contained within. But they won't work for $E or
Joel> $F. Is
Joel> there a line of code which will apply to all referenced scalars that
Joel> are
Joel> read in from the file?
>>
>> Hmm. Sounds like a FAQ to me. Did you check the FAQs first?
Joel> I tried everything I could think of. I read everything I could think of.
Joel> Which specific FAQ do you have in mind?
$ perldoc -tq variables
Found in /usr/libdata/perl5/pod/perlfaq4.pod
How can I expand variables in text strings?
Let's assume that you have a string like:
$text = 'this has a $foo in it and a $bar';
If those were both global variables, then this would suffice:
$text =~ s/\$(\w+)/${$1}/g; # no /e needed
But since they are probably lexicals, or at least, they could
be, you'd have to do this:
$text =~ s/(\$\w+)/$1/eeg;
die if $@; # needed /ee, not /e
It's probably better in the general case to treat those
variables as entries in some special hash. For example:
%user_defs = (
foo => 23,
bar => 19,
);
$text =~ s/\$(\w+)/$user_defs{$1}/g;
See also ``How do I expand function calls in a string?'' in this
section of the FAQ.
Found in /usr/libdata/perl5/pod/perlfaq7.pod
How can I catch accesses to undefined variables/functions/methods?
The AUTOLOAD method, discussed in "Autoloading" in perlsub and
"AUTOLOAD: Proxy Methods" in perltoot, lets you capture calls to
undefined functions and methods.
When it comes to undefined variables that would trigger a
warning under "-w", you can use a handler to trap the
pseudo-signal "__WARN__" like this:
$SIG{__WARN__} = sub {
for ( $_[0] ) { # voici un switch statement
/Use of uninitialized value/ && do {
# promote warning to a fatal
die $_;
};
# other warning cases to catch could go here;
warn $_;
}
};
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
~sdpm~
The posting address is: san-diego-pm-list at hfb.pm.org
List requests should be sent to: majordomo at hfb.pm.org
If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:
unsubscribe san-diego-pm-list
If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.
More information about the San-Diego-pm
mailing list