5.2. Standard objects and variables

The following is a list of Interchange Perl standard objects are:

$CGI

            <INPUT TYPE=hidden NAME=foo VALUE=bar>
            [perl]
                        return "Value of foo is $CGI->{foo}";
                [/perl]

Actually, you should not do that -- if someone sends you a value you should not output it willy-nilly for security reasons. Filter it first with the [filter] tag as accessed by the $Tag object:

            [perl]
                        my $val = $Tag->filter('encode_entities', $CGI->{foo});
                        return "Value of foo is $val";
                [/perl]

$CGI_array

            <INPUT TYPE=hidden NAME=foo VALUE='bar'>
            <INPUT TYPE=hidden NAME=foo VALUE='baz'>
            <% = "The values of foo are", join (' and ', @{$CGI_array->{'foo'}}) %>

$Carts

            {
                'main' => [
                            {
                                'code' => '00-0011',
                                'mv_ib' => 'products',
                                'quantity' => 1,
                                'size' => undef,
                                'color' => undef
                            },
                            {
                                'code' => '99-102',
                                'mv_ib' => 'products',
                                'quantity' => 2,
                                'size' => 'L',
                                'color' => 'BLUE'
                            }
                        ],
                'layaway' => [
                            {
                                'code' => '00-341',
                                'mv_ib' => 'products',
                                'quantity' => 1,
                                'size' => undef,
                                'color' => undef
                            }
                        ]
            }

$Config

    # Allow searching the User database this page only
    $Config->{NoSearch} =~ s/\buserdb\b//;

%Db

NOTE: This object is not present and the below will not work with [calc].

            my $db = $Db{products};
        
                # Key for a normal table with one primary key
                $key = 'foo';
        
                # Array reference key for a COMPOSITE_KEY table
                $composite_ary_key = ['foo','bar','buz'];
        
                # Alternate hash reference key for a COMPOSITE_KEY table
                $composite_hash_key = { key1 => 'foo', key2 => 'bar', key3 => 'buz'};
        
                # Alternate null-separated key for a COMPOSITE_KEY table
                $composite_nullsep_key = join "\0", 'foo','bar','buz';
        
                ### Any of the composite key types may be substitued
                ### when COMPOSITE_KEY table
        
            # access an element of the table
            $field = $db->field($key, $column);
        
            # set an element of the table
            $db->set_field($key, $column_name, $value);
        
            # atomic increment of an element of the table
            $db->inc_field($key, $column_name, 1);
        
            # Return a complete hash of the database row (minus the key)
            $hashref = $db->row_hash($key);
        
            # Return some fields from a row
            my @fields = qw/sku price description/;
            @values = $db->get_slice($key, \@fields);
        
            # Set some fields in a row (slice)
            my $key = 'os28004';
            my @fields = qw/price description/;
            my @values = (5.95, "Ergo Roller");
            $array_ref = $db->set_slice($key, \@fields, \@values);
        
            # Alternate way to set slice
            my $key = 'os28004';
            my %fields = ( price => 5.95, description => "Ergo Roller");
            $array_ref = $db->set_slice($key, \%fields);
        
            # Perform a SQL query, returning an array of arrays
            # (the equivalent of DBI $sth->fetchall_arrayref)
            $ary = $db->query($sql);
        
            # Same as above, except receive
            # hash reference of pointers to field positions and
            # array reference containing list of fields
            my $sql = 'select * from products';
            ($ary, $index_hash, $name_ary) = $db->query($sql);
            $fields_returned = join ",", @$name_ary;
            $pointer_to_price = $index_hash->{price};
        
            # Perform a SQL query, returning an array of hashes
            $ary = $db->query({ sql => $sql, hashref => 1 });
        
            # see if element of the table is numeric
            $is_numeric = $db->numeric($column_name);
        
            # Quote for SQL query purposes
            $quoted = $db->quote($value, $column_name);
        
            # Check configuration of the database
            $delimiter = $db->config('DELIMITER');
        
            # Find the names of the columns (not including the key)
            @columns = $db->columns();
            # Insert the key column name
            unshift @columns, $db->config('KEY');
        
            # See if a column is in the table
            $is_a_column = defined $db->test_column($column_name);
        
            # See if a row is in the table
            $is_present = $db->record_exists($key);
        
            # Create a subroutine to return a single column from the table
            $sub = $db->field_accessor($column);
            for (@keys) {
                push @values, $sub->($key);
            }
        
            # Create a subroutine to set a single column in the database
            $sub = $db->field_settor($column);
            for (@keys) {
                $sub->($key, $value);
            }
        
            # Create a subroutine to set a slice of the database
            $sub = $db->row_settor(@columns);
            for (@keys) {
                $sub->($key, @values);
            }
        
            # Return a complete array of the database (minus the key)
            @values = $db->row($key);
        
            # Delete a record/row from the table
            $db->delete_record($key);

%Sql

NOTE: This object is not present and the below will not work with [calc].

          [perl products]
            my $dbh = $Sql{products}
                or return "Database not shared.";
            my $sth = $dbh->prepare('select * from products')
                or return "Couldn't open database.";
            $sth->execute();
            my @record;
            while(@record = $sth->fetchrow()) {
                foo();
            }
            $sth = $dbh->prepare('select * from othertable')
                or return "Couldn't open database.";
            $sth->execute();
            while(@record = $sth->fetchrow()) {
                bar();
            }
          [/perl]

$DbSearch

            array    Returns a reference to an array of arrays (best)
            hash     Returns a reference to an array of hashes (slower)
            list     Returns a reference to an array of tab-delimited lines
            $DbSearch->{table} = $Db{foo};
        
            $search = {
        
                    mv_searchspec => 'Mona Lisa',
                    mv_search_field => [ 'title', 'artist', 'price' ],
                    mv_return_fields    => [ 'title' ]
        
                };
        
            my $ary = $DbSearch->array($search);
        
            if(! scalar @$ary) {
                return HTML "No match.\n";
            }
        
            for(@$ary) {

$Document

This is an object which will allow you to write and manipulate the output of your embedded Perl. For instance, you can emulate a non-parsed-header program with:

        [perl]
                $Document->hot(1);
                for(1 .. 20) {
                        $Document->write("Counting to $_...<br>");
                        $Document->write( " " x 4096);
                        $Tag->sleep(1);
                }
                $Document->write("Finished counting!");
                return;
        [/perl]

Note the write of 4096 spaces. Because Interchange's link program is parsed by default and your web server (and the link program) have buffers, you need to fill up the buffer to cause a write. You can do it without the extra padding if you set the link up as a non-parsed-header program -- see your web server documentation on how to do that.

There are several methods associated with $Document:

         HTML $foo;                     # Append $foo to the write buffer array
         $Document->write($foo);        # object call to append $foo to the write
                                        # buffer array
         $Document->insert($foo);       # Insert $foo to front of write buffer array
         $Document->header($foo, $opt); # Append $foo to page header
         $Document->send();             # Send write buffer array to output, done
                                        # automatically upon end of ASP, clears buffer
                                        # and invalidates $Document->header()
         $Document->hot(1);             # Cause writes to send immediately
         $Document->hot(0);             # Stop immediate send
         @ary = $Document->review();    # Place contents of write buffer in @ary
         $Document->replace(@ary)       # Replace contents of write buffer with @ary
         $ary_ref = $Document->ref();   # Return ref to output buffer

$Document->write($foo)

$Document->insert($foo)

            $Document->write("23");
            $Document->insert("1");
            $Document->send();
            $Document->write("23");
            $Document->write("1");
            $Document->send();

$Document->header($foo, $opt)

            $Document->header("Content-type: text/plain");
            $Document->header("Content-type: text/plain", { replace => 1 } );

$Document->hot($foo)

$Document->send()

$Document->review()

            @ary = $Document->review();

$Document->replace(@new)

$Document->ref()

            # Remove the first item in the write buffer
            my $ary_ref = $Document->ref();
            shift @$ary_ref;

HTML

            HTML $foo, $bar;
            $Document->write($foo, $bar);

$Items

$Scratch

           <% $Scratch->{foo} = 'bar'; %>
            [set foo]bar[/set]

$Session

            <%
                my $out = $Session->{browser};
                $Document->write($out);
            %>
            [data session browser]
            <%
                $Session->{source} = 'New_partner';
            %>

$Tag


IMPORTANT NOTE: If the tag will access a database that has not been previously opened, the table name must be passed in the ITL call. For example:

            [perl tables="products pricing"]
            [perl products pricing]
            [perl]
                my $user = $Session->{username};
                return $Tag->data('userdb', 'name', $user );
            [/perl]
            [data table=userdb column=name key="[data session username]"]
            # WRONG!!!
            $Tag->shipping-desc('upsg');
            # Right
            $Tag->shipping_desc('upsg');
            $Tag->data('products', 'title', '00-0011');
            my $opt = {
                            table   => 'products',
                            column  => 'title',
                            key     => '00-0011',
                        };
        
            $Tag->data( $opt );
            $Tag->item_list( {
                                'body' => "[item-code] [item-field title]",
                            });
            $Tag->item_list( { }, "[item-code] [item-field title]")

$Values

            <% $Document->write($Values->{foo}); %>
            [value foo]

&Log

            <%
                Log("error log entry");
            %>
            <%
                Log("\\error log entry without timestamp");
                Log('\another error log entry without timestamp');
                Log("error log entry with timestamp");
            %>