Need help figuring out how to create sharable functions in Perl
I am somewhat new to Perl. I have Googled Perl one liners and worked with the MIME::Lite library to send emails with attachments. But I have not done any real Perl scripting. I need to write a script to install code for our application using an Oracle database with DBI, and to track in the application history table, what was installed and when it was installed. The scripts would then look to see what is already installed every time a patch is run and only install a module once.
Rather than just writing code and repeating a lot of code, I would rather start by building a library of common code that needs to get run and then using the right code when I need it.
I wrote some test code to work with a script that I found on the internet. I am looking for advice on how to make the code modular with reusing as little code as possible. Someone there should be a best practices when it comes to designing how the code should get stored and how to make it modular.
Also what is the difference between require "packageName" and use "packageName" and which one I should use.
Perl reusable code is all about separation of namespace code and careful management of variable scope.
Here are some thoughts on it.
require() function is like #include in C programming, but keeps the lexical scope, meaning that any variable with `my' will not leak to the code that imports it.
It compiles at run time, (compiles just when it about to be used) which has the possibility that it will not run because errors. Perl addresses the issue by requiring that the code imported must evaluate to true, thus the return of 1; at most modules
The syntax:
or
Packages separates subroutine into namespaces. Every subroutine in Perl lives inside a package, every file begins with implicit package main;
__PACKAGE__ will tell you were you are
The syntax:
or
In Perl, object oriented and reusable code programming is all about the scope of variables, and subroutines are variables of packages
is equivalent to:
use can only take a bareword, not a string.
A BEGIN code is code that runs at compile time, meaning that as soon as the compiler encounters a BEGIN block, Perl will compile it and run it before even the code that follows is compiled.
Last edited by Aia; 10-03-2014 at 12:28 PM..
Reason: grammar
Hi Aia. Your explanation helps. So can I use the following syntax to make my code aware of code from another file, "use myfile;" then I can call the subroutines from that file, passing in variables that are needed and getting back data? Do I need to specify the package name where I am calling the code from to call a method in the file being included with the use statement?
Do you have any recommendations on what I should look at online to get a clearer idea on how to structure my packages?
It appears to me that what you are craving is how to do object oriented code in Perl. Ultimately, that's what you need to know in order to make use of any code you will find in CPAN, effectively.
I was going to try to compose some examples, but then I thought I knew better not to do that and I got a site for you that without much introduction tries to give you enough material to ponder and further practice.
Greetings, I've installed my Debian Server over 4 months ago, I didn't quite understand what the paritions were for, but the server provider made my partitions. Anyway I was putting most of my files in /gserver and it ran otu of space quickly when in the store page it says 2tb I barely used 18gb... (2 Replies)
I would like to use Perl in 'interactive' mode (kind off), starting it by
> perl -d -e 0;
But I need to have some function be read on loading.
Also, it should not be for anyone who starting Perl or use it any how.
I did try to search, but not much result.
I have try a file '.perldb':... (1 Reply)
I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line
if
then... (8 Replies)
I have googled around a bit and could not find an answer to how this works:
echo $STRING | awk '$0=$NF' FS=
I know what each part is doing. The record is being set to equal the last field and the field separator is being set to null so that each character is considered a field. Why can FS= be... (4 Replies)
I'm trying to delete everything between ( and ) in a line, ie: ( start xxxx, end xxx ). there is uppercase, lowercase and numbers in the parans. and are of varied length.
I tried this:
sed 's/()//' infile > outfileI'm not understanding the wildcard use in brackets (2 Replies)
Hello,
I need to write a perl script to find where functions is called in c files..
The script should scan the file and find out the function names and then search to see where they are called...
Lets for now assume all functions are internal.
I don't know where to start :(
... (4 Replies)
hi there
I have the following script in which i have created a PrintHash() function.
I want to pass to this function the reference to a hash (in the final code i will be passing different hashes to this print function hence the need for a function). I am getting an error
Type of arg 1 to... (1 Reply)
Im on an OS X 10.4 Mac server running bind 9.3, I just replaced the entire network with cisco hardware, all machines including servers now have private ip addresses that t he firewall resolves. I need to have a dns that works for both internal and external connections. any help would be great! (1 Reply)
Hi,
First I will tell my objective of this function (function one).
I have a table for ex:
id passwd name
-- ------ -----
1 fdhgfs werwer
2 fsdfs sdfsdf
3 sdfs sdfsdf
4 fdsfs dssdf
. . .
.
.
.
The id, passwd and name are the arguments for another function say two. (1 Reply)
Hello,
I've built a news site using SimplePie to pull in a set of feeds and display them on a page. The caching is working but the problem is that the first initial load is slow. After that, you can hit refresh and it loads very quickly. I'd like to eliminate that first slow load by creating a... (2 Replies)