test-eval02.pl to HTML.

index -|- end

Generated: Mon Aug 16 14:14:40 2010 from test-eval02.pl 2010/07/07 1 KB.

#!/usr/bin/perl -w
# 2010-07-07 - Example of setting a generated variable to a value using 'eval'
use strict;
use warnings;
my $dbg_v40 = 0;    # change this to 1
sub set_debug_item($$) {
    my ($value,$of) = @_;
    my $var = '$dbg_v'.$value# create desired variable name
    my $string = "$var = $of"; # create a program to set it
    eval $string;               # run the program
}
sub svc_set_dbg_item($$) {
    my ($val,$of) = @_;
    my $var = '$dbg_v'.$val;
    my $v = eval $var;
    if (defined $v) {
        my $stg = "$var"." = $of;";
        eval $stg;
        return 0;
    }
    return 1;
}

if ($dbg_v40) { print "Variable dbg_v40 is ON\n";
} else { print "Variable dbg_v40 is OFF\n"; }
set_debug_item(40,1); # set the variable
if ($dbg_v40) { print "Variable dbg_v40 is now ON\n";
} else { print "Variable dbg_v40 remains OFF\n"; }
if (svc_set_dbg_item(40,0)) { # set the variable
    print "Failed in function\n";
}
if ($dbg_v40) { print "Variable dbg_v40 is now ON\n";
} else { print "Variable dbg_v40 is now OFF\n"; }

index -|- top

checked by tidy  Valid HTML 4.01 Transitional