Perl: How to use command line special characters (newline, tab) in $LIST_SEPARATOR ($") -


i use value of variable (fixed command line option instance) list separator, enabling value special character (newline, tabulation, etc.).

unfortunately naïve approach not work due fact 2 following print statement behave differentely :

my @tab = ("a","b","c");  # block 1 gives expected result: # # b # c {   local $" = "\n";                    #" let please color syntax engine   print "@tab"; }  # block 2 gives unwanted result: # a\nb\nc {   use getopt::long;   $s;   getoptions('separator=s' => \$s);   local $" = "$s";                    #" let please color syntax engine   print "@tab"; } 

any idea can correct block 2 wanted result (the 1 produced block 1) ?

it work same if assign same string. perl's

"\n" 

creates 1 character string consisting of newline. shell (bash), you'd use

' ' 

to same.

$ perl a.pl --separator=' ' b ca b c 

you didn't this. passed string consisting of 2 characters \ , n perl instead.

if program convert 2 chars \n newline, you'll need tell so.

my @tab = qw( b c );  sub handle_escapes {   ($s) = @_;   $s =~ s/\\([\\a-z])/     $1 eq '\\' ? '\\' :     $1 eq 'n' ? "\n" :     { warn("unrecognised escape \\$1"); "\\$1" }   /seg;   return $s; }  {   $s = '\n';                     #" let please color syntax engine   local $" = handle_escapes($s);   print "@tab"; }  {   use getopt::long;   $s;   getoptions('separator=s' => \$s);   local $" = handle_escapes($s);    #" let please color syntax engine   print "@tab"; } 

$ perl a.pl --separator='\n' b ca b c 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -