Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Snippets Section

( #1967=superdoc: print w/replies, xml ) Need Help??

This section is now closed for posting code. Please use Cool Uses For Perl instead.

New Snippets
Using wxMemoryDC to draw on a jpeg
on Sep 26, 2009 at 21:26
0 replies by Steve_BZ
I found it hard to locate any easy examples of drawing on Bitmaps using wxPerl. Here is some working code that I put together to see if I could make it work. Just make sure you point the .jpg filename to one that exits on your machine, and you have wxPerl installed. Many thanks to Huub Peters for helping me with Mouse events and understanding DC.
output to STDOUT or a file
on Sep 04, 2009 at 11:03
1 reply by metaperl
apparently FileHandle is deprecated in favor of IO. Here is some code that allows you to write to either STDOUT or a file.
GTK2 Exif Thumbnail Reader
on Aug 31, 2009 at 08:51
2 replies by renegadex
This snippet shows how to get the thumbnail from exif metadata. This is very usefull for generating very fast thumbnails!! I hope many would benefit from this.
XP Perl Replacement for "Send to Clipboard as Filename"
on Aug 20, 2009 at 05:52
2 replies by Melly

For some reason, XP powertools is missing one of my favourites - the ability to right-click on a file, and send the path and name to the clipboard.

So, I added the following to a shortcut I called "Send to Clipboard" in my SendTo folder:

C:\Perl\bin\perl.exe -e "use Win32::Clipboard;Win32::Clipboard($ARGV[0 +]);"
un-Nifty qr OR ASCII bullet parser
on Aug 13, 2009 at 15:04
1 reply by belg4mit
While searching for a parse of ASCII bulleted lists the other day, I ended up rolling my own:

while( $text =~ /\G\s*($bullet)\s*([^\n]+)((?:\s*$bullet{2,}\s*[^\n]+)+)?/g ){ ...}

And stumbled across what I believed to be a clever use of qr to create $bullet from a separate data structure (which permits us to later determine what style of bullet we had, and map it to something):
on Jul 10, 2009 at 23:25
0 replies by Khen1950fx
I was having a problem with CPAN's "long-list" of modules, so I've been using this as a replacement. It will search for and return the names of all authors, all distributions, and all modules on CPAN.
move files to directories based on criteria
on Jul 10, 2009 at 14:20
2 replies by metaperl
We have a log directory full of files that we need to move out into different directories. I looked around for log file rotation utilities and found none. So I set out to write something. I first looked at various File::* modules but found none of them as easy as plain old File::Find. For usage at your site, customize the functions find_root_dir and categorize.
One PDF file for a set of images
on Jun 17, 2009 at 01:57
0 replies by graff
I had to scan a bunch of paper documents (one image per page), and upload them all to a web site that would accept only one PDF file containing the whole set.

A few CPAN module can do this, but their man pages leave a lot to the imagination. After a dozen trial/error iterations, here's what worked for me, using PDF::Create (it's manual was much better than PDF::API2).

oneliner to get module version
on Jun 14, 2009 at 15:35
6 replies by sflitman
Sometimes it helps to have a quickie way to get the version from a module. This lets me do it from the command line.
adding singleton methods to objects
on Jun 04, 2009 at 09:28
1 reply by andal
This piece of code may serve as the base class for objects that need singleton methods (methods specific for this object and not for whole class). One can use it directly
my $obj = DynObject->new(id => 'MyObj', action => sub{print "hi\n";}); print $obj->id, "\n"; $obj->action();
or as base class
$obj = MyDyn->new(id => 'MyObj'); print $obj->id, "\n"; $obj->action(); package MyDyn; use base 'DynObject'; sub action { print "hi\n"; }
Copy files by date modified
on Jun 04, 2009 at 03:19
3 replies by svetho
Yesterday I came across a problem to sync files between two computers in a network, but only those that had been created or modified within the last 60 days. Unfortunately rsync doesn't come with such a functionality (or it doesn't seem to - maybe I've overlooked something). It could well be that I've gone to work in a more complicated manner than necessary but I couldn't come up with anything better. So I took the extra step to copy the files that need to be synchronised to a temporary directory, preserving the original directory structure. Then the files can be effortlessly synced with rsync. Maybe this will come in handy for somebody else.
Change Image File Timestamp to Match EXIF Data
on Jun 01, 2009 at 03:10
6 replies by shoness
I catalog images by the date each was taken. My camera timestamps its images with the date/time. Perfect!

However, when I later "modify" an image, or when I transfer it over BlueTooth, etc. the "Date Modified" for the file changes.

I use this little script to change the atime/mtime for the image file BACK to where it should be.

All the action is inside Image::ExifTool, nevertheless, I welcome your feedback!

UPDATE: Added error checking.

multiple file operations using perl one-liners
on May 17, 2009 at 15:12
3 replies by sflitman
Sometimes you have a really repetitive task to do on a bunch of files. Sound familiar? You first need to specify the files, then you need to do the same operation on each one. The shell's traditional pipeline can nicely collect the paths, and then use the perl one-liner to do the operation.

Real life example: I needed to identify all the zipfiles in a directory tree and delete all *.exe files hiding inside the zipfiles.

First collect the files:

du -b */*/ | sort -n | xcol 1 >oldfiles
This shows all the zipfiles that were in subdirectories of subdirectories, then sorts them for size so I can decide to leave out the small ones. xcol is a perl script I posted to Code section to extract the column of filenames to file oldfiles.

Then use this perl one-liner:

perl -e 'while (<>) { chop; system qq{zip -d "$_" "*exe*"} }' oldfiles

Love that Perl!


Net::Syslog Patch
on Apr 29, 2009 at 15:36
0 replies by VinsWorldcom
I've been doing a little work coding a Syslog message receiver and needed a Syslog message sender to test with. I used Net::Syslog and it did the trick but it is a bit "lean". I updated it to generate Syslog messages according to RFC 3164 format. This is the patch.

Updated: 12-Jun-2009


torf or Mindy?
on Apr 29, 2009 at 11:11
1 reply by metaperl
Well, oftentimes when pulling data from somewhere, you will need to humanize booleans... not everyone is a Mork from Ork!
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2023-12-01 06:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found