X



無駄なことをして遊ぼう
0001名無しさん@お腹いっぱい。
垢版 |
2001/05/31(木) 16:41ID:/ItWxlxM
#!/usr/bin/perl
use ostream;

cout   << "overloadで遊んでみる。" << endl
    << "Hello Perl world" << endl;

__END__
0002名無しさん@お腹いっぱい。
垢版 |
2001/05/31(木) 16:42ID:/ItWxlxM
# ostream.pm
package ostream;
use base Exporter;
use Carp;
use strict;

use overload
'<<' => \&print;

our @EXPORT = qw(cout endl);
use constant cout => bless {};
use constant endl => "\n";

sub print{
my $self = shift;
my $arg = shift;
shift and croak "syntax error";
CORE::print($arg);
return $self;
}
__END__
0003名無しさん@お腹いっぱい。
垢版 |
2001/05/31(木) 20:58ID:???
#ありゃ…ウケなかったか。
#まあ良いや一人で遊ぼう。
package IO::Stream;
use Carp;
use base 'Exporter';
@EXPORT = qw(endl $cout $cin setw);

use strict;
use overload
'<<' => \&output,
'>>' => \&input,
'""' => sub { ref $_[0] || $_[0] },
fallback=> 1,
;

use vars qw/$cout $cin/;
use constant endl => "\n";
$cout = IO::Stream->cout();
$cin = IO::Stream->cin();

sub new { return bless { type => shift }, ref $_[0] || $_[0]; }
sub cin { return bless { type => 'istream' }, ref $_[0] || $_[0]; }
sub cout{ return bless { type => 'ostream' }, ref $_[0] || $_[0]; }

sub input{
my $self = shift;
($_[1] or index($self->{type}, 'i'))
and croak "syntax error";

if(ref $_[0]){
$self->{bufsize} = ${ shift() };
}else{
if($self->{bufsize}){
$_[0] = substr scalar(<STDIN>), 0, $self->{bufsize};
}else{
$_[0] = <STDIN>;
}
}
return $self;
}
sub setw{
my $len = shift;
return \$len;
}

sub output{
my $self = shift;
my $arg = shift;
($_[0] or index($self->{type}, 'o'))
and croak "syntax error";
print($arg);
return $self;
}

1;
# new周辺が見苦しいが
# 纏めようとすると何故かoverloadがうまく働かないことがあるんだよなぁ…。
# 何でだろ。
0004名無しさん@お腹いっぱい。
垢版 |
2001/05/31(木) 21:09ID:???
どうも挙動がおかしい。
>>3でcoutを変数にしたのは、
>>2のコードで
#!/usr/bin/perl
use ostream;
exit;
cout << "test " << endl;
__END__
とやると妙なことになってしまう為。
変数だと結果はマトモだ。
0007 
垢版 |
2001/06/27(水) 23:55ID:???
難しいことやってるねぇ。
意味不明です・・・私には・・・
0034\ ◆G.kfKDDY
垢版 |
2001/08/12(日) 14:44ID:???
すっかり練習場と化している・・・

おいらもだ
0037 ◆PoWerxxg
垢版 |
2001/08/12(日) 15:12ID:???
             
0038 ◆PERLWCdo
垢版 |
2001/08/12(日) 15:12ID:???
            
0039 ◆Sexghack
垢版 |
2001/08/12(日) 15:13ID:???
                  
0046◆skperlJw
垢版 |
2001/08/12(日) 23:09ID:???
とりあえずパス解析成功風?
0051GIKO@ ◆L4g4RxYk
垢版 |
2001/08/13(月) 04:47ID:???
>>47
おひさそっす!

変わってるのか???  またしょぼマシンぶん回さないと ウトゥだ(-_-
0053名無しさん◇一行野郎
垢版 |
2001/08/13(月) 15:50ID:???
print chr hex for split chr++$_**++$_*++$_*++$_*--$_-++$_*++$_-++$_*$_+$_+$_/$_,
'68Z74Z74Z70Z3AZ2FZ2FZ63Z6FZ72Z6EZ2EZ32Z63Z68Z2EZ6EZ65Z74Z2FZ70Z68Z70Z2FZ69Z6EZ64Z65Z78Z32Z2EZ68Z74Z6DZ6C'
レスを投稿する


ニューススポーツなんでも実況