Sponsored Content
Top Forums Shell Programming and Scripting Need help figuring out how to create sharable functions in Perl Post 302919709 by gandolf989 on Friday 3rd of October 2014 09:23:47 AM
Old 10-03-2014
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.

Thanks.

Code:
#!/usr/bin/perl

# Put on your nerd glasses and draw a square!

use strict;
use warnings;

package myPackage;

sub printSquare {

   my $balancex = 10; # width

   my $repeatx = $balancex; #don't change repeatx!!! use balancex instead.
   my $repeaty = 10; # height

   do{
      while($repeatx > 0){
         print ". "; # change the period to print another character, but keep the extra space.
         $repeatx -= 1;
      }

      print "\n";
      $repeatx += $balancex;
      $repeaty -= 1;
   }  until ($repeaty == 0); #corrected by Athanasius & AppleFritter

}

1;

Code:
#!/usr/bin/perl

use strict;
use warnings;

require myPackage;

myPackage::printSquare("printSquare");

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Figuring out Crons

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)
Discussion started by: eightgames
2 Replies

2. Shell Programming and Scripting

perl functions and arrays

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)
Discussion started by: mercuryshipzz
1 Replies

3. UNIX for Advanced & Expert Users

problems figuring out dns

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)
Discussion started by: nbredthauer
1 Replies

4. Shell Programming and Scripting

perl - passing hash references to functions

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)
Discussion started by: hcclnoodles
1 Replies

5. Shell Programming and Scripting

Perl script to find where functions is called in c

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)
Discussion started by: bojomojo
4 Replies

6. Shell Programming and Scripting

figuring out wildcards

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)
Discussion started by: dba_frog
2 Replies

7. Shell Programming and Scripting

figuring out an awk one liner

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)
Discussion started by: benalt
4 Replies

8. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

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)
Discussion started by: saku
8 Replies

9. Programming

Perl: How to load some functions automaticaly, starting Perl inteructively (with -d -e 0)?

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)
Discussion started by: alex_5161
1 Replies

10. Debian

Need help figuring out how to use my Debian Partitions

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)
Discussion started by: debianguy4
2 Replies
XMLRPC::Lite(3) 					User Contributed Perl Documentation					   XMLRPC::Lite(3)

NAME
XMLRPC::Lite - client and server implementation of XML-RPC protocol SYNOPSIS
Client use XMLRPC::Lite; print XMLRPC::Lite -> proxy('http://betty.userland.com/RPC2') -> call('examples.getStateStruct', {state1 => 12, state2 => 28}) -> result; CGI server use XMLRPC::Transport::HTTP; my $server = XMLRPC::Transport::HTTP::CGI -> dispatch_to('methodName') -> handle ; Daemon server use XMLRPC::Transport::HTTP; my $daemon = XMLRPC::Transport::HTTP::Daemon -> new (LocalPort => 80) -> dispatch_to('methodName') ; print "Contact to XMLRPC server at ", $daemon->url, " "; $daemon->handle; DESCRIPTION
XMLRPC::Lite is a Perl modules which provides a simple nterface to the XML-RPC protocol both on client and server side. Based on SOAP::Lite module, it gives you access to all features and transports available in that module. See t/26-xmlrpc.t for client examples and examples/XMLRPC/* for server implementations. DEPENDENCIES
SOAP::Lite SEE ALSO
SOAP::Lite CREDITS
The XML-RPC standard is Copyright (c) 1998-2001, UserLand Software, Inc. See <http://www.xmlrpc.com> for more information about the XML- RPC specification. COPYRIGHT
Copyright (C) 2000-2001 Paul Kulchenko. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. AUTHOR
Paul Kulchenko (paulclinger@yahoo.com) perl v5.12.1 2010-03-17 XMLRPC::Lite(3)
All times are GMT -4. The time now is 03:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy