Perl -e insert new line

0

Possible Duplicate:
insert newline in perl -e statement

Hi

If I do this in bash

perl -e '$x; $y'

Is there any character that can behave like a new line?

i.e. I want to do

perl -e '$x; some_chars_as_new_line $y'

where some_chars_as_new_line might be \, not meta character.

where perl interpreter actually sees an actual new line.

$x; 
$y;

Many thanks

Don Ch

Posted 2010-03-25T04:12:48.060

Reputation: 4 869

Question was closed 2010-03-25T11:00:39.770

2

Duplicate of http://superuser.com/questions/123467/insert-newline-in-perl-e-statement

– Josh K – 2010-03-25T04:40:31.473

please don't double-post. you can edit your earlier question to add information if you need to. – quack quixote – 2010-03-25T04:44:16.207

this is a slightly different question to http://superuser.com/questions/123467/insert-newline-in-perl-e-statement (which has been answered) Please don't close.

– Don Ch – 2010-03-25T05:26:11.863

ok, yes, i see a miniscule difference. but there's an answer on your other question (that i just expanded) that answers this question too. (granted, not as well as Chris's answer below...) since they are so closely related (they have practically the same title), i still think they should be in one post. – quack quixote – 2010-03-25T05:34:26.960

Answers

-1

The question you have asked only concerns how the shell interprets its input and passes it along to other programs. It has nothing to do with Perl, per se.

You should just be able to put this in a script:

foo '$x;
$y;'

The argument given to the command will have a newline in the same place as it does in the script itself. You may need to take care to save such a script in such a way that it ends with Unix-style (LF-only) line breaks, otherwise you might get a CR+LF (DOS/Windows line breaks) or just CR (old Mac-style lineb reaks). You can also do this at an interactive prompt, but you will see a continuation prompt before the second and any subsequent lines:

$ foo '$x;
> $y;'

In bash, you can also use the $'' quoting syntax to encode a newline character like this:

foo $'$x;\n$y;'

The argument passed to the program will be treated in a manner similar to an ANSI C string. If you want an actual backslash in the string you will have to escape it as \\, instead.

So, if you really want the literal-string quoting that single quotes give you, you should probably stick with an embedded newline so that you do not have to worry about extra escaping.

Chris Johnsen

Posted 2010-03-25T04:12:48.060

Reputation: 31 786

@Chris: this question will be closed as a duplicate; please post this to the duplicate and delete it here. (you'll lose the downvote when you do!) – quack quixote – 2010-03-25T05:23:56.440

@quack: I am not so concerned with the downvote that I would copy this answer to the other question as it stands (anyway, it might be downvoted there, too). I would want to see the other questioned explicitly broadened to include an ‘initial input’ sense (along with the original ‘edit/insert’ sense). But my editor rep is too fresh for me to initiate such a rewrite without the OPs consent. @lydonchandra, IMO, these questions are close enough they could be merged. I could do it if you like (just say “go ahead, merge them”). You could easily revert the changes if you did not like the result. – Chris Johnsen – 2010-03-25T11:19:53.293

i've gone ahead and edited the other question to include both parts, so this answer is now on-topic there. if you don't want to repost, it can be moved by the mods at some point in the future. – quack quixote – 2010-03-25T12:18:36.803