13.2 Accessing a Virtual File with Perl

Now that we have created a virtual file with a transformation template, accessing it is as simple as using basic open, close, read, and write functions.

Using the Perl script examples in the following sections, you can accomplish the following tasks:

13.2.1 Reading a Datastream

The following is a Perl script that will read a datastream:

#
#   Perl script that reads a datastream from a virtual file
#
# This script takes the name of the virtual file as the first parameter # and an optional datastream name as the second parameter. If the 
# datastream is not specified, the actual contents of the file are
# read, which means that this script cannot be used to read the default # datastream. However, a simple type command reads the default
# datastream with no problem. When using Perl, if you do not use the
# sysread and syswrite functions for reading and writing,
# the buffering can cause inconsistent results.

if (($#ARGV > 1) or ($#ARGV == -1)) 
{
   die "USAGE: readVirt.pl filename [datastream]\n";
}

open(FILE, "+<".$ARGV[0]) or die "Error opening $ARGV[0]";

if ($#ARGV == 0)
{
   $command = "<virtualIO><define></virtualIO>";
}
else
{
   $command = "<virtualIO><datastream name=\"".$ARGV[1]."\"></virtualIO>";
}
syswrite FILE, $command, length($command);

$len = 999;
while ( $len > 0)
{
   $len = sysread FILE, $buf, 1000;
   print("$buf");
}
close(FILE);

13.2.2 Writing a Datastream

Writing a datastream is similar to reading a datastream. The following is a Perl script that will read a datastream:

#
#   Perl script to write to a datastream in a virtual file
#
# This script takes as parameters the virtual file to be written to, a # file ontaining the data to write, and an optional datastream. If the # datastream is not given, the default datastream is used. Note the 
# same restrictions as with Reading a Datastream on the use of sysread
# and syswrite.

if (($#ARGV > 2) or ($#ARGV < 1)) 
{
    die "USAGE: writeVirt.pl virtfile inputfile [datastream]\n";
}
open(VIRTFILE, "+<".$ARGV[0]) or die "Error opening $ARGV[0]";
open(INFILE, "<".$ARGV[1]) or die "Error opening $ARGV[1]";

if ($#ARGV == 2)
{
    $command = "<virtualIO><datastream name=\"".$ARGV[2]."\"></virtualIO>";
    syswrite VIRTFILE, $command, length($command);
}

$len = 999;
while ( $len > 0)
{
    $len = sysread INFILE, $buf, 1000;
    print("$buf");
    syswrite VIRTFILE, $buf, length($buf)
}

close(INFILE);
close(VIRTFILE);

13.2.3 Writing a Command

The following is a Perl script that writes a command to a datastream:

#
#   Perl script to write a command to datastream in a virtual file
#
# The difference between the command and the normal read operation is
# that the read operation returns a result generated by completing the
# write operation.

if (($#ARGV > 2) or ($#ARGV < 1)) 
{
   die "USAGE: cmdVirt.pl virtfile [datastream]\n";
}

open(VIRTFILE, "+<".$ARGV[0]) or die "Error opening $ARGV[0]";

if ($#ARGV == 1)
{
   $command = "<virtualIO><datastream name=\"".$ARGV[1]."\"></virtualIO>";
   syswrite VIRTFILE, $command, length($command);
}

print("Enter command: ");
$buf = <STDIN>;
chomp($buf);
syswrite VIRTFILE, $buf, length($buf);
sysread VIRTFILE, $result, 1000;
print("$result");

close(VIRTFILE);