my $real_h1 = $tree->look_down(
'_tag', 'h1',
sub {
my $link = $_[0]->look_down('_tag','a');
return 1 unless $link; # no link means it's fine
return 0 if $link->attr('href') =~ m{/dyna/}; # a link to there is bad
return 1; # otherwise okay
}
);
A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.
Jump to
Scanning HTML
Screen scraping with HTML::TreeBuilder:
C# SQL database connections
One way to do it, building the SQL code by inserting parameters:
Another way to do it, with bound parameters:
//get db connection to copy from
MySqlConnection sourceDbConn = getConnection(db);
sourceDbConn.Open();
try
{
System.Data.DataSet ds = new System.Data.DataSet();
MySqlDataAdapter da;
MySqlCommandBuilder cb;
da = new MySqlDataAdapter("SELECT id FROM page where pageType = '" + pageType + "'", sourceDbConn);
cb = new MySqlCommandBuilder(da);
ds.DataSetName = "pageIds";
da.Fill(ds);
foreach (DataTable thisTable in ds.Tables)
{
foreach (DataRow row in thisTable.Rows)
{
idsArray[i++] = Int32.Parse(row["id"].ToString());
}
}
return idsArray;
}
catch
{
throw new Exception("Failed to get list of page to publish");
}
finally
{
sourceDbConn.Close();
}
}
Another way to do it, with bound parameters:
// get feed changes - picking up all data required to create new feeds
MySqlCommand getFeeds = new MySqlCommand("select id, type, title, maxresults, minrelevance, mindate from feeds where client_id = ?client_id and updated_date > ?max_updated_date", editorialConn);
getFeeds.Parameters.AddWithValue("?client_id", selected_staging_client_id);
getFeeds.Parameters.AddWithValue("?max_updated_date", PublishChangesSince);
MySqlDataReader getFeedsRdr = getFeeds.ExecuteReader();
while (getFeedsRdr.Read())
{
Int32 FeedID = getFeedsRdr.GetInt32(0);
String FeedType = getFeedsRdr.GetString(1);
String FeedTitle = getFeedsRdr.GetString(2);
Int32 FeedMaxResults = getFeedsRdr.GetInt32(3);
float FeedMinRelevance = getFeedsRdr.GetFloat(4);
DateTime FeedMinDate = getFeedsRdr.GetDateTime(5);
}
getFeedsRdr.Close();
iTunes graphic equalizer
The iTunes graphic equalizer is hidden in the 'Window' menu, on the Mac verison.
Native Perl configuration file
Define a configuration file as a Perl package, containing hash data:
$ cat data
package d;
$a = {
1 => 2,
b => 'c',
};
Use this method to read the data straight into Perl:
$ perl -MData::Dumper -le'eval { require "./data"; }; print Dumper($d::a)'
$VAR1 = {
'1' => 2,
'b' => 'c'
};
This avoids the need to employ a parser, as the data is already in a Perl data structure format.
NOTE: You must specify the absolute path to the data file. I found that 'data' failed, where './data' succeeded.
If you leave out the package declaration in the data file, you can access the hash using $main::a
Subscribe to:
Posts (Atom)