in reply to Conditional statements for template

G'day satya_rockstar,

Welcome to the monastery.

"How do I break the if . I know I cant use a if in a template. How do I break it ?"

I don't know what you mean by "break the if". Templates can have conditionals (as well as other programming constructs): for instance, take a look at Template::Toolkit's syntax. What templating system are you using?

Furthermore, the code you've posted has so many problems, it's difficult to know what you're actually trying to do.

I recommend you first read "perlintro - Perl introduction for beginners" and learn the basic syntax of the language.

Next, "perldsc - Perl data structures intro" will provide information on building, accessing and modifying a complex Perl data structure.

You also need to understand that we can't really help you if you leave out key information or post incomplete and syntactically incorrect code (such as the examples I've already highlighted). Please read "How do I post a question effectively?" for details on this.

Here's my best guess at the type of thing you're trying to achieve:

#!/usr/bin/env perl -l use strict; use warnings; use Data::Dump; my $template = { global => { variables => { variant => 10, dir => 'root', }, }, }; for (qw{s8 s9}) { print "*** Version: $_ ***"; $template->{global}{variables}{version} = $_; if ($template->{global}{variables}{version} eq 's8') { $template->{global}{variables}{base} = "$_/group"; } else { $template->{global}{variables}{base} = "$_/exe/group"; $template->{global}{variables}{exe} = 'exe_state'; } dd $template; }

Output:

*** Version: s8 *** { global => { variables => { base => "s8/group", dir => "root", variant => 10, v +ersion => "s8" }, }, } *** Version: s9 *** { global => { variables => { base => "s9/exe/group", dir => "root", exe => "exe_state", variant => 10, version => "s9", }, }, }

-- Ken

Replies are listed 'Best First'.
Re^2: Conditional statements for template
by AnomalousMonk (Archbishop) on Mar 20, 2014 at 17:12 UTC
    ""\${s_version/${exe}/$group"": another syntax error (mismatched braces).

    I quite agree that the actual or intended semantics of it all are very puzzling, but FWIW at least this expression is syntactically correct:

    c:\@Work\Perl\monks>perl -wMstrict -le "my ($exe, $group) = qw(foo bar); my $s = \"\${s_version/${exe}/$group\"; print qq{'$s'}; " '${s_version/foo/bar'

      Looking at it again, an escaped dollar sign could well be the intention (particularly in the context of a template).

      I suspect, making allowances for multiple typos, I assumed another (i.e. scalar vs. scalarref) when I got to those lines containing '\${'.

      However, if "\${version}/$group", "\${exe_state}" and "\${root}" are written as intended (i.e. generating "${version}/group_value", "${exe_state}" and "${root}" for subsequent evaluation via a template), then "\${s_version/${exe}/$group" is probably still missing a brace after 's_version'.

      Furthermore, I wonder why braces are used for ${exe} but not for $group: perhaps "\${s_version/${exe}/$group" is actually meant to be "\${s_version}\${exe}/$group" or "\${s_version}/\${exe}/$group" or <insert other guesses here>.

      Perhaps the OP will respond at some point and clarify the situation. :-)

      -- Ken

        Perhaps the OP will ... clarify the situation.

        We can only hope.