File: Clever.pm

package info (click to toggle)
io-stringy 2.111-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 320 kB
  • sloc: perl: 1,427; makefile: 5
file content (36 lines) | stat: -rw-r--r-- 802 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package IO::Clever;
require 5.005_03;
use strict;
use vars qw($VERSION @ISA);
@ISA = qw(IO::String);
$VERSION = "1.01";

# ChangeLog:
# 1999-07-21-02:06:47 Uri Guttman told me a critical fix: 
#	$fp->input_record_separator is _Global_; local($/) is safer

my(%params);

sub new {
	my $class = shift;
	return IO::File->new(@_) unless $_[0] =~ /^>/;
	my $self = bless IO::String->new(), ref($class) || $class;
	$params{$self} = [ @_ ];
	$self;
}

sub DESTROY {
	my($self) = @_;
	my $filename = $params{$self}->[0];
	return unless $filename =~ s/^>//;
	my($new) = ${$self->string_ref};
	if (-f $filename) {
		my $fp = IO::File->new("<$filename") || die "$0: $filename: $!\n";
		local ($/);
		return if $new eq $fp->getline;
	}
	IO::File->new(@{$params{$self}})->print($new);
	delete $params{$self};
}

1;