Parag Kalra wrote:

On Sat, Dec 12, 2009 at 1:55 AM, John W. Krahn <[email protected]> wrote:

Parag Kalra wrote:

 This works on Linux -
perl -e 'foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print "I
am only addicted to - 'Shekels' \n" if ($_ =~ /^s.*/i) }'

This works on Windoze -
perl -e "foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print 'I
am only addicted to - Shekels' if ($_ =~ /^s.*/i) }"

How can I have 1 code that will run both on Nix & Windoze.

I don't have Windows to test this on, but try:

perl -le "/^s/i && print q/I am only addicted to - 'Shekels'/ for qw/Sugar
Sex Simplicity Sleep Success Smoking/"
>
And thanks John, your code worked like a breeze both on Windoze & *Nix :)
And good thing is it provided the solution to insert a new-line character
which was not present in my Windoze code.

One question though in following code -

perl -le "/^s/i && print q/I am only addicted to - 'Shekels'/ for qw/Sugar
Sex Simplicity Sleep Success Smoking/"

How is it going to new line without a new line character - '\n'.

The switch -l (in "perl -le") sets the Output Record Separator to be the same as the Input Record Separator.

$ perl -MO=Deparse -e1
'???';
-e syntax OK
$ perl -MO=Deparse -le1
BEGIN { $/ = "\n"; $\ = "\n"; }
'???';
-e syntax OK


If I am not wrong - 'q/ /' signifies quoted context thus - a
replacement for double quotes

No.  qq// is double quotes (two q's) but q// is single quotes.


but still you should need a new line character to go to next line.

Kindly correct me if I am wrong.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to