2009/12/12 Parag Kalra <paragka...@gmail.com>: > Hello All, > > 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.
The easy way: use cygwin! The meta way: why do you care? It's a one-liner, and by its very nature a one-off, throwaway program. The cheating way: you can force the windows version to work under *NIX like this: p...@tui:~/tmp$ echo \$_ $_ p...@tui:~/tmp$ perl -e "foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print 'I am only addicted to - Shekels' if ($_ =~ /^s.*/i) }" I am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - Shekels this is because in bash, $_ means "The last argument of the previous command executed". The "echo \$_" command forces $_ to interpolate to a literal $_, allowing the perl script to see it unmolested. The real way: Consider that you must use "" to surround the program, because cmd.exe won't understand ''. Now, the reason that the existing windows version doesn't work under *NIX is that the shell is trying to interpolate $_ into the string as mentioned above, so we must try to prevent that. We can do this by escaping the $: p...@tui:~/tmp$ perl -e "foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print 'I am only addicted to - Shekels' if (\$_ =~ /^s.*/i) }" I am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - Shekels D:\>perl -e "foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print 'I am only addicted to - Shekels' if (\$_ =~ /^s.*/i) }" I am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - Shekels In the end though, I fail to see the utility of writing one-liners which execute on both linux and windows. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/