dreman has asked for the wisdom of the Perl Monks concerning the following question:

Would someone please help me understand this code. I'm trying to use and only know little bit about FIFO within perl. I'm trying to use pipe command to bulk copy data (bcp) out of Sybase server. Need to understand this code and how it ends. Thank for responding.
while ( 1 ) { open(FIFO, " > $ENV{HOME}/.plan") or die "Couldn't open $ENV{HOME}/.plan for writing:$!\n"; print FIFO "The current time is", scalar(localtime), "\n"; close FIFO; sleep 1; }

Edit Masem 2001-08-17 - Code tags
Edit kudra, 2001-08-28 Extended title from 'Understanding Code'

Replies are listed 'Best First'.
Re: Understanding Code
by dragonchild (Archbishop) on Aug 17, 2001 at 18:52 UTC
    while (1) { open(FIFO, ">$ENV{HOME}/.plan") or die "Couldn't open $ENV{HOME}/.plan for writing:$!\n"; print FIFO "The current time is", scalar(localtime), "\n"; close FIFO; sleep 1; }
    What this code does is write the time to a file called ".plan" in your home directory. It does this every second and will overwrite the previous file. It will end only when the script cannot open the file for writing. Or, just like any other Unix process (and I know it's Unix cause of the usage of $ENV), it can be kill'ed from the commandline.

    Now, this isn't an example of FIFO or piping or anything of the sort. It's just a plain old write-to-file script. The 'pipe command' you refer to is probably a commandline thing in Unix.

    Or, alternately, it could refer to opening a datastream which is the result of a command, along the lines of open(LS_PIPE, "ls -F|"); That allows you to read from LS_PIPE the results of 'ls -F', as if it was any other file or socket.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Re: Understanding Code
by busunsl (Vicar) on Aug 20, 2001 at 10:17 UTC
    You cannot use the 'pipe command' to bcp data out of a sybase server.
    You have to use a named pipe.

    Do something like this:

    Create a named pipe:

    mknod /tmp/bcp-pipe p
    Start a bcp-session:
    bcp mydb..mytable out /tmp/bcp-pipe -c -Uuser -Sserver
    And then you can read from that pipe in your perl program:

    open FIFO, '/tmp/bcp-pipe' or die "cannot open /tmp/bcp-pipe: $!\n"; while (<FIFO>) { print "bcp-data: $_"; } close FIFO;
      Thanks for your information, one more question. If I was attempting to complete this task on Window NT, would it be possible creating a pipe and using it. Thanks. Dre Create a named pipe: mknod /tmp/bcp-pipe p Start a bcp-session: bcp mydb..mytable out /tmp/bcp-pipe -c -Uuser -Sserver And then you can read from that pipe in your perl program: open FIFO, '/tmp/bcp-pipe' or die "cannot open /tmp/bcp-pipe: $!\n"; while (<FIFO>) { print "bcp-data: $_"; } close FIFO;
        What I wrote works on *nix.
        I don't know, if you can create named pipes in NT, I've never heard of it.