Piped input to one-liners being truncated

T

tnitzke

I'm working from Windows 2000 and a lot of my use of Perl involves writing general purpose
batch files and using them from one-liners. The first time I ran into this problem I
was quite mystified until I figured out what was going on. It seems that piped input to a
one-liner that runs a Perl program nested inside a batch file only executes the first line
of input. I wondered if anybody here could shed some light on why this is occuring? I'm able
to work around it, but I'm still curious what is going on.

As an example lets assume I have a file named "servers" with a list of machines running
MS SQL Server. I would like to check the SQL Server version for each machine in the list.
The following two one-liners should produce the same output. Yet the first one stops
after the first machine in the list. Following the two one-liners, is the listing for
"select.bat" that each one-liner runs via "system".


type servers | perl -ne "print; chomp; $cmd=qq(select $_ master \"select \@\@version\"\n); system $cmd;"

perl -e "open S,'servers';while(<S>){print;chomp;$cmd=qq(select $_ master \"select \@\@version\");system $cmd;}"

================== select.bat ===================================================
@echo off
perl -x -S %0 %*
goto endofperl
#!perl
use DBI;
use strict; use warnings;
die "usage is select <server> <database> <select statement>\n" if $#ARGV < 2;
my ($srvr, $db, $sql) = @ARGV;
my $dsn = "driver=SQL Server;server=$srvr;database=$db;trusted_connection=Yes";
my $dbh = DBI->connect("dbi:ODBC:$dsn", '', '');
my $sh = $dbh->prepare($sql);
$sh->execute();
print join "|", @{ $sh->{NAME} }, "\n";
while (my @row = $sh->fetchrow_array) {
print join "|", @row, "\n";
}
__END__
:endofperl
============== end of select.bat ==================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top