Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Say I have a file which looks like this:
Hey\tThere
It's two words, 'Hey' and 'There', seperated with a tab char. I've called this file 'dummy'. I then run this little code snippet:
$ perl -e 'open FILE, "dummy"; print "A\tTab\n"; while(<FILE>) { print +; }'
This results in:
A    Tab
Hey\tThere
a) Why isn't my line interpolated? Not even print "$_"; will do the tab.
b) How can I make it print out my tab?

Replies are listed 'Best First'.
RE: Newline?
by lhoward (Vicar) on Jul 21, 2000 at 17:20 UTC
    This is happening because string interpolation (when /t is turned into a tab) occurs when the scalar is loaded with the string. The double-quotes, as you used in your program cause interpolation to occur. Single quotes, reading data from a file, etc. cause no interpolation to occur. You have two possibilities:
    1. 1. translate the \t to a tab character. i.e. =~s/\\t/\t/g;
    2. 2. use an eval to force interpolation.
      eval "\$f=qq/$_/"; print $f;
Re: Newline?
by Corion (Patriarch) on Jul 21, 2000 at 14:05 UTC

    Your question is either unclear or wrong. I made myself the following test file :

    Hallo	Da
    Dies	Test
    Ist	ein
    

    And your script works as I think it should, as it prints out :

    A Tab Hallo Da Dies Test Ist ein
    Maybe you should rephrase your question ... Is it maybe that you have a literal "\t" (and not the TAB character) ? Then maybe a regular expression replacing \t by \009 would do what you want ...
    s/\\t/\009/g

Re: Newline?
by lindex (Friar) on Jul 22, 2000 at 02:21 UTC
    just my output :)
    ~(pts)$ echo -ne "Hi\tThere\n" > dummy ~(pts)$ cat dummy Hi There ~(pts)$ vi dummy :set list 1 Hi^IThere$ :x ~(pts)$ perl -e 'open(FILE,"dummy"); print "A\tTab\n"; print <FILE>;' A Tab Hi There ~(pts)$




    lindex
    /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/