assigning variable to a pipe

Discussion in 'Perl Misc' started by mike, Apr 23, 2004.

  1. mike

    mike Guest

    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...
     
    mike, Apr 23, 2004
    #1
    1. Advertising

  2. mike

    Anno Siegel Guest

    mike <> 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
     
    Anno Siegel, Apr 23, 2004
    #2
    1. Advertising

  3. mike

    mike Guest

    -berlin.de (Anno Siegel) wrote in message news:<c6ajsd$gs2$-Berlin.DE>...
    > mike <> 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
     
    mike, Apr 24, 2004
    #3
  4. 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";



    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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 24, 2004
    #4
  5. 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
     
    James Willmore, Apr 24, 2004
    #5
  6. mike

    mike Guest

    James Willmore <> wrote in message news:<>...
    > 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.
     
    mike, Apr 26, 2004
    #6
  7. On Mon, 26 Apr 2004 05:18:43 -0700, mike wrote:

    > James Willmore <> wrote in message news:<>...
    >> 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.
     
    James Willmore, Apr 26, 2004
    #7
  8. mike

    Joe Smith Guest

    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
     
    Joe Smith, May 5, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Cox [Microsoft MVP]

    Re: Assigning contents within HTML tag to string variable.

    Ken Cox [Microsoft MVP], Aug 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    900
    tonymjohn
    Aug 7, 2004
  2. RA
    Replies:
    4
    Views:
    4,661
  3. lee, wonsun
    Replies:
    1
    Views:
    498
    Jack Klein
    Nov 2, 2004
  4. Replies:
    1
    Views:
    237
    Ben Morrow
    Jun 2, 2004
  5. weston
    Replies:
    1
    Views:
    265
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page