narainhere has asked for the wisdom of the Perl Monks concerning the following question:

Hie Monks!! Now that I am a bit more than a novice in perl ,
I think I can change the way I </>CODE!!

In my work I very often write scripts with Logging.We have a Logging module which log's the process ID and the line number in script for every log message.It goes something like

my $log=MODLOG::LogIt->new(file name,process ID); $log->add("Header");
I have to write these 2 line of code and some more logging code in all the scripts.Is there a way create a template (which contains the above code) in simple way.I know require would serve this but I need to pass the file name and process ID dynamically during run time, So I am curios to know whether this can be achieved with templates.
--------Part2---------
Thanks for you replies, actually my intention was to get some starting idea on writing templates in perl.I thought an example would help others to rewrite it a template.Anything input on writing a simple template and using it would be highly appreciated

update:added Part2

The world is so big for any individual to conquer

Replies are listed 'Best First'.
Re: Templatize Repeated code
by GrandFather (Saint) on Sep 17, 2007 at 10:24 UTC

    You may find caller is of help. Consider:

    use strict; use warnings; Logit (); sub Logit { Log ("In Logit"); } sub Log { my $str =shift; my ($package, $filename, $line, $subroutine) = caller 1; print "$subroutine $filename ($line): $str\n" }

    Prints:

    main::Logit noname.pl (4): In Logit

    DWIM is Perl's answer to Gödel
Re: Templatize Repeated code
by graff (Chancellor) on Sep 17, 2007 at 12:20 UTC
    If you are asking for a way to reduce those two lines of code to something really short, my first reaction would be "don't bother -- those two lines are not so very much to add, and their purpose and effect are clear." I honestly don't think you would gain much by reducing it to something else.

    Of course, it would be handy for the "new()" method in the logging module to support a third parameter, allowing the "add('Header')" operation to be done as part of the object creation. Assuming it's an in-house module, that would be the approach that makes the most sense.

Re: Templatize Repeated code
by andreas1234567 (Vicar) on Sep 17, 2007 at 12:39 UTC
    Log::Log4perl is a very powerful log framework. You will still need 2-3 lines of code in each script to initialize it, but you can keep the log configuration in a single file for all your scripts.
    --
    Andreas
Re: Templatize Repeated code
by graff (Chancellor) on Sep 22, 2007 at 16:30 UTC
    Regarding your "Part2" update: I don't quite understand what you are looking for. A template for perl scripts? Why?

    Are you looking for a setup like: "Here's a template. Apply it with parameter set 'A' and it will produce perl script 'template_A.pl'; apply it with parameter set 'B' and it will produce perl script 'template_B.pl', ..." ?

    That strikes me as being not the best way to capture generalities (or provide a layer of abstraction) across some set of related programming tasks. A properly abstracted solution to a set of related problems would be a single script or module that uses an appropriate set of user-selectable parameters to control its behavior in different contexts, and/or to produce different outputs depending on the user's needs in a given instance.

    (Admittedly, "code factory" solutions do have their place, but I don't normally associate those with "templates". Of course, I don't use dynamic code generation all that often, so I'm probably not the best source for advice on that.)

    Maybe you should explain your actual goals in a bit more detail?

      I am also looking for an example on how we can templatize the repetitive code of perl scripts. A sample example can be great. Thanks!