agidden has asked for the wisdom of the Perl Monks concerning the following question:

Haved used Perl for years on Windows, Linux, and UNIX. In the last few days, me and the rest of the team have been getting thwarted by a silly character escaping issue related to C shell on Unix (Sun).
We have a working .pl script that needs to be used as a 'one-liner' with the -e option. The problem is that the script will not run when you use the -e option in CSH. We believe it may have to do with escaping characters in the Perl code since it will run as-is on Linux in BASH.
Note that the code sample is indented oddly due to its inclusion in a shell script.
Here is our Perl code inside our shell script:
#!/usr/bin/csh -f # perl -e '$outputDir = "/tmp/foo/";\ $input = "file.java";\ open( INPUT_FILE,"<$input" ) or die ("Could not open $input :$!"); \ $file = "";\ $dir = "";\ $package = "";\ @imports = "";\ while( <INPUT_FILE> ) {\ if(s/^\/\/<END_CLASS: (.*) ?$/$1/) {\ close(OUTPUT);\ $file = "";}\ if($file) {\ print OUTPUT $_;}\ if(s/^package ([a-zA-Z\._0-9]+).*$/$1/) {\ $package = $dir = $1;\ $dir =~ tr/\./\//;}\ if(/^import.*/) {\ push(@imports,$_);}\ if(s/^\/\/<BEGIN_CLASS: ([a-zA-Z_0-9]+).*$/$1/) {\ if($dir) {\ $file = "$dir/$1.java";\ } else {\ $file = "$1.java";}\ open(OUTPUT,">$outputDir$file") or die("Could not open output file + $outputDi r$file $!"); \ print "Created $file\n";\ if($package) {\ print OUTPUT "package $package;\n";}\ print OUTPUT @imports;\ }\ }\ close( INPUT_FILE );'
Note that the end of each line is escaped with a backslash. We expected to have to do this. But we get this error when you run the script on Sun UNIX:
");: Event not found
We have escaped the quotes and the parens (together and individually) and nothing seems to change the error.
We have to use CSH and we have to use Perl -e for this code, so we have no choice but to find a way to progress. Our Perl version is 5.004_04 for SunOS.
Any suggestions on how to escape this code would help. We have burned a lot of time on this and are getting nowhere.
Thanks in advance for any suggestions you may have.

Replies are listed 'Best First'.
Re: Need help using Perl -e in C shell
by merlyn (Sage) on Apr 27, 2005 at 19:02 UTC
      Ahh. Here-doc. Have not used this in 10 years. Completely forgot to try this. Thanks for your assistance. I wrote UNIX installers for many years and should have thought of this. We have a solution, but I will try this as well since it will allow the code to be formatted a bit nicer. FYI: We have to add some new features to a very old legacy application that generates source code. Therein lies the constraint to use the existing shell scripts and to not be able to add any files. Amazing what you will do when a large customer throws a lot of money at you. My team and I sincerely appreciate your assistance with this issue. We spent too long on it and were just not getting anywhere.
Re: Need help using Perl -e in C shell
by Corion (Patriarch) on Apr 27, 2005 at 19:01 UTC

    The problem arises from the character "!", which is special for some shells, and seemingly for your C shell.

    I really wonder why you aren't using a separate file or a here-document in your shell script (if C shell supports them). ... And why you're using C shell at all ...

      Thank you for the answer. We tried to escape the exclamations, but perhaps we combined it with something else that caused a problem. We have a legacy-based process that we are trying to scab some newer features into. The product generates source code and we have to work with what is already being generated (CSH, and cannot add files). We blew a lot of time on this, and your help finding the simple problem has been priceless.
Re: Need help using Perl -e in C shell
by davidrw (Prior) on Apr 27, 2005 at 19:16 UTC
    Would perl -x (man perlrun) be a solution here to cleanly embed the perl code (the here-doc method suggested above is also good) but still be allowed within your constraints?
    #!/usr/bin/csh -f /usr/bin/perl -x $0 exit; #!/usr/bin/perl printf "I'm a perl script\n";
Re: Need help using Perl -e in C shell
by blazar (Canon) on Apr 28, 2005 at 10:15 UTC