I'm quite new to ESLint and I couldn't find a rule that does what I want. Consider the following code:
if(someCondition)i++;
I want to enforce a single space after the parentheses, so that we should have the following instead:
if(someCondition) i++;
However I simply couldn't find such a rule; it's not space-before-function-paren (obviously), keyword-spacing (which only affects the spacing after if) or space-before-blocks (since this is a single-line-if, there's no block). Please help. Thanks!
P.S. Same rule should apply to single-line-while as well.
https://github.com/eslint/eslint/discussions/15267#discussioncomment-1597748
I think we don't have that rule in the core.
keyword-spacing can disallow if (foo)return;, space-before-blocks can disallow if (foo){, but it seems that there is no rule that would disallow if (foo)bar; and enforce if (foo) bar;
Simple, but effective:
$ cat input
if (condition)i++;
if (condition)i--
while ( this ^ foo || bar )continue
for (true ||
true )z--
Naive solution, but probably ok.
$ cat input | busybox sed -e 's/)\([a-zA-Z_]\)/) \1/'
if (condition) i++;
if (condition) i--
while ( this ^ foo || bar ) continue
for (true ||
true ) z--
This will put a space between ) and the first letter of a variable name (a-z_).
Consider my answer here (and others') for tips on how to process all files that contain the undesired pattern:
https://unix.stackexchange.com/questions/675881/remove-trailing-spaces-from-text-files-only-when-necessary/676367#676367
Related
For personal development and projects I work on, we use four spaces instead of tabs.
However, I need to use a heredoc, and I can't do so without breaking the indention flow.
The only working way to do this I can think of would be this:
usage() {
cat << ' EOF' | sed -e 's/^ //';
Hello, this is a cool program.
This should get unindented.
This code should stay indented:
something() {
echo It works, yo!;
}
That's all.
EOF
}
Is there a better way to do this?
Let me know if this belongs on the Unix/Linux Stack Exchange instead.
(If you are using bash 4, scroll to the end for what I think is the best combination of pure shell and readability.)
For heredocs, using tabs is not a matter of preference or style; it's how the language is defined.
usage () {
⟶# Lines between EOF are each indented with the same number of tabs
⟶# Spaces can follow the tabs for in-document indentation
⟶cat <<-EOF
⟶⟶Hello, this is a cool program.
⟶⟶This should get unindented.
⟶⟶This code should stay indented:
⟶⟶ something() {
⟶⟶ echo It works, yo!;
⟶⟶ }
⟶⟶That's all.
⟶EOF
}
Another option is to avoid a here document altogether, at the cost of having to use more quotes and line continuations:
usage () {
printf '%s\n' \
"Hello, this is a cool program." \
"This should get unindented." \
"This code should stay indented:" \
" something() {" \
" echo It works, yo!" \
" }" \
"That's all."
}
If you are willing to forego POSIX compatibility, you can use an array to avoid the explicit line continuations:
usage () {
message=(
"Hello, this is a cool program."
"This should get unindented."
"This code should stay indented:"
" something() {"
" echo It works, yo!"
" }"
"That's all."
)
printf '%s\n' "${message[#]}"
}
The following uses a here document again, but this time with bash 4's readarray command to populate an array. Parameter expansion takes care of removing a fixed number of spaces from the beginning of each lie.
usage () {
# No tabs necessary!
readarray message <<' EOF'
Hello, this is a cool program.
This should get unindented.
This code should stay indented:
something() {
echo It works, yo!;
}
That's all.
EOF
# Each line is indented an extra 8 spaces, so strip them
printf '%s' "${message[#]# }"
}
One last variation: you can use an extended pattern to simplify the parameter expansion. Instead of having to count how many spaces are used for indentation, simply end the indentation with a chosen non-space character, then match the fixed prefix. I use : . (The space following
the colon is for readability; it can be dropped with a minor change to the prefix pattern.)
(Also, as an aside, one drawback to your very nice trick of using a here-doc delimiter that starts with whitespace is that it prevents you from performing expansions inside the here-doc. If you wanted to do so, you'd have to either leave the delimiter unindented, or make one minor exception to your no-tab rule and use <<-EOF and a tab-indented closing delimiter.)
usage () {
# No tabs necessary!
closing="That's all"
readarray message <<EOF
: Hello, this is a cool program.
: This should get unindented.
: This code should stay indented:
: something() {
: echo It works, yo!;
: }
: $closing
EOF
shopt -s extglob
printf '%s' "${message[#]#+( ): }"
shopt -u extglob
}
geta() {
local _ref=$1
local -a _lines
local _i
local _leading_whitespace
local _len
IFS=$'\n' read -rd '' -a _lines ||:
_leading_whitespace=${_lines[0]%%[^[:space:]]*}
_len=${#_leading_whitespace}
for _i in "${!_lines[#]}"; do
printf -v "$_ref"[$_i] '%s' "${_lines[$_i]:$_len}"
done
}
gets() {
local _ref=$1
local -a _result
local IFS
geta _result
IFS=$'\n'
printf -v "$_ref" '%s' "${_result[*]}"
}
This is a slightly different approach which requires Bash 4.1 due to printf's assigning to array elements. (for prior versions, substitute the geta function below). It deals with arbitrary leading whitespace, not just a predetermined amount.
The first function, geta, reads from stdin, strips leading whitespace and returns the result in the array whose name was passed in.
The second, gets, does the same thing as geta but returns a single string with newlines intact (except the last).
If you pass in the name of an existing variable to geta, make sure it is already empty.
Invoke geta like so:
$ geta hello <<'EOS'
> hello
> there
>EOS
$ declare -p hello
declare -a hello='([0]="hello" [1]="there")'
gets:
$ unset -v hello
$ gets hello <<'EOS'
> hello
> there
> EOS
$ declare -p hello
declare -- hello="hello
there"
This approach should work for any combination of leading whitespace characters, so long as they are the same characters for all subsequent lines. The function strips the same number of characters from the front of each line, based on the number of leading whitespace characters in the first line.
The reason all the variables start with underscore is to minimize the chance of a name collision with the passed array name. You might want to rewrite this to prefix them with something even less likely to collide.
To use in OP's function:
gets usage_message <<'EOS'
Hello, this is a cool program.
This should get unindented.
This code should stay indented:
something() {
echo It works, yo!;
}
That's all.
EOS
usage() {
printf '%s\n' "$usage_message"
}
As mentioned, for Bash older than 4.1:
geta() {
local _ref=$1
local -a _lines
local _i
local _leading_whitespace
local _len
IFS=$'\n' read -rd '' -a _lines ||:
_leading_whitespace=${_lines[0]%%[^[:space:]]*}
_len=${#_leading_whitespace}
for _i in "${!_lines[#]}"; do
eval "$(printf '%s+=( "%s" )' "$_ref" "${_lines[$_i]:$_len}")"
done
}
If I have a variable which have been defined as a string,
my $x = "abc";
sub p { ... }
do I then have to p("$x") or can just do p($x) or p($hash->{x})?
All works in my tests. Any downsides to not quote?
Regardless of whether it is used as a subroutine call parameter, it is generally considered to be bad practice to quote a single scalar variable, as in "$s", for two reasons
You are unnecessarily making a duplicate of the value
You may be invoking an overloaded stringify behaviour
Of course, the second may also be a good reason to choose to do exactly this, because you wanted to use the stringify special behaviour.
The only downside with using a bare variable as a subroutine parameter is that, since Perl passes the values by reference, it is possible to modify that value from within the subroutine. However you would need to modify an element of #_ which is very difficult to do accidentally.
The usual form of a subroutine is this
sub proc {
my ($p1, $p2, $p3) = #_;
# Do stuff with $p1, $p1, $p3
}
in which case you are working with safe copies of the parameters anyway, and modifying them will have no effect on the actual parameters
p($x) and p($hash->{x}) are fine. You already make a copy of the variable when you do
my ($x) = #_;
or
my $x = shift;
No need to create a copy (using "$x") on the caller's side too.
If you didn't copy the elements, you could have a problem if you changed a global variable in the sub, and you also pass that global variable as an argument to the sub.
$ perl -E'
my $x;
sub f { $x = "def"; say $_[0] }
$x = "abc";
say $x;
f($x);
'
abc
def
But why would you do that? The plausible instance of this is I can think of is the following:
$ perl -E'
sub f { "def" =~ /(.*)/s; say $_[0] }
"abc" =~ /(.*)/s;
say $1;
f($1);
'
abc
def
So maybe f("$1") makes sense sometimes, but that's about it.
I have a log file which looks like below:
4680 p4exp/v68 PJIANG-015394 25:34:19 IDLE none
8869 unnamed p4-python R integration semiconductor-project-trunktip-turbolinuxclient 01:33:52 IDLE none
8870 unnamed p4-python R integration remote-trunktip-osxclient 01:33:52
There are many such entries in the same log file such that some contains IDLE none at the end while some does not. I would like to retain the ones having "R integration" and "IDLE none" in a hash and ignore the rest. I have tried the following code but not getting the desired results.
#!/usr/bin/perl
open (FH,'/root/log.txt');
my %stat;
my ($killid, $killid_details);
while ($line = <FH>) {
if ($line =~ m/(\d+)/){
$killid = $1;
}
if ($line =~ /R integration/ and $line =~ /IDLE none/){
$killid_details = $line;
}
$stat{$killid} = {
killid => $killid_details
};
}
close (FH);
I am getting all the lines with R integration (for example I get 8869, 8870 lines) which should not be the case as 8870 should be ignored.
Please inform me if any mistake. I am still learning perl. Thank you.
I made a few changes in your program:
Always put in use strict; and use warnings;. These will catch 90% of your errors. (Although not this time).
When you open a file, you need to either use or die as in open my $fh, "<", $file or die qq(blah, blah, blah); or use use autodie; (which is now preferred). In your case, if the file didn't open, your program would have continued merrily along. You need to test whether or not the open statement worked.
Note my open statement. I use a variable for the file handle. This is preferred because it's not global, and it's easier to pass into subroutines. Also note I use the three parameter open. This way, you don't run into trouble if your file name begins with some strange character.
When you declare a variable, it's best to do it in scope. This way, variables go out of scope when you no longer need them. I moved where $killid and $killid_details to be declared inside the loop. That way, they no longer exist outside the loop.
You need to be more careful with your regular expressions. What if the phrase IDLE none appears elsewhere in your line? You only want it if its on the end of the line.
Now, for the issues you had:
You need to chomp lines when you read them. In Perl, the NL at the end of the line is read in. The chomp command removes it.
Your logic was a bit strange. You set $killid if your line had a digit in it (I modified it to look only for digits at the beginning of the line). However, you simply went on your merry way even if killid was not set. In your version, because you declared $killid outside of the loop, it had a value in each loop. Here I go to the next statement if $killid isn't defined.
You had a weird definition for your hash. You were defining a reference hash within a hash. No need for that. I made it a simple hash.
Here it is:
#! /usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use autodie;
use Data::Dumper;
open my $log_fh, '<', '/root/log.txt';
my %stat;
while (my $line = <$log_fh>) {
chomp $line;
next if not $line =~ /^(\d+)\s+/;
my $killid = $1;
if ($line =~ /R\s+integration/ and $line =~ /IDLE\s+none$/){
my $killid_details = $line;
$stat{$killid} = $killid_details;
}
}
close $log_fh;
say Dumper \%stat;
I think this is probably what you want:
while (<FH>) {
next unless /^(\d+).*R integration.*IDLE none/;
$stat{$1} = $_;
}
The regexp should be anchored to the beginning of the line, so you don't match a number anywhere on the line. There's no need to do multiple regexp matches, assuming the order of R integration and IDLE none are always as in the example. You need to use next when there's no match, so you don't process non-matching lines.
And I suspect that you just want to set the value of the hash entry to the string, not a reference to another hash.
In my perl script I want to have both versions of $config directory:
my $config='$home/client/config';
and
my $config_resolved="$home/client/config";
But I want to get $config_resolved from $config, i.e. something like this:
my $config_resolved=resolve_vars($config);
How can I do such thing in perl?
From the Perl FAQ (which every Perl programmer should read at least once):
How can I expand variables in text strings?
(contributed by brian d foy)
If you can avoid it, don't, or if you can
use a templating system, such as Text::Template or Template Toolkit,
do that instead. You might even be able to get the job done with
sprintf or printf:
my $string = sprintf 'Say hello to %s and %s', $foo, $bar;
However, for the one-off simple case where I don't want to pull out a
full templating system, I'll use a string that has two Perl scalar
variables in it. In this example, I want to expand $foo and $bar to
their variable's values:
my $foo = 'Fred';
my $bar = 'Barney';
$string = 'Say hello to $foo and $bar';
One way I can do this involves the substitution operator and a double /e flag. The
first /e evaluates $1 on the replacement side and turns it into $foo. The
second /e starts with $foo and replaces it with its value. $foo,
then, turns into 'Fred', and that's finally what's left in the string:
$string =~ s/(\$\w+)/$1/eeg; # 'Say hello to Fred and Barney'
The /e will also silently ignore violations of strict, replacing undefined
variable names with the empty string. Since I'm using the /e flag
(twice even!), I have all of the same security problems I have with
eval in its string form. If there's something odd in $foo, perhaps
something like #{[ system "rm -rf /" ]}, then I could get myself in
trouble.
To get around the security problem, I could also pull the
values from a hash instead of evaluating variable names. Using a
single /e, I can check the hash to ensure the value exists, and if it
doesn't, I can replace the missing value with a marker, in this case
??? to signal that I missed something:
my $string = 'This has $foo and $bar';
my %Replacements = (
foo => 'Fred',
);
# $string =~ s/\$(\w+)/$Replacements{$1}/g;
$string =~ s/\$(\w+)/
exists $Replacements{$1} ? $Replacements{$1} : '???'
/eg;
print $string;
I use eval for this.
So, you must replace all scalars (their names) with their values.
$config = 'stringone';
$boo = '$config/any/string';
$boo =~ s/(\$\w+)/eval($1)/eg;
print $boo;
Because you are using my to declare it as private variable, you might as well use a /ee modifier. This can find variables declared to be in local scope:
$boo =~ s/(\$\w+)/$1/eeg;
This is most tidily and safely done by the double-eval modifier on s///.
In the program below, the first /e evaluates the string $1 to get $home, while the second evaluates $home to get the variable's value HOME.
use strict;
my $home = 'HOME';
my $config = '$home/client/config';
my $config_resolved = resolve_vars($config);
print $config_resolved, "\n";
sub resolve_vars {
(my $str = shift) =~ s/(\$\w+)/$1/eeg;
return $str;
}
output
HOME/client/config
I have written a code to get the url of a website and then search for a string and then compare that string(actually a number) with a hardcoded number
#!/usr/bin/perl
use LWP::Simple;
my $oldversion =36;
$pageURL="http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/";
my $simplePage=get($pageURL);
my $newPage = "$simplePage";
my $str = (split("href=\"CVP-LATEST-5.3.0.",$newPage ))[1];
my $version = substr("$str",0,2);
print $version; // HERE IT PRINT 37 WHICH IS CORRECT
if($version =! $oldVersion )
{
print $version; // BUT HERE IT PRINTS 1 WHICH IS WRONG. HOW IS IT CHANGING ?
##-- fetch the zip and save it as perlhowto.zip
my $status = getstore("http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/CVP-LATEST-5.3.0.$version.iso", "CVP-LATEST-5.3.0.$version.iso");
}
else
{
print("Currently new version\n");
}
Why is it changing the value ? its not able to download the file becuase of that.
You mean !=, not =!, which is an assignment of a negation.
Also, split always uses a regex (except for the very special case of a string that has a single space), so those .s in 5.3.0. will match any non-newline. You probably want to \-escape them.
You may be interested in the uscan script in the debian devtools package.
You have got your "not equals" operator backwards. It should be != rather than =!.
By using =! you are in effect saying "set $version to the negated value of $oldversion".
Here is the offending line
if($version =! $oldVersion ) # Should be if($version != $oldVersion )
Also notice that by using the != operator you are telling perl that $version and $oldversion contain numbers. For string comparisons you should use the ne operator, which assumes that these variables contain strings.
if($version ne $oldVersion ) # String inequality
Here is the documentation for equality operators -
http://perldoc.perl.org/perlop.html#Equality-Operators
It's because you are assigning to $version the value !$oldVersion in this "test":
if($version =! $oldVersion )
And $oldVersion is nothing--but $oldversion is 37. You are assigning $version the boolean negation of an undefined variable. Undefined is boolean false, and so the negation is boolean true or 1.
If you read very much on perl, you're bound to come across the advice to use strict and warnings. Had you done that, it would have told you, among other things:
Global symbol "$oldVersion" requires explicit package name at - line 21.
This means that you didn't declare $oldVersion as lexical (my) or package-level (our) in this package, so if you want to use it, please include the package where you're getting it. In a vast majority of cases, a seasoned Perl programmer will recognize this as "Ugh, I didn't declare $oldVersion!" and the reason is that you declared $oldversion.
Your use of split doesn't make a lot of sense here. What you really want are the two digits following the CVP-LATEST-5.3.0. string. You're also not really doing anything by assigning one variable to another with the addition of quotes ($newPage = "$simplePage").
And, of course, as others have pointed out, the comparison is != not =!.
I'd rewrite this as:
use strict;
use warnings;
use LWP::Simple;
my $oldVersion = 36;
my $url = 'http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/';
my $newPage = get($url)
or die "Cannot retrieve contents from $url\n";
if ( $newPage =~ /href=\"CVP-LATEST-5\.3\.0\.(\d\d)/ ) {
my $version = $1;
if ( $version != $oldVersion ) {
my $status = getstore($url . "CVP-LATEST-5.3.0.$version.iso",
"CVP-LATEST-5.3.0.$version.iso");
} else {
print "Already at most recent version\n";
}
} else {
die "Cannot find version tag in contents from $url\n";
}