VS Code - Keep first mixin argument on new line - visual-studio-code

In an SCSS file, I have a mixin defined like this:
#mixin foo(
$arg1,
$arg2,
$arg3
) {
color: $arg1;
}
After applying formatting (Command Palette > Format Document), the first argument is moved up to the first line of the mixin:
#mixin foo($arg1,
$arg2,
$arg3) {
color: $arg1;
}
How can I keep $arg1 on a new line? Is there a setting for this?

Related

unable to give value to a scalar from command line in perl

I am trying the following perl code:
use strict;
use warnings;
use Scalar::Util ("looks_like_number");
my $color;
if (undef $color)
{
my $color = $ARGV[0];
}
print "$0\n";
print "$ARGV[0]\n";
my #colors = ("blue", "yellow", "brown", "white");
print "Please select a num:\n";
foreach my $i (0..$#colors)
{
my $j = $i+1;
print " $j $colors[$i]\n";
}
my $num = <STDIN>;
chomp($num);
if (looks_like_number($num) and defined $colors[$num-1])
{
$color = $colors[$num-1];
}
else
{
print "Bad Selection\n";
}
print "selected color is $color\n";
I want to select any number for the corresponding color choice or I should be able to provide a value of color by $color variable through command line, I am trying to run it in windows cmd using ' perl [C:/scriptname.pl] [color] ' but its not taking the argument but when I am printing ARGV[0] it is showing the argument being passed correctly. so what is the issue with my 'if (undef ARGV[0])' statement that its not getting executed.
You are declaring the variable $color twice:
my $color;
if (undef $color)
{
my $color = $ARGV[0];
}
The second my $color will create a second binding of the name $color and the value you assign to it and that binding will not be visibile outside of the scope, the enclosing curly brackets.
The expression
if (undef $color)
does not do what you intend it to do. undef will always set a value to undef. You want to use defined instead.
After applying those two changes, the code could look like:
my $color;
if (! defined $color)
{
$color = $ARGV[0];
}
my $color;
if (undef $color)
{
my $color = $ARGV[0];
}
This looks rather strange. In line 1 you declare a scalar variable called $color. In line 2, you call undef() on that variable which replaces the contents of $color with undef() (that's unnecessary as a newly-declared Perl scalar will always contain undef()). This expression will return undef, which is false, so the code in your if block is never executed.
In line 4, you declare a new variable, also called $color and set that to $ARGV[0]. There are two problems with this. Firstly, that line will never be executed for the reasons explained in the previous paragraph. And, secondly, your new $color variable will cease to exist once you leave the block, so you would never see the effect of this change.
I think the main problem here is that you have confused undef() with defined(). undef() will always give a variable an undefined values, but I suspect you wanted defined() which tells you whether or not a variable contains a defined value.
What you probably wanted was:
my $color;
if (!defined $color) {
$color = $ARGV[0];
}
Which can be written as:
my $color //= $ARGV[0];
But it's still slightly confusing as it's not clear why you think $color could ever be defined immediately after being declared.

Does Anyone Know How to Understand Such Kind of Perl Code Blocks?

I'm confused by Perl Named Blocks (I thought they are...). Below is an example:
#!/usr/bin/perl
sub Run(&){
my ($decl) = #_;
$decl->();
}
sub cmd(&){
my($decl) = #_;
my(#params) = $decl->();
print "#params\n";
}
sub expect(&){
my ($decl) = #_;
my(#params) = $decl->();
print "#params\n";
}
Run {
cmd { "echo hello world " };
expect { exit_code => 0, capture => 2};
};
Note the last lines. It looks like "Run", "cmd", "expect" are named blocks, but not functions. Does anyone know what they are? Any available link introduces them?
I can't find any reference for such grammar.
Let's decipher this definition for Run:
sub Run(&){
my ($decl) = #_;
$decl->();
}
It means subroutine called Run, which accepts parameter of type CODE (that's why it uses (&)). Inside it $decl gets assigned to that passed code, and this code gets called by $decl->();.
Now, last lines in your example:
Run {
cmd { "echo hello world " };
expect { exit_code => 0, capture => 2};
};
are equivalent to:
Run(sub {
cmd { "echo hello world " };
expect { exit_code => 0, capture => 2};
});
In other words, it calls Run with anonymous procedure code that is in braces.
Run, cmd, and expect are prototype defined functions, and they work like built-in functions (no need to write sub{..}, as this is implicit due (&) signature for these functions).
If these functions were defined without prototypes,
sub Run { .. }
sub cmd { .. }
sub expect { .. }
then explicit sub{} arguments would be not optional but required,
Run(sub{
cmd(sub{ "echo hello world " });
expect(sub{ exit_code => 0, capture => 2});
});
They are subroutines accepting block as an argument. See: http://perldoc.perl.org/perlsub.html#Prototypes. The (&) in their definitions means that their first argument is a block of code.

Pass a function (with arguments) as a parameter in PowerShell

I've been successfully passing no-argument functions around in PowerShell using ScriptBlocks. However, I can't get this to work if the function has arguments. Is there a way to do this in PowerShell? (v2 preferably)
Function Add([int] $x, [int] $y)
{
return $x + $y
}
Function Apply([scriptblock] $s)
{
write-host ($s.Invoke(1,2))
}
Then
Apply { Add }
writes 0 to the console. Apply does invoke Add, but doesn't pass any arguments in (i.e. uses the default [int] values of 0 and 0)
Ok, I found the answer here:
I wanted ${function:Add} rather than { Add } in the call to Apply.
There is a much cleaner way to do this using the PowerShell function provider. I needed to be able to run functions from other source files with variable numbers of arguments based on an XML file provided at run time.
In developing this I wrote a small "Hello World" tester in two source files which should be fairly self-explanatory (the magic line is "$x = (& $func $parm)"):
"Hello.ps1" - The remote function
function Hello ($in) {
write-Host "Hello $in"
}
"Hello2.ps1" - The executing routine
function exec-script ($file, $func, $parm) {
Invoke-Expression $file
$x = (& $func $parm)
$x
}
exec-script -file ".\Hello" -func "Hello" -parm "World"

How can I use switch type arguments for subroutines in Perl

I want to call subroutines in Perl like:
sub temp {
---- some code -----
}
temp(-switchName, value1, --switchName2, value2)
Like I know Getopt::Long is there for command line switches type arguments.
So I want to know for subroutine type arguments.
There's a number of reasons why one might want to do that, but you didn't say so I'm going to have to speculate some.
Switches on the command line are useful because command line programs mix up options with a list of arguments and need some way of knowing the difference. Thus the "things which start with - are not regular arguments" convention.
command --key value --key2 value2 file1 file2 file3
command file1 --key value file2 --key2 value2 file3
You can certainly do something similar with a subroutine, where it picks through its list of arguments looking for things that start with -- and implying the next in the list is the associated value... but subroutines have better and easier ways to do it.
temp( ["file1", "file2", "file3"], { something => 1 } );
In this case the main list of arguments are passed in first as an array reference, and the options are passed in second in a hash reference. There's no ambiguity.
sub temp {
my($files, $options) = #_;
print "Something!\n" if $options->{something};
for my $file (#$files) {
...do something with $file...
}
}
You can even take this one step further and pass in everything as an option.
temp( files => ["file1", "file2", "file3"], something => 1 );
sub temp {
my %args = #_;
print "Something!\n" if $args{something};
for my $file (#{$args{files}}) {
...do something with $file...
}
}
That's useful if it's not so clear what's an option and what's an argument. Probably overkill for the "list of files with options" example here.
If I understand correctly you can pass your arguments to a hash and use it to acces them. Like that:
sub temp {
my %opts = #_;
if ($opts{'-switch1'}) {
# ... do something ...
}
...
}
temp(-switch1 => 1);
# or
temp(-switch1, 1);

Why am I getting a syntax error when I pass a coderef to this prototyped Perl subroutine?

This is the code:
sub function($&) {
my $param1 = shift;
my $code = shift;
# do something with $param1 and $code
}
If I try to call it like this:
function("whatever") {
print "i'm inside the coderef\n";
}
I get Not enough arguments for MyPackage::function at x.pl line 5, near ""whatever" { ". How can I call it without having to add sub in front of the code block?
Put the coderef argument first:
sub function (&$) {
my $code = shift;
my $param1 = shift;
# do something with $param1 and $code
}
function { print "i'm inside the coderef\n" } "whatever";
See the perlsub man page, which reads in part:
An "&" requires an anonymous subroutine, which, if passed as the first argument,
does not require the "sub" keyword or a subsequent comma.
Here,
$& is a Perl's special variable which is used to match the exact pattern. (you have wrongly used it in your context)
$` is used to match the string before the given pattern.
$' is used to match the string after the given pattern.