This line I have in my code cuts the .csh from a string and returns the rest of it. Can someone explain what each part of it does?
($eachJOBID = $eachScriptNoPath) =~ s/\.csh// ;
This is about Perl syntax. The parts are:
($eachJOBID = $eachScriptNoPath)
The value in the scalar variable $eachScriptNoPath
is copied into the scalar variable $eachJOBID
. The statement is enclosed in parentheses to group the assignment operation and control the precedence between the assignment and the regular expression match in the next part.
=~ s/\.csh// ;
As the value is copied to $eachJOBID
, the value is evaluated against the regular expression match and operation. This expression matches any part of the value that is the string '.csh', and substitutes nothing for that string. I.e., deletes the string.
So the value that ends up in $eachJOBID
will not have '.csh'. I.e., the job id will be the script filename with '.csh' removed.
If the assignment statement on the left had not been enclosed in parentheses, Perl would likely have performed $eachScriptNoPath =~ s/\.csh//
first because of operator precedence, and the count of times the regular expression matched would have been put into $eachJOBID
instead of the intended job id string.
s/\.csh//
. The backslash was hidden as the OP hadn't used code blocks. It can be seen in the subject even before my edit.
Commented
Jun 13 at 19:48
s/regex/replacement/flags
operator returns the number of substitutions made (unless the r
flag is used), not really a boolean though it can be used as a boolean (perl like C doesn't have a boolean type, but scalars other than 0,"0",undef,"" are considered as "true")
Commented
Jun 13 at 19:54
=~
operator will return a boolean match/no-match to the left-hand side of the statement, but I will admit that my Perl is getting dusty from lack of use.
Commented
Jun 14 at 5:15
/pattern/
or $_ =~ m/pattern/
returns true (!!1) or false (!!0), but s/pattern/replacement/flags
or $_ =~ s/pattern/replacement/flags
returns the number of substitutions, though when that's 0, it doesn't return number 0, but a "false value" (Data::Dumper shows that as !!0
in recent versions of perl; I was wrong to say perl has no boolean)
Commented
Jun 14 at 5:37
.csh
. For instance, if you started with foo.csh.c.cshsh
, after the two .csh
s are removed, you end up with foo.csh
.
Commented
Jun 14 at 5:43