|
C.DeRykus |
at Jul 25, 2010 at 9:58 am
|
⇧ |
| |
...
2010/7/24 Mike Martin <redt...@gmail.com>:
This is how I worked round the issue (didn't think of hash slice)
foreach my $w (qw/$file_start $file_time $video_track $audio_track
$quality $sync $sync_box $qual_box $vid_box $aud_box $time_label
$times $file_hbox/) #Use qw to turn variable name into a string
{
my $key= $w;
my $value=$w;# assign $w to value and key
$key=~s/^\$//; # remove $ sign from key
$value=eval('\\'.$value); # turn variable name into a reference
pointing to original variable
$gui_hash{$key}=$$value ; create hash with and dereferenced value as value
}
Your use of string eval is slow - especially in a loop -
since the string must compiled. Potentially dangerous
too if there's any user input involved. (You also store
a ref to $value unnecessarily since you just deref it
during the hash assignment later.)
Here's an example of the faster, safer hash slice that
was mentioned:
my @var_names = qw/ file_start, file_time ... /;
my @var_values = ( $file_start, $file_time, ... );
my %gui_hash;
@gui_hash{ @var_names } = @var_values;
Might as well just sync up names,values directly
in the hash declaration:
my %gui_hash = ( file_start => $file_start,
file_time => $file_time,
...
);
--
Charles DeRykus