Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
Convert Gnome2::Canvas::Pixbuf to Image::Magick Array
on Jul 22, 2008 at 23:16
0 replies by renegadex
This simple code will convert a Gnome2::Canvas::Pixbuf into a Image::Magick Array. Enjoy Perlmagick Programmers!
Keep FastCGI Processes Up and Running
on Jul 01, 2008 at 16:56
2 replies by SouthFulcrum
A little script that checks to see if a site is up based on the response code; a response of 500 executes a shell script that kills and restarts the FastCGI processes whereas a response of 404 restarts the Web server. Oh, and keeps a little log.
Burrows-Wheeler transform
on Jul 01, 2008 at 13:48
1 reply by shi
As told by wikipedia the one in the title is a transform useful when compressing data. I spent some minutes on writing these snippets for direct and inverse transformation. May any wise monk help shrinking the code, I'd be thankful and glad to see the results :)
Regex tester
on Jun 27, 2008 at 11:45
3 replies by oko1
This is something I made up for a quick tester of regexes against strings; since it remembers both, either one can be 'adjusted' as necessary. It shows not only whether the match succeeds but also anything that was captured by the memory parens. It works with stand-alone regexes, substitution expressions, and the 'tr' operator. It's not perfect - it's probably somewhat fragile - but it's worked well for me for several months now, happily parsing my regexes by the dozen. I hope others find it useful.
Portuguese code
on Jun 10, 2008 at 09:58
1 reply by smokemachine
Portuguese code...
Duplicate Test:: output
on Jun 04, 2008 at 16:49
2 replies by tye

I have some tests that include a lot of 'diagnostic' output so, by default, STDERR gets redirected to a file so I can easily go look at the details if there is a failure.

The "ok" vs "not ok" messages all need to go to STDOUT which either goes to my interactive session or to a test harness. But they would be extremely useful on STDERR to provide milestones in the detailed output. So I went looking for how to get them written to both STDOUT and STDERR.

I often find it at least mildly entertaining the rather convoluted OO-koolaidesque framework that most of the Test:: modules are now part of. I suspect that there is some way that I can define a subclass and convince Test::More to use it such that I can have "ok" message go to two different file handles. However, that wasn't obvious while the following hack was obvious to me.

Given that the functionality I wanted to replace was named "_print" (note the leading underscore), I also figured that overriding it in a subclass isn't officially sanctioned anyway. (Seems like something not unreasonable to want to override, tho.)

Replies noting the "proper" way to do something like this welcome. :)

Using Search::Dict on log files
on May 22, 2008 at 06:14
1 reply by stigpje
First perlmonks post. If you have very large log files or slow disk, or both... As long as the log file is ordered by datetime, you can use Search::Dict's binary search to find entries for datetimes. (this code has not been extensively tested)
Adding a new user to all groups where another user is
on May 12, 2008 at 05:48
1 reply by bronto
You just added a user to your UNIX system. You want him to belong to all groups where you belong. E.g.: let's say your user name is bronto and the new user is robin. Here we go.
List all modules and versions used by a program
on Apr 21, 2008 at 09:10
3 replies by tachyon-II
Sometimes things break because code that worked with certain versions of modules does not work with others. This short block just dumps all the modules in use, with their version numbers when a script exits. By doing it in an END block you get a realistic picture of what got loaded and it will still print if your code dies for whatever reason.
IP Iterator
on Apr 17, 2008 at 10:06
6 replies by camlet
Do you know when a new server or network device is added to your network? Does someone else let you know a week later that a new device needs to be monitored? Are you putting a list of IP's together in a text file and writing a for loop to ping each of these servers? How about an IP iterator that walks from a start IP to end end IP.

Converting CSV to tab-delimited
on Apr 14, 2008 at 09:48
3 replies by PhilHibbs
A colleague is having problems loading CSV (comma-separated variable length) data into Microsoft SSIS, so I told him I'd write a script to help him. I saw Text::CSV, but I don't want to have to go through module installation with him as he's a bit scared of Perl already. So I wrote this little script - and I didn't want to have to teach him about < and > on the command line, so the script automatically generates a file name based on the input with ".tab" on the end.

The rules are the standard Excel-style CSV rules, any embedded '"' characters get doubled up, and any value that contains a ',' character must have '"' characters delimiting the value, but other values don't need to have delimiters.

The code is a simple state machine processing one character at a time and storing two state variables based on whether an opening " has been detected and when a " has been encountered within a quoted value.


Doesn't handle newlines in quoted values

Playing with code found on Perlmonks
on Apr 13, 2008 at 12:10
2 replies by oko1

When I answer questions here, I often want to see exactly what kind of errors the posted code is going to throw - so I copy it, open a file in 'vi', paste it in, and (following a careful look at it to make sure that it's not going to do anything nasty to me) run it. The additional bits - e.g., adding on a Perl shebang and running 'chmod +x' on the file - are already shortcuts in my 'vi', but I thought that it would be nice to automate this part, at least. I've got "pmedit" linked to an icon on my Gnome toolbar, so all I have to do now is select the code and click the icon. The displayed file will already contain the code that I highlighted.

The following is a Bourne shell script, and requires 'Xdialog'. Please feel free to modify for other OSes and situations. Constructive comments are highly welcomed. :)

Simple perl virus PoC
on Apr 08, 2008 at 20:12
3 replies by cyb3rdemon
A very simple perl virus that I wanted to share. Copies itself to the end of every perl file in its directory that is not already infected; does nothing else. The code is obfuscated to make it harder to recognize (althought, it's not very hard for anyone who knows perl well).
[Win] Compare files between two directories (and subdir)
on Apr 08, 2008 at 10:36
3 replies by jeepj

I have written this script because I am maintaining a little set of Perl libraries and tools at work, and I'm modifying the installed files on my PC. To avoid erasing it with old versions from CVS, I have the CVS repository elsewhere, and this little script is giving me which file is different from the repository in my current installation

I know the code is not error proof, but it does the work, and it's all I need

Getopt::Long-based commandline argument replacement substitution expansion
on Apr 05, 2008 at 22:25
0 replies by jdporter

Modifies @ARGV by replacing a certain option (with or without argument) with something else. Uses Getopt::Long.

In my application, which takes a list of filespecs on the commandline, I replace -tar foo.tar with /tmp/tar/* after having extracted foo.tar into /tmp/tar/:

option_replacement( "tar=s", sub { system "tar xf $_[1] -C $tmpdir"; " +$tmpdir/*" } );
The first arg is the sort of thing you pass as an option spec to Getopt::Long's GetOption().
The second arg is a sub ref, the interface of which is exactly the same as a sub you'd pass as an option handler to GetOption(), except that it returns the list of strings to insert into @ARGV.

Log In?

What's my password?
Create A New User
[1nickt]: hm, what did I do wrong? No download link in a code secrtion of a node I just posted ...
[Corion]: I see a download link there?
[Corion]: (at least the curl stuff is in a download link)
[Corion]: Also, I think the OPs problem is on their intranet and not on the internet (also, they should likely use WWW::Mechanize instead, which knows about cookie)
[1nickt]: yes, could be.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2017-05-23 18:14 GMT
Find Nodes?
    Voting Booth?