Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Can't write files (even redirected from shell)

by Flame (Deacon)
on Jan 14, 2008 at 04:17 UTC ( #662252=note: print w/ replies, xml ) Need Help??


in reply to Re: Can't write files (even redirected from shell)
in thread Can't write files (even redirected from shell)

... Thank you... I knew it would be something simple...

Let this be a lesson to ALL!! Don't fall out of perl practice... I used to know that...

Again, thanks.


0x596F752068617665206265656E2068657865642E


Comment on Re^2: Can't write files (even redirected from shell)
Re^3: Can't write files (even redirected from shell)
by ikegami (Pope) on Jan 14, 2008 at 04:21 UTC

    Is Morningtide out yet? Guess not.

    For long running processes like that, I usually tee the output. And since Windows doesn't have tee, I wrote one quickly.

    tee.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 use strict; use warnings; use IO::Handle qw( ); use Getopt::Long qw( ); my $opt_a; sub usage { print STDERR (<<'__EOI__'); usage: tee [<options>] [--] [file [...]] Use tee --help for help. __EOI__ exit(1); } sub help { print(<<'__EOI__'); usage: tee [<options>] [--] [file [...]] Use tee --help for help. Options: -a Append the output to the files rather than overwriting them. -i Ignore the SIGINT signal. __EOI__ exit(0); } sub process_args { Getopt::Long::Configure('posix_default'); Getopt::Long::GetOptions( "a" => \$opt_a, "help|h|?" => \&help, ) or usage(); } { process_args(); my $mode = ($opt_a ? '>>' : '>' ); my $mode_text = ($opt_a ? 'open' : 'create'); my @fhs; my %fh_names; { my $fh = \*STDOUT; push @fhs, $fh; $fh_names{$fh} = 'STDOUT'; } foreach (@ARGV) { my $fh; if (!open($fh, $mode, $_)) { die("Unable to open file \"$_\": $!\n"); } push @fhs, $fh; $fh_names{$fh} = "file \"$_\""; } binmode(STDIN); foreach my $fh (@fhs) { $fh->autoflush(1); binmode($fh); } for (;;) { my $buf = ''; my $rv = sysread(STDIN, $buf, 4096); if (not defined $rv) { die("Unable to read from STDIN: $!\n"); } if (not $rv) { last; } foreach my $fh (@fhs) { if (!$fh->print($buf)) { die("Unable to write to $fh_names{$fh}: $!\n"); } } } } __END__ :endofperl

      Cool. Definately a good use of Perl. Fortunately, I am blessed with having tee on windows (yay UnixUtils), but it was among the list of programs I had never needed to use and so, didn't know what it did, *nix is full of those still.

      On an amusing note, having a perl version I can look at gives me a better idea of what it does than the man page did.


      0x596F752068617665206265656E2068657865642E

      This is a nice boilerplate use of perl -x to make this into a polyglot batch file. I especially liked the following batch line:
      if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul

      --
      [ e d @ h a l l e y . c c ]

        The batch file was created by using pl2bat on the Perl script.

        I have personally used cmd /c exit 1 to set the errorcode in the past.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://662252]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-07-25 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls