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

Can anyone explain why this doesn't work?

which date | perl -e '$_ = <STDIN>; $_ =~ s|/|\\|g; print $_;'

Edit mirod: added code tags

Replies are listed 'Best First'.
Re: Perl & NT
by azatoth (Curate) on Jun 11, 2001 at 15:28 UTC
    Change the ' for ". In Windows, specifically CMD, ' won't work.

    So in this instance, all you're sending to perl is : perl -e `$_, and the rest of the line is parsed by CMD, including the pipes.

    Your correct code should look like this :
    which date | perl -e "$_ = <STDIN>; $_ =~ s|/|\\|g; print $_;"
    P.S You need the MKS toolkit installed on your machine to in order to run the which command.

    P.M.S Also use the code tags, to format any posted code in a nice, readable fashion.

    Azatoth a.k.a Captain Whiplash

    Make Your Die Messages Full of Wisdom!
    Get YOUR PerlMonks Stagename here!
    Want to speak like a Londoner?
Re: Perl & NT
by bwana147 (Pilgrim) on Jun 11, 2001 at 16:05 UTC

    azatoth already said it all. I'll just add my €0.02 about conciseness (for that's what a one-liner should aim at, IMHO):

    which date | perl -pe "s|/|\\|g"

    --bwana147

Re: Perl & NT
by bikeNomad (Priest) on Jun 11, 2001 at 20:20 UTC
    Another possibility is that NT doesn't have "which" unless you've added it using something like Cygwin or the MKS Toolkit. So there isn't enough information here to figure it out. We'd need to know:

    <bl>

  • What shell you're using (CMD or something sh-like)
  • What the COMSPEC and SHELL variables are
  • What "which" is and whether it exists </bl> Anyway, if you just want a good which command, you might try this all-Perl solution (save as which.bat):

    @rem = '--*-Perl-*-- @echo off if "%OS%" == "Windows_NT" goto WinNT perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl :WinNT perl -x -S "%0" %* if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl if %errorlevel% == 9009 echo You do not have Perl in your PATH. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul goto endofperl @rem '; #!perl #line 15 # uses PATH and PATHEXT under NT my @path = split(';', $ENV{PATH}); my @pathext = split(';', $ENV{PATHEXT}); @pathext = qw(.com .exe .bat .cmd) if !@pathext; unshift(@pathext, ''); unshift(@path, '.'); my $all = 0; ARG: foreach my $arg (@ARGV) { if ($arg eq '-a') { $all = 1; next ARG; } foreach my $path (@path) { foreach my $ext (@pathext) { if (-f "$path\\$arg$ext") { print "$path\\$arg$ext\n"; next ARG if !$all; } } } } __END__ :endofperl
Re: Perl & NT
by hackmare (Pilgrim) on Jun 11, 2001 at 20:09 UTC

    Careful here there are 2 things going on.

    1) I presume you know that win32 does not support the pipe character and the which command, and I'll move on to the other possible dumb thing:

    2) On my solaris implementation, there si no support for which at the non-root level. Hence your command does not work on this machine either. This is the case on my machine. Because of this, I tried to use an env variable $VAL

    Here's what I got:

    509$ echo $VAL /usr/local/home/esop/esop_dev/batch/apps 510$ echo $VAL | perl -e '$_ = <STDIN>; $_ =~ s|/|\\|g; print "a $_"; +' \usr\local\home\esop\esop_dev\batch\apps 511$ echo $VAL | perl -e '$_ = <STDIN>; $_ =~ s|/|\\|g; print "$_";' \usr\local\home\esop\esop_dev\batch\apps 512$

    I suggest you try the same thing to make sure that you're not enjoying double errors.

    --Hackmare

      I presume you know that win32 does not support the pipe character and the which command...

      Windows NT/2000 (and maybe 9x - I don't use them, so I can't check) support pipes just fine. Try typing something like:

      dir | more

      next time you encounter a Windows box. By the time you install the GNU utilities from the resource kit you can even get part way to getting a useful CLI