How to expand escape sequence (e.g. \n)?

  • Thread starter Wojtek Dziegielewski
  • Start date
W

Wojtek Dziegielewski

How to expand escape sequences contained in a variable?


Example, if I want the input record separator ($/) to be 2 consecutive new
line characters, I can use literal string like this:

$/ = "/n/n";

However, my sequence for the input record separator is inside a variable,
like this:

my $a = '/n/n';

I need to assign the contents of $a to $/ in such a way that /n's will get
translated into newline characters. How can I have it accomplished in Perl?
 
S

Scott W Gifford

[...]
my sequence for the input record separator is inside a variable,
like this:

my $a = '/n/n';

I need to assign the contents of $a to $/ in such a way that /n's will get
translated into newline characters. How can I have it accomplished in Perl?

It works like you'd expect, except there are two bugs in your code
above. First, you use a backslash, not a forward slash, to write \n.
Second, \n isn't interpreted inside of single quotes, but only inside
of double quotes. So if you do:

my $a = "\n\n";
$/ = $a;

you'll get what you expect.

----ScottG.
 
W

Wojtek Dziegielewski

My apologies for the typo - it was a long day for me.

My actual scenario however is more complex and cannot be addressed by a
simple assignment. I used initial assignment to $a as an illustration and
not the actual piece of code.

my $a = '\n\n'; # in reality $a is read from XML config file, so I can't
say my $a = "\n\n"; instead

I need to take a value of $a (which contains literal '\n\n') and have it
translated to 2 newlines. Haw can I do that?

Scott W Gifford said:
[...]
my sequence for the input record separator is inside a variable,
like this:

my $a = '/n/n';

I need to assign the contents of $a to $/ in such a way that /n's will get
translated into newline characters. How can I have it accomplished in
Perl?

It works like you'd expect, except there are two bugs in your code
above. First, you use a backslash, not a forward slash, to write \n.
Second, \n isn't interpreted inside of single quotes, but only inside
of double quotes. So if you do:

my $a = "\n\n";
$/ = $a;

you'll get what you expect.

----ScottG.
 
P

Paul Lalli

Wojtek said:
My apologies for the typo - it was a long day for me.

My actual scenario however is more complex and cannot be addressed by a
simple assignment. I used initial assignment to $a as an illustration and
not the actual piece of code.

my $a = '\n\n'; # in reality $a is read from XML config file, so I can't
say my $a = "\n\n"; instead

I need to take a value of $a (which contains literal '\n\n') and have it
translated to 2 newlines. Haw can I do that?

A simple search and replace will do it:

$a =~ s/\\n/\n/g;

That searched for all literal \n and replaces them with a new-line
character.

Paul Lalli
 
T

Tad McClellan

[ Please do not post upside-down. We don't like it here (nor does
anywhere else on Usenet).
]


Wojtek Dziegielewski said:
I need to take a value of $a (which contains literal '\n\n') and have it
translated to 2 newlines. Haw can I do that?


$a =~ s/\\n/\n/g;
 
B

Brian McCauley

Wojtek Dziegielewski top-posts (please don't):
my $a = '\n\n'; # in reality $a is read from XML config file, so I can't
say my $a = "\n\n"; instead

You could condider using the actual string in the XML rather than a
representation of it.
I need to take a value of $a (which contains literal '\n\n') and have it
translated to 2 newlines. Haw can I do that?

Take a step back. The string '\n\n' is a representation of two newlines
in Perl source code. There is a way to cause perl to interpret a
string as Perl source code. This is the eval(STRING) function. The
problem is that if you use the eval(STRING) to implement this you allow
the the person writing the config file to execute arbitrary commands
inclusing stuff like system('rm -rf /'). For this reason many of the
people 'round here consider the existance of eval() to be forbidden
knowledge.

Depending on circumstances it may be that the config file already has
the capability to execute arbitrary code. For example there may be
parameters that are to be interpreted as shell commands. If this is the
case then then perhaps eval() would not be such a bad idea.

So my advice is probably that you should not use eval() but you should
not make this decision by default simply by not being aware of the option.

Should you want to use eval...

chop( $a = eval "<<__EOD__\n$a\n__EOD__" );

This will expand \n and \t and anthing else you could embeb in a
double-quotish context like \x{263a} but also including arbitrary Perl
statements using @{ whatever }.

If you do not want to provide the config file with the full semantics of
Perl double-quotish string then you can implement a subset of the
semantics using a s/// as others have suggested - just choose your subset.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top