Is there a word list interpolation operator in the form of a word?

qw{} is a nice way to write lists. Is there something like this that interpolates words, i.e. extends variables? perlop doesn't seem to mention.

+3
source share
4 answers

No, there is no built-in, but many of us write ourselves.

Also for the two kinds of ql() needs for string lists. I use deQ for q() version and deQQ for qq version of those that work with the Perls "hasta" operator:

 sub dequeue($$) { my($leader, $body) = @_; $body =~ s/^\s*\Q$leader\E ?//gm; return $body; } sub deQ($) { my $text = $_[0]; return dequeue q<|Q|>, $text; } sub deQQ($) { my $text = $_[0]; return dequeue qq<|QQ|>, $text; } 

This allows me to use things like this:

 sub compile($) { my $CODE = shift(); my $wrap = deQQ<<"END_OF_COMPILATION"; |QQ| |QQ| use warnings qw[FATAL all]; |QQ| no warnings "utf8"; |QQ| |QQ| sub { |QQ| my \$_ = shift; |QQ| $CODE; |QQ| return \$_; |QQ| } |QQ| END_OF_COMPILATION return eval $wrap; } 

or

  my $sorter = new Unicode::Collate:: upper_before_lower => 1, preprocess => \&reduce_for_sorting, entry => deQ<<'END_OF_OVERRIDE' |Q| |Q| 005B 006E 002E ; [.0200.0020.0002.0391] # [n. |Q| 005B ; [.0220.0020.0002.0392] # [ |Q| 005D ; [.0225.0020.0002.0395] # ] |Q| END_OF_OVERRIDE 

See how it works?

+6
source

You can sprinkle qw() in the middle of a "normal" list. Sometimes I write code as follows:

 my @command = ( qw(cat arg1 arg2), $arg3, qw(arg4 arg5 arg6), "$arg7 $arg8", # ... ); 
+6
source

Ysth answer extension:

 sub qqw($) { split /\s+/, $_[0] } my @list = qqw"$var interpolating string"; 

Caveats: I don't know how leading and trailing spaces are handled. In addition, the prototype must make sure that qqw does not consume multiple values, separated by commas, for example, like regular calls, but you should check this.

+2
source

Just:

 split(' ', "$var interpolating string "); 
+1
source

Source: https://habr.com/ru/post/1393303/


All Articles