puppet in not in code - puppet

I have this code...
if $hostname in $var_slave {
file {
"/var/spool/cron/mysql":
ensure => present,
owner => $mysqlUser,
group => $mysqlGroup,
mode => "0600",
source => 'puppet:///modules/eikonappdbcron/mysql-slave',
}
}
I want a way of checking if a value is not in a file.
I tried "if $hostname not in $var_master" but this doesn't work. After doing some research I believe I need to use an "!". I can't get the syntax correct.

Puppet does not have a single, combined "not in" operator. Instead, it has the in operator, and it has a general-purpose boolean negation operator (!). You can use these together to write a compound expression that evaluates the condition you want.
If you already realized that, then perhaps you ran into a problem with operator precedence. The negation operator has the higher precedence than does in. Indeed, ! has the highest precedence of any Puppet operator, so if its operand is intended to be a binary expression then you must enclose the operand in parentheses.
Thus, the negation of the boolean expression
$hostname in $var_master
is
! ($hostname in $var_master)

Related

What does '! -e "/etc/httpd"' in Perl in 'if' Condition

Please explain what following code snippet could do in Perl script,
if (! -e '/etc/httpd') {
print "Something";
}
I can't find exact scenario or condition that makes this condition 'true'. This question also not explain much regarding this.
Exactly, what -e switch does in this condition?
The -e filename is one of filetests ("-X"), which checks for existence of a "file" (an entry, not only a plain file).
So the condition tests whether /etc/httpd does not exist, since the unary operator ! does logical negation of what is on its right-hand side using Perl's rules for truth and falsehood.

Perl critic policy violation in checking index of substring in a string

for my $item (#array) {
if (index($item, '$n') != -1) {
print "HELLO\n";
}
}
Problem is: Perl critic gives below policy violation.
String may require interpolation at line 168, near '$item, '$n''. (Severity: 1)
Please advise how do I fix this?
In this case the analyzer either found a bug or is plain wrong in flagging your code.
Are you looking for a literal "$n" in $item, or for what $n variable evaluates to?
If you want to find the literal $n characters then there is nothing wrong with your code
If you expect $item to contain the value stored in $n variable then allow it to be evaluated,
if (index($item, $n) != -1)
If this is indeed the case but $n may also contain yet other escaped sequences or encodings which you need as literal characters (so to suppress their evaluation) then you may need to do a bit more, depending of what exactly may be in that variable.
In case you do need to find characters $ followed by n (what would explain a deliberate act of putting single quotes around a variable) you need to handle the warning.
For the particular policy that is violated see Perl::Critic::Policy::ValuesAndExpressions
This policy warns you if you use single-quotes or q// with a string that has unescaped metacharacters that may need interpolation.
To satisfy the policy you'd need to use double quotes and escape the $, for example qq(\$n). In my opinion this would change the fine original code segment into something strange to look at.
If you end up wanting to simply silence the warning see documentation, in Bending The Rules
A comment. The tool perlcritic is useful but you have to use it right. It's a static code analyzer and it doesn't know what your program is doing, so to say; it can catch bad practices but can't tell you how to write programs. Many of its "policies" are unsuitable for particular code.
The book that it is based on says all this very nicely in its introduction. Use sensibly.
When I look at the question where this comes from it appears that you are looking for index at which substrings were matched, so you need the content of $n variable, not literal "$n". Then perlcritic identified a bug in the code, good return for using it!

Why is this left-recursive and how do I fix it?

I'm learning ANTLR4 and I'm confused at one point. For a Java-like language, I'm trying to add rules for constructs like member chaining, something like that:
expr1.MethodCall(expr2).MethodCall(expr3);
I'm getting an error, saying that two of my rules are mutually left-recursive:
expression
: literal
| variableReference
| LPAREN expression RPAREN
| statementExpression
| memberAccess
;
memberAccess: expression DOT (methodCall | fieldReference);
I thought I understood why the above rule combination is considered left-recursive: because memberAccess is a candidate of expression and memberAccess starts with an expression.
However, my understanding broke down when I saw (by looking at the Java example) that if I just move the contents of memberAccess to expression, I got no errors from ANTLR4 (even though it still doesn't parse what I want, seems to fall into a loop):
expression
: literal
| variableReference
| LPAREN expression RPAREN
| statementExpression
| expression DOT (methodCall | fieldReference)
;
Why is the first example left-recursive but the second isn't?
And what do I have to do to actually parse the initial line?
The second is left-recursive but not mutually left recursive. ANTLR4 can eliminate left-recursive rules with an inbuilt algorithm. It cannot eliminate mutually left recursive rules. There probably exists an algorithm, but this would hardly preserve actions and semantic predicates.
For some reason, ANTLRWorks 2 was not responding when my grammar had left-recursion, causing me to (erroneously) believe that my grammar was wrong.
Compiling and testing from commandline revealed that the version with immediate left-recursion did, in fact, compile and parse correctly.
(I'm leaving this here in case anyone else is confused by the behavior of the IDE.)

Does the shorthand ternary operator make multiple calculations?

The ternary operator has a shorthand version, i.e.
var = exp ?: exp2
I know it works in PHP. Other languages may have also picked it up. C# has similar functionality (for the context of this question) - ??.
When the condition passes, is the expression evaluated again, or is the result stored somewhere?
http://msdn.microsoft.com/en-us/library/ms173224.aspx
The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.
It is stored, not computed twice.

How to compare alphanumeric values in Perl?

Here is my code-
print $cpu_arch."\n";
if ($cpu_arch eq'AMD64') {
print "Remote machine is 64bit\n";
}
Depending upon the CPU architecture, $cpu_arch returns x86 or AMD64.
The problem is that no matter what $cpu_arch returns, the if loop condition is not getting satisfied. I've tried chomp'ing the $cpu_arch but that doesn't seem to help either.
The operator for string is eq as you have. Your issue isn't in your operator it is in the data. More than likely you are buffering improperly ( change the $| variable ) or your $cpu_arch has trailing white-space. Or maybe you are looking for regex match and not literal match ( then you want /AMD64/ )
Your code is correct. Either $cpu_arch doesn't contain AMD64, or you are mistaken about the if condition being false (perhaps because of buffering).
The following might help you examine $cpu_arch.
use Data::Dumper;
{
local $Data::Dumper::Useqq = 1;
print(Dumper($cpu_arch));
}

Resources