Name

SPVM::IO::File - File Stream

Description

IO::File class in SPVM represents a file stream.

Usage

use IO::File;

# Write a line
my $fh = IO::File->new("foo.txt", ">");
$fh->say("Hello");>

# Read lines
my $fh = IO::File->new("foo.txt", "<");
while (my $line = $fh->readline) {
  
}

Super Class

IO::Handle

Fields

FileStream

has FileStream : Sys::IO::FileStream;

A file stream associated with the file descriptoer FD.

InputLineNumber

has InputLineNumber : long;

The current line number. This value is incremented by "getline" method.

AutoFlush

has AutoFlush : protected byte;

A flag for auto flush.

This field expects a boolean value.

Class Methods

new

static method new : IO::File ($file_name : string = undef, $open_mode : string = undef);

Creates a new IO::File object.

And opens a file given the file name $file_name and the open mode $open_mode by calling "open" method.

And returns the new object.

If $file_name is not defined, a file is not opened.

Exceptions:

Exceptions thrown by "open" method could be thrown.

See Also:

See IO#open method if you find a method ported from Perl opendir function.

new_from_fd

static method new_from_fd : IO::File ($fd : int, $open_mode : string);

Creates a new IO::File object.

And opens a file given the file descriptor $fd and the open mode $open_mode by calling "fdopen" method.

And returns the new object.

Exceptions:

Exceptions thrown by "fdopen" method could be thrown.

open

method open : void ($file_name : string, $open_mode : string);

Opens a file given the file name $file_name and the open mode $open_mode.

This method calls Sys#open method.

"FileStream" field is set to the opened file stream.

"InputLineNumber" field is set to 0.

Exceptions:

The file name $file_name must be defined. Otherwise an exception is thrown.

The open mode $open_mode must be defined. Otherwise an exception is thrown.

If a file is already opened, an exception is thrown.

Instance Methods

input_line_number

method input_line_number : long ();

Returns the value of "InputLineNumber" field.

close

method close : void;

Closes the file stream "FileStream".

This method calls Sys::IO#fclose method.

"InputLineNumber" field is set to 0.

FD field is set to -1.

Exceptions:

Exceptions thrown by Sys::IO#fclose method could be thrown.

read

method read : int ($string : mutable string, $length : int = -1, $offset : int = 0);

Reads the length $length of data from the file stream "FileStream" and store it to the offset $offset position of the string $string.

This method calls Sys#read method.

Exceptions:

Exceptions thrown by Sys#read method could be thrown.

getc

method getc : int ();

Reads a character from the file stream "FileStream", and returns the line.

This method calls Sys#getc method with the file stream "FileStream".

Exceptions:

Exceptions thrown by Sys#getc method could be thrown.

getline

method getline : string ();

Reads a line from the file stream "FileStream", incrementes the input line number "InputLineNumber" by 1, and returns the line.

A line is the part that ends with \n or EOF.

If EOF has been reached, returns undef.

This method calls Sys#readline method with the file stream "FileStream".

Exceptions:

Exceptions thrown by Sys#readline method could be thrown.

getlines

method getlines : string ();

Reads all lines from the file stream "FileStream", joins them to a string, and returns it.

This method calls "getline" method repeatedly.

If the first character is EOF, returns an empty string "".

Exceptions:

Exceptions thrown by "getline" method could be thrown.

write

method write : int ($string : string, $length : int = -1, $offset : int = 0);

Writes the length $length from the offset $offset of the string $string to the file stream "FileStream".

This method calls Sys::IO#fwrite method.

If "AutoFlush" field is a true value, thie method calls "flush" method after the write operation.

Exceptions:

Exceptions thrown by Sys::IO#fwrite method could be thrown.

flush

method flush : void ();

Flushes the write buffer.

This method calls Sys::IO#fflush method.

Exceptions:

Exceptions thrown by Sys::IO#fflush method could be thrown.

error

method error : int ();

If the file stream "FileStream" reaches the end of file, returns 1, otherwise returns 0.

This method calls Sys::IO#ferror method with the file stream "FileStream".

Exceptions:

Exceptions thrown by Sys::IO#ferror method could be thrown.

clearerr

method clearerr : void ();

Clears the error satus of the file stream "FileStream".

This method calls Sys::IO#clearerr method with the file stream "FileStream".

Exceptions:

Exceptions thrown by Sys::IO#clearerr method could be thrown.

eof

method eof : int ();

If the file stream "FileStream" reaches the end of file, returns 1, otherwise returns 0.

This method calls Sys::IO#feof method with the file stream "FileStream".

Exceptions:

Exceptions thrown by Sys::IO#feof method could be thrown.

ungetc

method ungetc : int ($c : int);

Pushes the character $c back to the file stream "FileStream" and returns the character that is actually pushed.

This method calls Sys::IO#ungetc method with the file stream "FileStream".

Exceptions:

Exceptions thrown by Sys::IO#ungetc method could be thrown.

sync

method sync : void ();

Transfers all modified in-core data of the file referred to by the file descriptor FD to the disk device.

This method calls Sys::IO#fsync method with the file descriptor FD.

Exceptions:

Exceptions thrown by Sys::IO#fsync method could be thrown.

truncate

method truncate : void ($legnth : long);

Causes the regular file named by referenced by the file descriptor FD to be truncated to a size of precisely $legnth bytes.

This method calls Sys::IO#ftruncate method with the file descriptor FD.

Exceptions:

Exceptions thrown by Sys::IO#ftruncate method could be thrown.

fdopen

protected method fdopen : void ($fd : int, $open_mode : string);

Opens a file given the file descriptor $fd and the open mode $open_mode.

This method calls Sys#fdopen method.

"FileStream" field is set to the opened file stream.

"InputLineNumber" field is set to 0.

Exceptions:

The open mode $open_mode must be defined. Otherwise an exception is thrown.

If a file is already opened, an exception is thrown.

autoflush

method autoflush : int ();

Returns the value of "AutoFlush" field.

set_autoflush

method set_autoflush : void ($autoflush : int);

Sets "AutoFlush" field to $autoflush.

See Also

Porting

This class is a Perl's IO::File porting to SPVM.

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License