GX::HTTP::Body::File

Module Version: 0.2000_01

NAME

GX::HTTP::Body::File - File-based HTTP message body class

SYNOPSIS

# Load the class
use GX::HTTP::Body::File;

# Create a new body object
$body = GX::HTTP::Body::File->new( '/tmp/body.content' );

# Add content
$body->add( "Hello world!" );

# Get an IO::File handle to read from
$handle = $body->open;

# Get an IO::File handle to write to
$handle = $body->open( '>' );

# Print the message body
$body->print_to( *STDOUT );

DESCRIPTION

This module provides the GX::HTTP::Body::File class which extends the GX::HTTP::Body class.

METHODS

Constructor

new

Returns a new GX::HTTP::Body::File object.

$body = GX::HTTP::Body::File->new( %attributes );
Attributes:
  • cleanup ( bool )

    A boolean flag indicating whether or not to delete the associated file when the body object is cleared or destroyed. False by default, but automatically set to true (unless specified otherwise) when a temporary body file is created. Ignored if the readonly flag is set to true.

  • file ( string )

    A path to the file that is used to store the message body. If omitted, a temporary file will be used instead.

  • readonly ( bool )

    If set to true, a GX::Exception will be raised when an attempt is made to add content to the body or to open the body file in write or append mode. Useful for preventing accidental modifications when sending static files.

Returns:
Exceptions:

Alternative syntax:

$body = GX::HTTP::Body::File->new( $file );
Arguments:
  • $file ( string ) [ optional ]
Returns:
Exceptions:

Public Methods

add

Adds the given content to the message body.

$body->add( @content );
Arguments:
  • @content ( scalars )

    • byte strings
    • references to byte strings
    • references to subroutines returning byte strings
    • IO::Handle objects / GLOB references to read() bytes from
Exceptions:

as_string

Returns the message body as a byte string.

$string = $body->as_string;
Returns:
  • $string ( byte string | undef )

cleanup

Returns / sets the cleanup flag.

$bool = $body->cleanup;
$bool = $body->cleanup( $bool );
Arguments:
  • $bool ( bool ) [ optional ]
Returns:
  • $bool ( bool )

Unless set explicitly, the cleanup flag is automatically set to true when a temporary body file is created.

clear

Clears the message body.

$body->clear;

Calling this method resets the cleanup flag, the readonly flag and the file attribute. Additionally, the associated file is deleted if the cleanup flag had been set to true and the file was not marked as read-only.

file

Returns / sets the path to the associated file.

$file = $body->file;
$file = $body->file( $file );
Arguments:
  • $file ( string ) [ optional ]
Returns:
  • $file ( string )

length

Returns the size of the message body in bytes.

$length = $body->length;
Returns:
  • $length ( integer )

open

Returns an opened IO::File handle for the associated file.

$handle = $body->open( $mode );
Arguments:
  • $mode ( string ) [ optional ]

    Supported modes: "<", ">" and ">>". Defaults to "<".

Returns:
Exceptions:

print

An alias for add().

$body->print( @content );

print_to

Prints the message body to the specified filehandle, returning true on success or false on failure.

$result = $body->print_to( $handle );
Arguments:
  • $handle ( IO::File object | typeglob | GLOB reference )
Returns:
  • $result ( bool )

readonly

Returns / sets the readonly flag.

$bool = $body->readonly;
$bool = $body->readonly( $bool );
Arguments:
  • $bool ( bool ) [ optional ]
Returns:
  • $bool ( bool )

AUTHOR

Jörg A. Uzarek <uzarek@runlevelnull.de>

COPYRIGHT AND LICENSE

Copyright © 2009-2011 Jörg A. Uzarek.

This module is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License Version 3 as published by the Free Software Foundation.