cr4nk
cr4nk

Reputation: 29

Combine 'if' and 'if not' in perl

I have got the following filter inside my httpd.conf:

ExtFilterDefine jsonfilter mode=output intype=application/json cmd="/usr/bin/perl -pe 's|^|qq(\,\") . valid . qq(\"\: ) . qq(\") . time() . \\x0D . qq(\") . qq(\\n)|e if ($==eof) &&  unless (-f q{/tmp/md5_filter.tmp})'"

But the way how I used the && operater is not valid. I receive no output if I request the file. The filter should only run if the md5_filter.tmp file doesn't exists and s command should only add the timestamp at the end of file (eof). Does somebody know what's wrong with my code?

Upvotes: 1

Views: 266

Answers (1)

TLP
TLP

Reputation: 67908

There are a lot of strange things about your code.

/usr/bin/perl -pe 's|^|qq(\,\") . valid . qq(\"\: ) . qq(\") . time() . \\x0D . qq(\") . qq(\\n)|e if ($==eof) &&  unless (-f q{/tmp/md5_filter.tmp})'
  • perl -pe does not actually change the file. Not sure if that is your intent. If you do want to change the file, you need to add the -i switch. Note that this will alter your file each time you run it.
  • s| You have changed the delimiter of the substitution operator from / to |. This is usually done when you need to use slash / inside the pattern, and you want to avoid having to escape it \/. You however do not have slashes in your pattern, which makes this change unnecessary.
  • ^ This denotes the start of a string. Your substitution will add from the beginning of the string. Since you want to add to the end of the file, I am not sure how you are thinking there.
  • qq(\,\") . valid . qq(\"\: ) . qq(\") . time() . \\x0D . qq(\") . qq(\\n) You seem to be under the impression that the concatenation operator . does something special. This statement can be written qq(,\"valid\": \") . time() . qq(\x0D\"\n) Probably. The quoting and escaping is somewhat tricky to figure out. (No need to escape , and :)
  • if ($==eof) is a weirdness beyond words. It means $= = eof, which assigns the return value of the eof() test to the predefined $= variable (The current page length). I feel pretty sure you didn't mean that, and only meant to check if (eof).
  • if ($==eof) && unless (-f q{/tmp/md5_filter.tmp}) -- This is actually two separate statements, and you cannot use post-fix if twice in a row. Nor do you need to. You would chain them into one, like this: if ( (eof) && (not -f q{...}) ).

What do we get if we combine all these corrections? Well, we get this:

/usr/bin/perl -pe' s/^/qq(,\"valid\": \") . time() . qq(\x0D\"\n)/e if ( (eof) && (not -f q{/tmp/md5_filter.tmp}) )'

But.... what are you doing with this statement? For each line in the file, you are checking if it is the end of the file. And if it is, you check for the existence of a file, and then add to the last line, from the start of the line, a new string.

I think what you want to do is wait until the last line of the file, and then print a line after it. You don't need a substitution to print, you just print. So we remove the substitution and replace it with a print. Since we want to print after the last line, we use an END block to execute code at the end.

/usr/bin/perl -pe 'END { unless (-f q{/tmp/md5_filter.tmp}) { print qq(\,\"valid\"\: \") . time() . qq(\x0D\"\n) }'

The printed string may need to start with a newline qq(\n..., if the last line of the file does not end with a newline. If, for some reason, you actually meant to write the string at the beginning of the line, things get more complicated. Then you're probably better off using the first command.

Upvotes: 1

Related Questions