XEDitor like ed, but in perl

some very very small bugs
but is working very fine
questions?
h - for help ;D
:eek: enourmous .. sorry

#!/usr/bin/perl
# xed Prompt Text Editor Like ed
# by Sendai
# Don't Remove this header
# you can change the source but credits to me o/
# include 
#   cammelpop by myself (usefull no ?)
#   cammelpush by myself 

$popen=join(" ",@ARGV);
die "Usage :\n   ./xed ARCHIVENAME\n" unless $popen ne "";
while($prompt ne "q"){
print('>');
chop($prompt=<stdin>);
@v=split(//,$prompt);
$xor=$v[0]; # xor is the first char
@v=reverse(@v);
pop(@v);
@v=reverse(@v);
$inx=join("",@v);
$inx=int($inx);  #inx is the value
print "xed help menu
q  - quit
h  - this help menu
r  - read archive
rX - read archive line X
w  - write in EOF
wX - write in line X
d  - delete all lines
dX - delete line X
aX - add a line in X
f  - find a word in document
X is any integer value
" if($prompt eq "h") ;
	if($xor eq "r"){
	red($popen) if($inx==0);
	lred($popen,$inx) if($inx!=0);	
	}elsif($xor eq "w"){
	wrt($popen) if($inx==0);
	wrtx($popen,$inx) if($inx!=0);
	}elsif($xor eq "d"){
	dl($popen) if($inx==0);
	dlx($popen,$inx) if($inx!=0);
	}elsif($xor eq "a"){
	adx($popen,$inx) if($inx!=0);
	}elsif($xor eq "f"){
	fini($popen) if($inx==0);		
	}

}

#head on

sub fini{
$fl=@_[0];
print "Search>";
chop($sh=<stdin>);
open(FD,"< $fl")|| print "Can't open $fl to RDONLY\n";
@lines=<FD>;
close(FD);
$x=1;
foreach(@lines){
	if($_=~/$sh/){
	print "$x:$_";
	}
$x++;
}
print "\n";
}

sub adx{
$fl=@_[0];
$x=@_[1];
open(FD,"< $fl")|| print "Can't open $fl to RDONLY\n";
@lines=<FD>;
close(FD);
print "Text>";
$tx=<stdin>;
@lines=cammelpush(@lines,$x,$tx);
open(FD,"> $fl")|| print "Can't open $fl to WRONLY\n";
print FD @lines;
close(FD);
}

sub dlx{
$fl=@_[0];
$x=@_[1];
open(FD,"< $fl")|| print "Can't open $fl to RDONLY\n";
@lines=<FD>;
close(FD);
@lines=cammelpop(@lines,$x);
open(FD,"> $fl")|| print "Can't open $fl to WRONLY\n";
print FD @lines;
close(FD);
}

sub dl{
$fl=@_[0];
open(FD,"> $fl")|| print "Can't open $fl to WRONLY\n";
print FD "";
close(FD);
}

sub wrtx{
#ok
$x=pop; #line to put
$fl=pop; #file
open(FD,"< $fl")||print "Can't open $fl to RDONLY\n";
@lines=<FD>;
close(FD);
$y=$x-1;
print "$x:$lines[$y]";
print "NewText:";
$tx=<stdin>;
$lines[$y]=$tx;
open(FD,"> $fl")||print "Can't open $fl to WRONLY\n";
print FD @lines;
close(FD);
#open(FD,">> $fl");
#	foreach(@lines){
#	print FD "$_";
#	}
#close(FD);
}

sub wrt{
$fn=@_[0];
open(FD,">> $fn")|| print "Can't open $fn to WRT\n";
print "Text>";
$tx=<stdin>;
print FD "$tx";
close(FD);
}

sub lred{
#ok
$fn=@_[0];
$d=@_[1];
open(FD,"< $fn")|| print "Can't open $fn to RDONLY\n";
@lines=<FD>;
close(FD);
$d--;
print "$lines[$d]";

}

sub red{
#ok
$fn=@_[0];
open(FD,"< $fn")|| print "Can't open $fn to RDONLY\n";
@lines=<FD>;
close(FD);
$x=0;
foreach(@lines){
$x++;
print "$x:$_";

}
print "\n";
}

sub cammelpush{
#cammelpush(@array,$INTEGER,$value);
#push a VALUE in @array[$integer]
#by sendai
$vl=pop; #string
$cod=pop; #int
@ar=@_; #array
$as=push(@ar,"q");
pop(@ar);
if($as < $cod){
	print "Inexistent Line :$cod";
}else{
	#breack array in $cod
	while($cod != $as){ 
	$s=pop(@ar);
	push(@void,$s);
	$as=push(@ar,"q");
	pop(@ar);
	}

push(@ar,$vl);
	while(@void){
	$s=pop(@void);
	push(@ar,$s);
	}
}

return @ar;
}


sub cammelpop{
#cammelpop(@ARRAY,$INTEGER);
$x=pop;
$x++;
@ar=@_;
$as=push(@ar,"q");
pop(@ar);
if($as < $x){
	print "Inexistent Line :$cod";
}else{
	#breack array in $cod
	while($x != $as){ 
	$s=pop(@ar);
	push(@void,$s);
	$as=push(@ar,"q");
	pop(@ar);
	}
pop(@ar);
	while(@void){
	$s=pop(@void);
	push(@ar,$s);
	}
return @ar;
}
}