I'm sure this is covered by a Perl Module some where, but I haven't gone looking. Instead I've written and the re-written a function that will convert a raw number of bytes to something more human friendly.
Below you can see the two subroutines I've written over time.
The old one only went up to Petabyte (which is already larger than we will accept for a file). While usable it just seems clunky compared to the new function.
My musings while programming in a number of languages and libraries, from PHP, Perl, JavaScript, jQuery, and KnockoutJS, with liberal use of CSS and Bootstrap to make it all pretty.
2014-06-10
2014-04-29
jQuery adding options to a select list
From time to time I find myself searching for the same solution to something I know I've already done. This is one of those things. I needed to add options to a select list with jQuery.
In the code below lines one and four are the old manual way I did things. If I needed the value or the text of the option to be a JavaScript variable, I had to start concatenating the strings. This is a bit easier with lines two and five.
In the code below lines one and four are the old manual way I did things. If I needed the value or the text of the option to be a JavaScript variable, I had to start concatenating the strings. This is a bit easier with lines two and five.
Also note, in the programmatic way if you need to insert HTML entities you have to use the html function instead of the text function.
Labels:
HTML,
JavaScript,
jQuery
Location:
Milwaukee, WI, USA
2014-03-11
Ubic: A polymorphic service manager
A few years ago I wrote a simple perl script that looks to a Database and processes any jobs. It runs in an instance of screen that I keep running on a server. The script is a "while (1){...;sleep(30);}" type script. I wanted the script to run more frequently than every minute and I wanted to be able to keep track of its progress. For some reason I didn't want to write output to a log file or check the DB to ensure jobs had run as expected.
I'd really like to move away from that screen'ed script and move it to something that starts when the server reboots / and is kept running should anything go wrong with it (shockingly nothing has killed it to date). My mentor at work recently showed us Ubic in a presentation for doing just this sort of thing. So I've spent the morning studying Ubic, along with some of the blog entries for creating a simple Ubic service.
The worker script:
The service description:
Some sample output in the log file:
As you can see, its a very simple setup to replicate multiple worker threads that take an indeterminate amount of time to complete their jobs.
My next task is to actually move my original script out of screen and into Ubic for better resiliency and tracking. The other task I have ahead of me is monitoring Ubic to ensure my scripts remain running. Remember to "Trust, but verify"!
I'd really like to move away from that screen'ed script and move it to something that starts when the server reboots / and is kept running should anything go wrong with it (shockingly nothing has killed it to date). My mentor at work recently showed us Ubic in a presentation for doing just this sort of thing. So I've spent the morning studying Ubic, along with some of the blog entries for creating a simple Ubic service.
The worker script:
The service description:
Some sample output in the log file:
As you can see, its a very simple setup to replicate multiple worker threads that take an indeterminate amount of time to complete their jobs.
My next task is to actually move my original script out of screen and into Ubic for better resiliency and tracking. The other task I have ahead of me is monitoring Ubic to ensure my scripts remain running. Remember to "Trust, but verify"!
Location:
Milwaukee, WI, USA
2014-03-10
CSS tag parsing in Perl
Just a quick one here, haven't played enough yet to fully expand on this.
Mojo::DOM
It let's you do CSS style tag parsing in Perl.
For example, where $content is an HTML document:
Mojo::DOM
It let's you do CSS style tag parsing in Perl.
For example, where $content is an HTML document:
2014-02-21
jQueryUI Tooltip, ignore certain elements
On one of my recent projects I needed to turn off the jQueryUI Tooltips for a certain element on the page. This was simple enough to complete, just initialize the tooltips as shown below:
The only bit I added is the ".not('#insertFeedbackSnippets *[title]')". Nice and simple!
The only bit I added is the ".not('#insertFeedbackSnippets *[title]')". Nice and simple!
Labels:
JavaScript,
jQuery,
jQueryUI
Location:
Milwaukee, WI, USA
2014-02-17
jQuery DataTables: How to get total rows
Given the DataTables initialization below:
I had to get the total number of rows to display elsewhere in the page. I didn't want to rely on "Showing X to Y of Z entries", as I wanted it much higher in the page.
The solution was extremely simple:
With that little bit of code I could show the user how many records I had for them before they saw the table.
Labels:
DataTables,
jQuery
Location:
Milwaukee, WI, USA
2014-02-13
JavaScript Array Prototypes, Clearing an Array in JavaScript and getting the last element from an Array in JavaScript
Recently I had to use these two prototypes for a project, and I don't wish for them to get lost in the sands of time.
Clearing an Array in JavaScript:
Getting the last element from an Array in JavaScript:
Clearing an Array in JavaScript:
Getting the last element from an Array in JavaScript:
Labels:
JavaScript,
JavaScript Prototypes,
jQuery
Location:
Milwaukee, WI, USA
2014-02-12
Quick reminder jQuery $.ajax calls, UTF8, and you!
Just a quick reminder, jQuery.ajax calls encode everything in UTF8, so if lets say you're saving that out to a JSON file for retrieval later you will need to decode UTF8 for proper display.
For example, this:
Should be:
For example, this:
Should be:
Location:
Milwaukee, WI, USA
2014-02-11
Bing Wallpaper Downloader
So I happened upon a simple Perl script that downloads the Bing wallpaper of the day and sets it as the active desktop background for GNOME. I'm working to modify this for Windows 7, and am currently stuck on how to set the active background in Windows 7.
Here is what I have so far:
Does any know how to set an image as the current desktop wallpaper in Windows 7? If so, please leave it in the comments!
Here is what I have so far:
Does any know how to set an image as the current desktop wallpaper in Windows 7? If so, please leave it in the comments!
Labels:
File::Copy,
File::Spec,
Perl,
Win32::TieRegistry,
Windows 7
Location:
Milwaukee, WI, USA
2014-02-08
Archive::Zip, setting compression level
One of my recent additions was to give users the ability to download files, but without manually downloading each file individually. I turned to Archive::Zip and it was simple enough to add the files.
But these I didn't want to waste server CPU resources on compressing these files, as they are likely already compressed (JPGs, MP3s, MP4s, etc.).
Final(ish) output:
But these I didn't want to waste server CPU resources on compressing these files, as they are likely already compressed (JPGs, MP3s, MP4s, etc.).
Final(ish) output:
2014-02-07
DataTables Footer Callback with Multiple Footer Rows
I've been working on updating our legacy product to be "Modernized". One of the pages had some simple calculations at the bottom of a table handled by the server. As part of the modernization effort I'm moving those calculations to the client side.
Since all of our tables utilize DataTables, this should be a very simple:
The original documentation shows adding the calculations to a single row in the tfoot, however, this would not work for me.
As you can see from above, I cheated and used jQuery set the values on each row explicitly.
Since all of our tables utilize DataTables, this should be a very simple:
The original documentation shows adding the calculations to a single row in the tfoot, however, this would not work for me.
As you can see from above, I cheated and used jQuery set the values on each row explicitly.
Labels:
DataTables,
JavaScript,
jQuery
Location:
Milwaukee, WI, USA
2014-02-06
Template Toolkit Multiple Filters (chaining filters)
This one is very simple, but not obvious as first...
I tried [% user.firstname FILTER lower,ucfirst %] but my output was lower only. The solution:
[% user.firstname FILTER lower FILTER ucfirst %].
There is also a short hand version:
[% user.firstname | lower | ucfirst %].
Note that filters are processed left to right so [% user.firstname FILTER ucfirst FILTER lower %] would produce only lowercase output.
I tried [% user.firstname FILTER lower,ucfirst %] but my output was lower only. The solution:
[% user.firstname FILTER lower FILTER ucfirst %].
There is also a short hand version:
[% user.firstname | lower | ucfirst %].
Note that filters are processed left to right so [% user.firstname FILTER ucfirst FILTER lower %] would produce only lowercase output.
Subscribe to:
Posts (Atom)