escape sequence for tab not working

D

dn_perl

I am unable to print a tab using "\t" sequence in a file
which has some database related statements.

I am using perl on a solaris server with Oracle.

In some_dbi.pl, the patch is : \t prints as single character.
--------
my $this_str, $sub_str ;
$sub_str = "abc" ;
$this_str = sprintf "%s\t%s", $sub_str, $sub_str ;
system( "echo $this_str") ;
--------




In ch.pl, the patch is : \t prints as tab-character.
--------
$sub_str = "abc" ;
$this_str = sprintf "%s\t%s", $sub_str, $sub_str ;
print $this_str ;
--------

What could be causing this behaviour? I want \t to print
as tab .

TIA.
 
A

Anno Siegel

I am unable to print a tab using "\t" sequence in a file
which has some database related statements.

I am using perl on a solaris server with Oracle.

In some_dbi.pl, the patch is : \t prints as single character.

I don't think "patch" is the word you want to use here.

The behavior of your code is correct and must be expected.

There are parentheses missing around the variables. Are you running
under strict and warnings? You should.
$sub_str = "abc" ;
$this_str = sprintf "%s\t%s", $sub_str, $sub_str ;

This sets $this_str to "abc", followed by a literal tab, followed by
"abc".
system( "echo $this_str") ;
--------

When you use system() in this form, it calls a shell to execute the
command given. The string the shell sees is "echo abc abc", where
the white space between the two "abc" is a tab. The shell, however,
doesn't care in what form or amount whitespace is given between words,
it just splits the command into words. So echo doesn't get to see
a tab at all, all it knows it has to echo the word "abc" twice, and
that's what it does.
In ch.pl, the patch is : \t prints as tab-character.
--------
$sub_str = "abc" ;
$this_str = sprintf "%s\t%s", $sub_str, $sub_str ;
print $this_str ;
--------

Here Perl prints the value without shell intervention. The tab character
is displayed as such.
What could be causing this behaviour? I want \t to print
as tab .

Why do you want to print the string via system() and "echo"? Perl
can print strings just fine. I don't think you'll need the following
information.

Your question is more about the shell than about Perl. You need to find
out how to make your shell's "echo" (or the system's /bin/echo) print a
tab character. For my sh, that's 'echo -e "abc\tabc"' (note inner quotes).

That string, in Perl, is written $str = '"abc\\tabc"'. The "\" must
be doubled because it is a quoting character even in single quotes.
So your sprintf should look like

$this_str = sprintf '"%s\\t%s"', $sub_str, $sub_str;

Then

system "echo $this_str";

will print the tab.

Anno
 
T

Tad McClellan

my $this_str, $sub_str ;


Do not re-type Perl code

Use copy/paste or your editor's "import" function rather than
attempting to type in your code. If you make a typo you will get
followups about your typos instead of about the question you are
trying to get answered.


Like this one.
 
N

nobull

This sets $this_str to "abc", followed by a literal tab, followed by
"abc".
Your question is more about the shell than about Perl. You need to find
out how to make your shell's "echo" (or the system's /bin/echo) print a
tab character. For my sh, that's 'echo -e "abc\tabc"' (note inner quotes).

For my sh a real tab character escaped with \ works too.

So in general you can protect $this_str from being word-split by the
shell using quotemeta().

system( "echo \Q$this_str\E"); # \E technically redundant

BTW: Someone whose oppinion I usually respect once told me the above
will not always work but was unable to offer an example where it
wouldn't.
 
N

naniwadekar

Tad McClellan said:
Do not re-type Perl code

Use copy/paste or your editor's "import" function rather than
attempting to type in your code. If you make a typo you will get
followups about your typos instead of about the question you are
trying to get answered.

I re-typed the perl code because I can't access internet
from my workstation at my workplace. But since the
followup tackled the typo AND (not INsteaDOf) my
question, I need have no regrets. *This* time, at any rate.

I do run my code under strict but I excuse myself from
re-typing it when I submit queries to the forum.

To Anno Siegel - Thanks for the help.
I did not use perl's print because I wanted the output to
go to a file instead of stdout.

Thanks, again.
 
T

Tintin

naniwadekar said:
To Anno Siegel - Thanks for the help.
I did not use perl's print because I wanted the output to
go to a file instead of stdout.

Believe it or not, Perl can actual output to files too.

It would be a little limiting having a language that only deals with
stdin/stdout.
 
A

Anno Siegel

naniwadekar said:
I re-typed the perl code because I can't access internet
from my workstation at my workplace.

Then test it again before you send it.
But since the
followup tackled the typo AND (not INsteaDOf) my
question, I need have no regrets. *This* time, at any rate.

Great. You can't be bothered to make sure the code you submit is
correct. Never mind if people have to correct few spurious errors
along the way, as long as they also answer the original question.

It's nice to learn that you don't have regrets, but I do.
I do run my code under strict but I excuse myself from
re-typing it when I submit queries to the forum.

Oh no. It's far easier to make a dozen experts find your stricture
violations manually.
To Anno Siegel - Thanks for the help.

Thanks for making your attitude clear.
I did not use perl's print because I wanted the output to
go to a file instead of stdout.

Nonsense.

Anno
 
N

nobull

naniwadekar said:
I re-typed the perl code because I can't access internet
from my workstation at my workplace.

Could I suggest removable media?
But since the followup tackled the typo AND (not INsteaDOf) my
question, I need have no regrets.

Believing that you need have no regets because you got what you wanted
even though you pissed people off in the process is not something that
most people would mention in public. (Because it makes you sound like
a bit of a sociopath).
I do run my code under strict but I excuse myself from
re-typing it when I submit queries to the forum.

Indeed, there's no moral requirement to actually include "use strict"
and "use warnings" in the code fragments you post. There is a moral
requirement to ensure there are not mistakes that would have been
detected by their use.
To Anno Siegel - Thanks for the help.

Usenet is a threaded medium. If you wish to follow-up stuff in Anno's
response you should do so in a follow-up to Anno's response.
I did not use perl's print because I wanted the output to
go to a file instead of stdout.

Perl's print() is the usual way to write to files in Perl.
 
T

Tad McClellan

naniwadekar said:
But since the
followup tackled the typo AND (not INsteaDOf) my
question, I need have no regrets.


Thank you for making your selfishness so explicit,
I now know what I need to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top