Fry-Lib-CDBI-Basic

 view release on metacpan or  search on metacpan

lib/Fry/Lib/CDBI/Basic.pm  view on Meta::CPAN

		
		for (@$array2) {$seen{$_}++}
		for (@$uniq) { $i++; do {push(@unique,$_);push(@num,$i) } if (! exists $seen{$_}) }
		return (\@unique,\@num);
	}
	sub file2array {
		shift;
		#local function
		#d:converts file to @ of lines
		open(FILE,"< $_[0]");
		my @lines; chomp(@lines = <FILE>);
		close FILE;
		return @lines;
	} 
	sub check_for_regex {
		#d: AoHregexp, could be used as an 'or' search on multiple columns
		my ($class,$regex,@records) = @_;
		my @unclean;

		for (@records) {
			for my $col (@{$class->Var('action_columns')}) {
				if ($_->$col =~ /$regex/) {
					push(@unclean,$_);
					last; #break?
				}
			}
		}
		return @unclean;
	}
	#internal methods
	sub _set_insert_col {
		my $cls = shift;
		#set insert_columns 
		my @insert_columns = @{$cls->Var('columns')};
		shift @insert_columns;
		$cls->setVar(insert_columns=>\@insert_columns);

	}	
	sub regexChangeAoH {
		my ($cls,$op,@records2update) = @_;
		for my $rec (@records2update) {
			for (my $j=0; $j < @{$cls->Var('action_columns')}; $j++) {
				my $col= $cls->Var('action_columns')->[$j];
				$_ = $rec->$col;
				eval $op; die($@) if $@;
				$rec->$col($_);
			}
		$rec->update;
		}
	}
	sub modify_file {	
		my ($cls,$tempfile) = @_;
		my $inp;

		system($cls->Var('editor') . " $tempfile");# or die "can't execute command as $<: $@";
		#?: why does this system always return a fail code
		#$cls->view("cdbi_update (y/n)? "); chomp($inp = <STDIN>);
		$inp = $cls->Rline->stdin("cdbi_update (y/n)?");
		return ($inp eq "y");
	}
	sub update_from_file {
		my ($cls,$tempfile,@records) = @_;

		my @lines = $cls->file2array($tempfile);

		#my $firstline = shift(@lines);
		#read column order from file
		#my @fields = split(/$updatedelim/,$firstline);
		#or not
		my @fields = @{$cls->Var('action_columns')};

		my $i;
		foreach (@records) {		#each row to update
			my @fvalues = split(/${\$cls->Var('field_delimiter')}/,$lines[$i]);
			for (my $j=0; $j < @fields; $j++) {		#each column to update

				my $temp=$fields[$j];
				$_->$temp($fvalues[$j]);		# this line = $_->$field($fieldvalue)
			}
			$_->update;
			#$_->dbi_commit if ($db = postgres
			$i++;
		}
	}
	sub col2f1 {
		#d: aliases column names with c and number
		my $class = shift;
		my @newterms;

		for (@_) { 
		#if (/c(\d+)=/) { my $col = $col[$1-1];s/c\d+/$col/} 
		if (/c([-,\d]+)(.*)/) { 
		my @tempcol = $class->sub->parseNum($1,@{$class->Var('columns')});
			for my $eachcol (@tempcol) {  
				push(@newterms,$eachcol.$2);
			}
		}
		else {push (@newterms,$_)}
		}
		return @newterms;
	}
#sub objects
	##print functions,input is objects
	sub printtofile {
		#d:prints rows to temporary file
		my ($cls,$tempfile,@records) =  @_;

		my $output = join($cls->Var('field_delimiter'),@{$cls->Var('action_columns')})."\n";
		$output .= $cls->View->objAoH_dt(\@records,$cls->Var('action_columns'));
		$cls->View->file($tempfile,$output);
	}
	sub printTextTable {
		my $cls = shift;
		$cls->print_text_table(\@_,$cls->Var('action_columns'));
	}
	sub print_text_table {
		my $cls = shift;
		my ($ref1,$ref2) = @_; my @row = @{$ref1}; my @columns = @{$ref2};
		my (@column_values,@longest);

		#defaul



( run in 1.832 second using v1.01-cache-2.11-cpan-39bf76dae61 )