in reply to Re: input special characters
in thread input special characters

Hi, The input is $char = <>: Then I use it like this: $out = "Field1$charField2$char....." Regards, Kepler

Replies are listed 'Best First'.
Re^3: input special characters
by AppleFritter (Vicar) on Jul 13, 2014 at 11:49 UTC

    I'm not sure what the issue is, then. Take this snippet of code:

    chomp(my $sep = <>); say join $sep, @data;

    if I run this (with the above @data) and input a tab character, I do indeed get the output separated by tabs. Or do you want to be able to input the characters "\t" and have them interpreted as a tab, rather than a literal backslash followed by a literal lower-case t? In that case, you can use a regular expression to transform your separator:

    chomp(my $sep = <>); $sep =~ s/\\t/\t/g; say join $sep, @data;
      Hi, Your code is brilliant - problem solved! Thank you very much :) But I was thinking....is there a way to apply this to all special characters (\t, \n, ....etc)? Kind regards, Kepler

        You're welcome! *tips hat* My anonymous brother's solution from Re: input special characters will do that:

        chomp(my $sep = <>); my $sep = eval "qq/$sep/"; say join $sep, @data;

        That said, as he also points out, this has its own set of risks, so weight the benefits carefully and decide whether you can trust your users. Using eval on user-supplied strings is generally a Bad Idea™.

        A quick CPAN search finds String::Unescape, which avoids these problems and also keeps you from having to do your own unescaping:

        use String::Unescape; # ... chomp(my $sep = <>); $sep = String::Unescape->unescape($sep); say join $sep, @data;