Ask a Question related to PERL Beginners, Design and Development.
-
Benjamin Walkenhorst #1
Check names of variables
Hello everybody,
I've written a perl-module that stores some settings in a DBM-hash.
Currently, the module checks if certain keys have defined values in the
hash and if so, it copies the values belonging to those keys to the
corresponding variables.
These variables are filled with default values before, so they work even
if no such value is given in the hash.
What I would like to do now: Iterate over the keys in the DBM-hash and
look if a local variable of the same name exists; if so, copy the value
from the hash to the corresponding local variable.
Is such a thing possible? And if so, how should I do it? Could I create
a command dynamically and run it via eval?
Kind regards,
Benjamin
Benjamin Walkenhorst Guest
-
#39150 [NEW]: PHP allows invalid names for constants and variables
From: z_rules55 at hotmail dot com Operating system: WinXP PHP version: 5.1.6 PHP Bug Type: Variables related Bug... -
#39150 [Opn->Bgs]: PHP allows invalid names for constants and variables
ID: 39150 Updated by: tony2001@php.net Reported By: z_rules55 at hotmail dot com -Status: Open +Status: ... -
session names and variables
Hi, I am trying to attach a variable to a session name Is it possible to accomplish something to the effect of the following? <cfloop... -
form button names as variables
perhaps I am just a little tired, but I am having trouble with my form buttons. Firstly I name them like this name=\"round".$counter."heats\"... -
Help with $_POST variables. Reading their names.
I have a dynamic form that that creates input variables named different things based on what it reads from the database. So, for example, it may... -
Jan Eden #2
Re: Check names of variables
Benjamin Walkenhorst wrote:
My initial idea is this (assuming you're talking about identity between local variable name and hash key name):>Hello everybody,
>
>I've written a perl-module that stores some settings in a DBM-hash.
>Currently, the module checks if certain keys have defined values in the
>hash and if so, it copies the values belonging to those keys to the
>corresponding variables.
>These variables are filled with default values before, so they work even
>if no such value is given in the hash.
>
>What I would like to do now: Iterate over the keys in the DBM-hash and
>look if a local variable of the same name exists; if so, copy the value
>from the hash to the corresponding local variable.
>
for (sort keys %dbm_hash)
if (defined $$_) {
$$_ = $dbm_hash{$_};
}
Since there is no hard reference in $_, the dereferencing should be treatedas a symbolic reference.
- Jan
--
There are 10 kinds of people: those who understand binary, and those who don't
Jan Eden Guest
-
Wiggins D Anconia #3
Re: Check names of variables
>
between local variable name and hash key name):> Benjamin Walkenhorst wrote:
>> My initial idea is this (assuming you're talking about identity> >Hello everybody,
> >
> >I've written a perl-module that stores some settings in a DBM-hash.
> >Currently, the module checks if certain keys have defined values in the
> >hash and if so, it copies the values belonging to those keys to the
> >corresponding variables.
> >These variables are filled with default values before, so they work even
> >if no such value is given in the hash.
> >
> >What I would like to do now: Iterate over the keys in the DBM-hash and
> >look if a local variable of the same name exists; if so, copy the value
> >from the hash to the corresponding local variable.
> >treated as a symbolic reference.>
> for (sort keys %dbm_hash)
> if (defined $$_) {
> $$_ = $dbm_hash{$_};
> }
>
> Since there is no hard reference in $_, the dereferencing should beBut lets not go there first. What is with all of the copying in and out>
of hashes? What is the point? Generally you shouldn't need to do this
type of thing, it usually indicates a problem elsewhere. A hash is
usually a more useful data structure so pulling single values out and
munging a bunch of scalar variables *usually* isn't the way you want to
go about it.... so what are you really doing? Why don't you want to
work with the values while they are in the %dbm_hash?
[url]http://danconia.org[/url]
Wiggins D Anconia Guest
-
Benjamin Walkenhorst #4
Re: Check names of variables
Hello,
On Wed, 11 Feb 2004 07:20:45 -0700
"Wiggins d Anconia" <wiggins@danconia.org> wrote:
I know, I am going to switch over into that direction. I justed wanted> But lets not go there first. What is with all of the copying in and
> out of hashes? What is the point? Generally you shouldn't need to do
> this type of thing, it usually indicates a problem elsewhere. A hash
> is usually a more useful data structure so pulling single values out
> and munging a bunch of scalar variables *usually* isn't the way you
> want to go about it.... so what are you really doing? Why don't you
> want to work with the values while they are in the %dbm_hash?
to make sure the program works at all, while being able to test if the
DBM is read correctly...
Thus I had local variable filled with default values (so they wouldn't
overwrite the values in the DBM-hash) and then override them with the
values in the DBM-hash, if there were any. This way, if there was no
value given in the DBM-hash for a certain setting, the program would not
end up with invalid or empty values.
Since it works now, I can get rid of the local variables.
But still, I need two hashes, one for the default values, one for the
overrides.
The program allows you to change settings at run-time as well as change
values in the dbm-hash - I don't want local-settings to go into the
dbm-hash unless I explicitly choose to.
Thanks,
kind regards,
Benjamin
Benjamin Walkenhorst Guest
-
John W. Krahn #5
Re: Check names of variables
Benjamin Walkenhorst wrote:
Hello,>
> Hello everybody,
> I've written a perl-module that stores some settings in a DBM-hash.
> Currently, the module checks if certain keys have defined values in the
> hash and if so, it copies the values belonging to those keys to the
> corresponding variables.
> These variables are filled with default values before, so they work even
> if no such value is given in the hash.
>
> What I would like to do now: Iterate over the keys in the DBM-hash and
> look if a local variable of the same name exists; if so, copy the value
> from the hash to the corresponding local variable.
>
> Is such a thing possible? And if so, how should I do it? Could I create
> a command dynamically and run it via eval?
Instead of using "local variables" store your defaults in a hash, for
example:
my %defaults = (
a => 1,
b => 2,
c => 3,
d => 4,
);
use GDBM_File;
tie %hash, 'GDBM_File', $filename, &GDBM_WRCREAT, 0640;
@defaults{ keys %hash } = values %hash;
untie %hash;
John
--
use Perl;
program
fulfillment
John W. Krahn Guest



Reply With Quote

