When i use the line :
if (m/^$END$/g) {
# ...
}
in my code, the compiler thinks that I am searching for a Static'END$' in my code,
whereas I want to search the string "$END$". How shall I go about it?
To match a literal $, just escape it with a backslash:
if (m/^\$END\$/) { ... }
Removed the /g that shouldn't be there.
if (/\A\Q $END$ /x) { ... }
perldoc perlreref:
\Q Disable pattern metacharacters until \E
Removed the /g that shouldn't be there.
Related
I'm tinkering with JobDSL and can't seem to find a way to run several powershell commands in one go. Example:
job('whatever'){
steps{
powershell("""$var = $env:mybuildvar
cmdlet2 $var""")
}
}
How do I achieve this? Thanks!
it seems that """ """ works for batch, but not for powershell.
Also, if I try to use $var with escaping or without JobDSL fails with
ERROR: (sandbox_CI_Dev, line 15) No signature of method:
javaposse.jobdsl.dsl.helpers.step.StepContext.powershell() is
applicable for argument types: (java.lang.String) values: [$var =
$ENV:mybuildvar]
The error is reproducable on jobdsl playground (http://job-dsl.herokuapp.com/), use following code (or anything similar to code above):
job('whatever') {
steps{
powershell("write-output $")
}
}
also powershell('write-output test; write-output test') doesn't work
The method name is powerShell, not powershell. See https://jenkinsci.github.io/job-dsl-plugin/#path/job-steps-powerShell.
And Groovy interpolates double quoted strings, see String interpolation. You need to use single quotes to avoid the interpolation if you want to use the dollar sign ($), e.g. '$var'. Use triple single quotes for multiline strings.
job('whatever'){
steps{
powerShell('''$var = $env:mybuildvar
cmdlet2 $var''')
}
}
I have a file like this:
function a() {
doSomething();
doSomethingElse();
}
Now I need to replace all text between function a() { and } with some other text
I tried several ways found here, but they all failed. I hope I could get the explanation along with an answer.
P.S. the trick need to be compatible with both OS X and GNU Sed.
You can do a standard substitution :
sed -i 's/\(\<\)doSomething()/\1somethingElse()/' your_file
We use the word boundary to delimit your string :
\(\<\)doSomething()
And then we replace that string with your new string (including the captured word boundary) :
\1somethingElse()
The -i flag stands for : inline replacement
I used this sample file :
function a() {
doSomething();
doSomething();
foo();doSomething();
}
Output :
function a() {
somethingElse();
somethingElse();
foo();somethingElse();
}
sed '/^function a() {/,/}/{
/^[[:blank:]]*doSomething();/ c\
Replace with\
whaterver you want \
where new line are backslash \
at the end for multiline.
}' YourFile
replace the line of doSomething inside function a() paragraph by some other line(s)
Highly depend on content of function a() (assuming there is no ending } line inside the function)
In my book it uses something like this:
for($ARGV[0])
{
Expression && do { print "..."; last; };
...
}
Isn't the for-loop incomplete? Also, what's the point of the do, couldn't it just be { ... }, or does the do have some importance here?
There are two forms of for statement in Perl. The one you're seeing here is often written as foreach, but for and foreach are synonyms. It normally iterates over a list, setting $_ to each element. In this case, the "list" is a single value, so it has the effect of setting $_ to $ARGV[0] for the body of the loop.
The do is needed to make the block { ... } into an expression, so it can be an operand of the && operator. (See what happens if you omit the word do.)
(And you were missing a semicolon; I've edited the question to fix that.)
Lets assume I have the following Java code:
public String foo()
{
// returns foo()
String log = "foo() : Logging something!"
return log;
}
Can I search in Eclipse for foo() occurring only in a String literal, but not anywhere else in the code? So in the example here Eclipse should only find the third occurrance of foo(), not the first one, which is a function name and not the second one, which is a comment.
Edit: Simple Regular Expressions won't work, because they will find foo() in a line like
String temp = "literal" + foo() + "another literal"
But here foo() is a function name and not a String literal.
You can try it like this:
"[^"\n]*foo\\(\\)[^"\n]*"
You have to escape brackets, plus this regex do not match new lines or additional quotes, which prevent wrong matches.
Maybe you should use regex to find any occurence of foo() between two " ?
The following script gives me what I want but Perl also throws me a warning saying "Useless use of a variable in void context". What does it mean?
use strict;
use warnings;
my $example = 'http\u003a//main\u002egslb\u002eku6\u002ecom/c0/q7LmJPfV4DfXeTYf/1260269522170/93456c39545857a15244971e35fba83a/1279582254980/v632/6/28/a14UAJ0CeSyi3UTEvBUyMuBxg\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c1/q7LmJPfV4DfXeTYf/1260269522170/3cb143612a0050335c0d44077a869fc0/1279582254980/v642/10/20/7xo2MJ4tTtiiTOUjEpCJaByg\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c2/q7LmJPfV4DfXeTYf/1260269522170/799955b45c8c32c955564ff9bc3259ea/1279582254980/v652/32/4/6pzkCf4iqTSUVElUA5A3PpMAoA\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c3/q7LmJPfV4DfXeTYf/1260269522170/cebbb619dc61b3eabcdb839d4c2a4402/1279582254980/v567/36/19/MBcbnWwkSJu46UoYCabpvArA\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c4/q7LmJPfV4DfXeTYf/1260269522170/1365c39355424974dbbe4ae8950f0e73/1279582254980/v575/17/15/EDczAa0GTjuhppapCLFjtaQ\u002ef4v';
my #raw_url = $example =~ m{(http\\u003a.+?f4v)}g;
my #processed_url = map {
s{\\u003a}{:}g,$_;
s{\\u002e}{.}g,$_;
s{\\u002d}{#}g,$_;
} #raw_url;
print join("\n",#processed_url);
And why this map thing doesn't work if I omit those dollar underscores like so?
my #processed_url = map {
s{\\u003a}{:}g;
s{\\u002e}{.}g;
s{\\u002d}{#}g;
} #raw_url;
When I omit those dollar underscores, I get nothing except for a possibly success flag "1". What am I missing? Any ideas? Thanks like always :)
What you want is...
my #processed_url = map {
s{\\u003a}{:}g;
s{\\u002e}{.}g;
s{\\u002d}{#}g;
$_;
} #raw_url;
A map block returns the value composed of the last statement evaluated as its result. Thats why we pass the $_ as the last statement. The substitution operator s{}{} returns the number of substitutions made.
In your prior setup, you had by itself the following statement. Which is pretty much meaningless and that is what Perl is warning about.
s{\\u003a}{:}g, $_;
You already have the answer you were looking for, but I wanted to point out a subtlety about using the substitution operator inside a map block: your original array is also being modified. If you want to preserve the original array, one way to do it is to make a copy of the array, then modify only the copy:
my #processed_url = #raw_url;
for (#processed_url) {
s{\\u003a}{:}g;
s{\\u002e}{.}g;
s{\\u002d}{#}g;
}
Or, if you only need one array, and you want the original to be modified:
for (#raw_url) {
s{\\u003a}{:}g;
s{\\u002e}{.}g;
s{\\u002d}{#}g;
}