Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > assigning variable to a pipe

Reply
Thread Tools

assigning variable to a pipe

 
 
mike
Guest
Posts: n/a
 
      04-23-2004
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...
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      04-23-2004
mike <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
 
 
 
mike
Guest
Posts: n/a
 
      04-24-2004
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote in message news:<c6ajsd$gs2$(E-Mail Removed)-Berlin.DE>...
> mike <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > 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


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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-24-2004
mike <(E-Mail Removed)> wrote:

> 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.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
James Willmore
Guest
Posts: n/a
 
      04-24-2004
On Fri, 23 Apr 2004 22:55:25 -0700, mike wrote:
[ ... ]

> 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

 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      04-26-2004
James Willmore <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) ia.net>...
> On Fri, 23 Apr 2004 22:55:25 -0700, mike wrote:
> [ ... ]
>
> > 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.
 
Reply With Quote
 
James Willmore
Guest
Posts: n/a
 
      04-26-2004
On Mon, 26 Apr 2004 05:18:43 -0700, mike wrote:

> James Willmore <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) ia.net>...
>> On Fri, 23 Apr 2004 22:55:25 -0700, mike wrote:
>> [ ... ]
>>
>> > 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.


 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-05-2004
mike wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
[named pipe] i wanna know about validate of pipe handle of client lee, wonsun C++ 1 11-02-2004 04:29 AM
Re: Assigning contents within HTML tag to string variable. Ken Cox [Microsoft MVP] ASP .Net 2 08-07-2004 03:16 PM
Why does IO::Pipe::END generate an EXCEPT pipe message? lvirden@gmail.com Perl Misc 1 06-02-2004 02:17 PM



Advertisments