Tag Archive: Perl



Now as a Sys Admin I always am installing VM’s, testing OS’s hardening and such. One issue I always find myself in is when I have done a fresh OS install I need to get Perl up to date and install Modules that are needed to run some of my scripts. Now I always find myself in CPAN installing modules and finding out that they are failing to Make, and after banging my head into a wall for a while, I came across this fix.


$ sudo apt-get build-essential autoconf automake libtool gdb

This installs all the necessary packages to make CPAN modules. Once you have these installed, open CPAN and go ahead and install all the modules you need to. Oh and this works on Deb. Ubuntu releases of Linux, so if you need to install these for Red Hat you can use Yum and find the compatible packages to help with running Make on CPAN. Simple and quick as that.

Advertisement

For all of you that enjoy working in Linux you may have run across a framework called Mason. Mason is a framework much like catalyst however I think it is much easier to use and less convoluted. It allows you to create objects to get a set data for a webpage. Now I know Perl has its drawbacks like any other language out there. What I do like about it though is that there is a vast library of modules out there that will do just about anything you want it to do.

With the addition of Mason framework, this allows you to create a more object-oriented site as well as create handlers to do some of the heavy lifting and leave Apache to do the HTML rendering. One thing I have used in conjunction with the Mason module, is a module called prototype. This allows you to add Ajax to your web page which gives you a rich user experience. Well I digress.

One thing i wanted to talk about here was how to add the handler to your apache configuration. This is fairly straight forward as any cgi handler is. One thing to make sure of is to have a directory (usually called data) for the caching of the objects to be placed. Another directory you would need to make sure of is to assign the component root so that Mason knows where to start looking for components when called.

Now one trick I found was that I usually create a sub folder in the web root for all my components to go to, but when I use this directory as my component root my AutoHandler does not work and assist with processing the pages correctly. So what is a AutoHandler? it is a page much like Application.cfm or .cfc is for ColdFusion to instantiate your application and set global, session and other variables. The AutoHandler allows you to set up your database connection object along with setup any processing that you would need to be done each time a page is called within the application.

So being that the AutoHandler needs to be run, one thing I have done was to set my component root equal to the root of my application. Now I believe you can add the AutoHandler to apache as the primary cgi handler much like you would add perl as a handler of .pl files. However for the sake of testing and flexibility I found that just using your root as the component root, works just as easy. Now I still use a subfolder to contain all my components, I just need to make sure in the path defined for the component, that I specify the folder it is in. not such a big deal to me.

Other than that, all you need is to set your PerlHandler equal to HTML::Mason::ApacheHandler and possibly use PerlAddVar to set global variables to be persistent. Below is a sample setup I have made for you to see a possible scenario of setting up Mason as the Perl web framework.


Because I am a linux guy I have grown to love the perl language. Now I know many of you despise the language, however it has proven to be worthy in my field many of times. Not only worthy to stand up to loads but also to be flexible in it’s implementation. Am I speaking a little Greek?, forgive me. I say this because Perl is notabely good at parsing data from files, but over the years many of modules and frameworks have made it worthy in the field of web development.

So why don’t we just use PHP, or some other language like ASP if we are doing web development?. Well I have just grown fond of  languages that can be compiled and run as a client application, but also work in web development, or even be a good scripting language for Network Admins. I know there are many other languages out there that supersede Perl, but I give it some credit for putting up a good fight. Anyways I have wondered off the path.

For all those who are new with Perl and/or Catalyst I thought I would give a little instructional on what the different action types there are for Catalyst subroutines. Catalyst is an MVC framework for Perl. The frameworks help to try and leverage some of the great things in web web development, as well as make development more structured and easier. Although I am not sure about the easier part only because I feel frameworks tend to convolute code a bit. Now for a language like Perl, I feel it helps brings things together and make it more Object Oriented.

In MVC your subroutines in your controllers have actions assigned to them. This tells the routine what to part of the path to start from. For example

If the URL was http://localhost/My/Controller/foo

    package MyApp::Controller::My::Controller;
    sub foo : Local() { }

Then Local tell the routine that “foo” is the name of the routine and everything after bar in the url is considered an argument. So if the URL was http://localhost/My/Controller/foo/bar, then “bar” would be an argument passed in. Now another less used action is Global.

    package MyApp::Controller::My::Controller;
    sub foo : Global() { }

For this example, if we use the same URL then “Global” tells the routine to start from the root. In this case the root would be http://localhost/. So if you used $c->request->arguments->[0] then 0 in the array would be “My” instead of “bar” like when we used Local.

All these actions are just a shortcut way of using the action “Path”.  Example would be,

    package MyApp::Controller::My::Controller;
    sub foo : Path { }
    is the same as and matches on http://localhost/My/Controller/foo
    package MyApp::Controller::My::Controller;
    sub foo : Global { }
    and the following example will match on http://localhost/foo/bar
    package MyApp::Controller::My::Controller;
    sub bar: Path('foo/bar') { }

This is something that I had to tear apart and figure out a little bit, so I thought I would share this tidbit. Obviously there is tons more about this to talk about, so feel free to ask questions and I will share.

%d bloggers like this: