assigning variable to a pipe

M

mike

hi

i have a piece of code like this

open CMD ,"| wc -l " or die "blah blah: $!\n";
print CMD "ls -l" ;

how can i assign a variable (or array ) to store the output of print
CMD "ls -l" statement??

thanks...
 
A

Anno Siegel

mike said:
hi

i have a piece of code like this

open CMD ,"| wc -l " or die "blah blah: $!\n";
print CMD "ls -l" ;

how can i assign a variable (or array ) to store the output of print
CMD "ls -l" statement??

That's "assigning a variable to a pipe"?

It looks like what you're trying to do would be easier accomplished with
a shell script. With Perl, you want IPC::Open2 for that.

Anno
 
M

mike

That's "assigning a variable to a pipe"?

It looks like what you're trying to do would be easier accomplished with
a shell script. With Perl, you want IPC::Open2 for that.

Anno

hi,thanks for the advise
i went back to try this out :

use IPC::Open2;
$pid = open2(\*Reader, \*Writer, "sql -Uuser -Sserver -Ppassword" );
print Writer qq(select * from this_table\ngo);
$body .= $_ while (<Reader>);
print "$body\n";

But it doesn't work. Would appreciate if anyone can help
thanks

PS:
The original code was
open SQL, "| sql -Uuser -Sserver -Ppassword " or die "Failed to open pipe: $!" ;
print SQL "select \* from this_table";
and the intention was to assign the output to a variable
 
T

Tad McClellan

mike said:
PS:
The original code was
open SQL, "| sql -Uuser -Sserver -Ppassword " or die "Failed to open pipe: $!" ;
print SQL "select \* from this_table";


Why are you shelling out instead of using DBI.pm?

Your problem does not exist if you use DBI to communicate with your DB.
 
J

James Willmore

]
PS:
The original code was
open SQL, "| sql -Uuser -Sserver -Ppassword " or die "Failed to open pipe: $!" ;
print SQL "select \* from this_table";
and the intention was to assign the output to a variable

Is there some reason why you're not using the DBI module to interact with
your database (http://search.cpan.org/~timb/DBI-1.42/DBI.pm)? I see from
your code example that's what you *really* want to do.

Just my $0.02
HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Arithmetic is being able to count up to twenty without taking
off your shoes. -- Mickey Mouse
 
M

mike

James Willmore said:
]
PS:
The original code was
open SQL, "| sql -Uuser -Sserver -Ppassword " or die "Failed to open pipe: $!" ;
print SQL "select \* from this_table";
and the intention was to assign the output to a variable

Is there some reason why you're not using the DBI module to interact with
your database (http://search.cpan.org/~timb/DBI-1.42/DBI.pm)? I see from
your code example that's what you *really* want to do.

Just my $0.02
HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Arithmetic is being able to count up to twenty without taking
off your shoes. -- Mickey Mouse


hi
i managed to solve the problem. Just need to close the handle "Writer" before
doing the reading. I did not use DBI.pm because i have trouble installing the
module. I am using Solaris and the perl version is 5.005. My C compiler is
not gcc but proprietary C package from Sun.
 
J

James Willmore

James Willmore said:
]
PS:
The original code was
open SQL, "| sql -Uuser -Sserver -Ppassword " or die "Failed to open pipe: $!" ;
print SQL "select \* from this_table";
and the intention was to assign the output to a variable

Is there some reason why you're not using the DBI module to interact with
your database (http://search.cpan.org/~timb/DBI-1.42/DBI.pm)? I see from
your code example that's what you *really* want to do.
[ ... ]
i managed to solve the problem. Just need to close the handle "Writer" before
doing the reading. I did not use DBI.pm because i have trouble installing the
module. I am using Solaris and the perl version is 5.005. My C compiler is
not gcc but proprietary C package from Sun.

http://sunfreeware.com/ :)

Latest version of Perl and gcc are available.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Artistic ventures highlighted. Rob a museum.
 
J

Joe Smith

mike said:
I did not use DBI.pm because i have trouble installing the
module. I am using Solaris and the perl version is 5.005. My C compiler is
not gcc but proprietary C package from Sun.

It is for that very reason that many people stop using /usr/perl5/bin/perl
for anything but Sun-provided startup scripts.

1) Download gcc from www.sunfreeware.com and install it in /usr/local.
2) Download newer perl from www.sunfreeware.com and install it in /usr/local.
3) rm /usr/bin/perl; ln -s /usr/local/bin/perl /usr/bin/perl

The above has no affect on scripts that explict use /usr/perl5/bin/perl.
-Joe
 

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,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top