in reply to Evaling Strings in Subs for Hashref Variable Names to Values
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Evaling Strings in Subs for Hashref Variable Names to Values
by theleftsock (Beadle) on Nov 12, 2012 at 17:25 UTC | |
by CountZero (Bishop) on Nov 12, 2012 at 18:03 UTC | |
by theleftsock (Beadle) on Nov 13, 2012 at 14:38 UTC |