Cheating at Perl
Perl code snippet cheat sheet.
Send files to recycle bin:
Parse out file path:
use File::Basename;
($name, $path, $suffix) = fileparse($item, '\.[^\.]*');
#where $item is file with fullpath (perhaps derived from path::class
($name, $path, $suffix) = fileparse($item, '\.[^\.]*');
#where $item is file with fullpath (perhaps derived from path::class
Split at last instance:
# Splitting at last instance of \
($a,$b) = $line =~ /(.*)\\(.*)/;
($a,$b) = $line =~ /(.*)\\(.*)/;
Split at first instance:
Open file for overwriting:
Open file for append writing:
Simple split:
Discard duplicate array entries
Simple array sort:
Simple directory scan for files:
Simple file copy:
ODBC SQL Query:
use DBI;
use DBD::ODBC;
$user="DBUSER";
$auth="DBPASSWORD";
$host="DBSERVER";
$start="DATABASE_NAME";
$dsn = "dbi:ODBC:Driver={SQL Server};Server=$host;Database=$start"; $dbh = DBI->connect($dsn, $user, $auth, { RaiseError => 1 });
#Example query below - simply lists databases in MS SQL
$sql = qq/SELECT name FROM sys.databases/;
$sth = $dbh->prepare($sql);
$sth->execute();
#Time to manipulate the return
while (@row= $sth->fetchrow_array) {
unshift (@row, $database);
#Print the data
print join("", @row), "\n";
#Push the data into an array
push (@databases, join("", @row), "\n");
}
use DBD::ODBC;
$user="DBUSER";
$auth="DBPASSWORD";
$host="DBSERVER";
$start="DATABASE_NAME";
$dsn = "dbi:ODBC:Driver={SQL Server};Server=$host;Database=$start"; $dbh = DBI->connect($dsn, $user, $auth, { RaiseError => 1 });
#Example query below - simply lists databases in MS SQL
$sql = qq/SELECT name FROM sys.databases/;
$sth = $dbh->prepare($sql);
$sth->execute();
#Time to manipulate the return
while (@row= $sth->fetchrow_array) {
unshift (@row, $database);
#Print the data
print join("", @row), "\n";
#Push the data into an array
push (@databases, join("", @row), "\n");
}
Remove leading whitepace:
$line =~ s/^\s+//;
Remove trailing whitespace:
$line =~ s/\s+$//;
Remove leading and trailing whitespace:
$line =~ s/^\s*(.*?)\s*$/\1/;
Replace multi whitespace with commas:
$line =~ s/\s+/,/g;
Date (including yesterday’s date)