You do not want to use eval BLOCK to catch exceptions. You want to use eval STRING to evaluate a constructed code. You should include the assignment in the code, though:
Using "templates" might solve lots of problems this naïve approach might cause.#!/usr/bin/perl use warnings; use strict; my $sql_var_hr; $sql_var_hr->{col_name} = 'column_name'; my $sql = get_sql($sql_var_hr); sub get_sql { my ($vars) = @_; my $sql; while (my $iline = <DATA>) { next if $iline =~ /^#/; $sql .= $iline; } print "pre_eval: $sql\n"; eval '$sql = "' . $sql . '"' or warn $@; print "post_eval: $sql\n"; return $sql; } __DATA__ select $vars->{col_name} from schema.table select $$vars{col_name} from schema.table
In reply to Re: Evaling Strings in Subs for Hashref Variable Names to Values
by choroba
in thread Evaling Strings in Subs for Hashref Variable Names to Values
by theleftsock
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |