satya_rockstar has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to modify existing perl code and I would like to add an option to it. But there is a glitch here as there I have to go one elevel down. This levelling is conditional. The code :

use GetOpt::Long; my template ={ "global" => { "variables" =>{ "version" => "${s_version}", if($s_version eq "s8") "base" => "\${version}/$group", else{ "exe" => "\${exe_state}", "base" => "\${s_version/${exe}/$group" , } "variant" => "10", "dir" => "\${root}" } }}}

How do I break the if . I know I cant use a if in a template. How do I break it ? I think u can use hashing here. Biut, I don't know how to implement it. Can anybody give me a lead?

Replies are listed 'Best First'.
Re: Conditional statements for template
by graff (Chancellor) on Mar 20, 2014 at 03:22 UTC
    I don't understand what you're saying about templates, but just looking at your code snippet, it seems like a simple problem of assigning one thing or another to a part of hash structure, depending on some condition. That's not so hard - just move the conditional part outside the hash assignment:
    my $template = { global => { variables => { version => "$s_version", variant => "10", dir => "$root", } } }; if ( $s_version eq "s8" ) { $template{global}{base} = "$version/$group"; } else { $template{global}{exe} => "$exe_state", $template{global}{base} => "$s_version/$exe/$group" , }
    My example is different from your snippet in many little details - I was very confused by your use of back-slashes, dollar-signs and curly brackets inside of double-quotes, because it seemed strange and inconsistent. I simplified things, assuming that you probably have variables like "$group" and "$exe" defined somewhere else in your code, and you want those values used inside the quotes.

    Your use of curly brackets outside of quotes was also pretty confused.

    If you're not sure about the proper syntax to get what you want, please try to describe in better detail what you want, and maybe show us a bit more of your code, too. Or add just enough to the current snippet so that it should do something (like print something out).

Re: Conditional statements for template
by kcott (Archbishop) on Mar 20, 2014 at 04:38 UTC

    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.

    • "use GetOpt::Long;": not a module I'm familiar with — you probaly mean Getopt::Long; even assuming a simple typo, how does that module relate to any of the other code you posted?
    • "my template =": probably another typo (i.e. should be $template) but could be part of your (undisclosed) template syntax.
    • "if($s_version eq "s8") ...": even outside of a hashref definition, this is incorrect syntax.
    • ""\${version}/$group"": use of scalarrefs (here and in few other places) in this manner looks very wrong; however, you've shown insufficient code to determine what was intended.
    • ""\${s_version/${exe}/$group"": another syntax error (mismatched braces).
    • "} }}}": poor layout makes this difficult to read but it does look like one too many closing braces.

    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

      ""\${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

Re: Conditional statements for template
by choroba (Cardinal) on Mar 20, 2014 at 10:24 UTC
    You are missing a dollar sign before the variable name.

    You cannot use if in an expression, but you can use the Conditional Operator (aka the Ternary operator):

    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $group = 'g'; my $exe = 'e'; for my $s_version (qw(s8 s9)) { my $template = { global => { variables => { version => "${s_version}", ($s_version eq "s8") ? (base => "\${version}/$group" +) : ( exe => "\${exe_state}", base => "\${s_version/${exe} +/$group" ), variant => "10", dir => "\${root}", }, }, }; print Dumper $template; }

    Update: code formatting.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Conditional statements for template
by Anonymous Monk on Mar 20, 2014 at 02:38 UTC

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

    What template language is that?