Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #157 from dagurval/IO-spurt
Added spurt(IO::Handle... support
  • Loading branch information
labster committed May 22, 2013
2 parents 267c54c + 5fa040f commit a50c2ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
55 changes: 43 additions & 12 deletions src/core/IO.pm
Expand Up @@ -208,6 +208,32 @@ my class IO::Handle does IO::FileTestable {
}


proto method spurt(|) { * }
multi method spurt(Cool $contents,
:encoding(:$enc) = 'utf8',
:$createonly, :$append) {

fail("File '" ~ self.path ~ "' already exists, but :createonly was give to spurt")
if $createonly && self.e;

my $mode = $append ?? :a !! :w;
self.open(:$enc, |$mode);
self.print($contents);
self.close;
}

multi method spurt(Buf $contents,
:$createonly,
:$append) {
fail("File '" ~ self.path ~ "' already exists, but :createonly was give to spurt")
if $createonly && self.e;

my $mode = $append ?? :a !! :w;
self.open(:bin, |$mode);
self.write($contents);
self.close;
}

# not spec'd
method copy($dest) {
try {
Expand Down Expand Up @@ -445,28 +471,33 @@ multi sub slurp(IO::Handle $io = $*ARGFILES) {
}

proto sub spurt(|) { * }
multi sub spurt(IO::Handle $fh,
Cool $contents,
:encoding(:$enc) = 'utf8',
:$createonly,
:$append) {
$fh.spurt($contents, :$enc, :$createonly, :$append);
}
multi sub spurt(IO::Handle $fh,
Buf $contents,
:$createonly,
:$append) {
$fh.spurt($contents, :$createonly, :$append);
}

multi sub spurt(Cool $filename,
Cool $contents,
:encoding(:$enc) = 'utf8',
:$createonly,
:$append) {
fail("File '$filename' already exists, but :createonly was give to spurt")
if $createonly && $filename.IO.e;
my $mode = $append ?? :a !! :w;
my $fh = open($filename.Str, :$enc, |$mode);
$fh.print($contents);
$fh.close;
$filename.IO.spurt($contents, :$enc, :$createonly, :$append);
}

multi sub spurt(Cool $filename,
Buf $contents,
:$createonly,
:$append) {
fail("File '$filename' already exists, but :createonly was give to spurt")
if $createonly && $filename.IO.e;
my $mode = $append ?? :a !! :w;
my $fh = open($filename.Str, :bin, |$mode);
$fh.write($contents);
$fh.close;
$filename.IO.spurt($contents, :$createonly, :$append);
}

proto sub cwd(|) { * }
Expand Down
1 change: 1 addition & 0 deletions t/spectest.data
Expand Up @@ -619,6 +619,7 @@ S32-io/io-path.t
S32-io/IO-Socket-INET.t
S32-io/note.t
S32-io/slurp.t
S32-io/spurt.t
S32-list/categorize.t
S32-list/classify.t
S32-list/create.t
Expand Down

0 comments on commit a50c2ef

Please sign in to comment.