in reply to Evaling Strings in Subs for Hashref Variable Names to Values
If you use next, there is no need to use else: the code after next can only be reached if the condition was not true.
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 |
In Section
Seekers of Perl Wisdom