Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Get code ready for a loop (and a little RFC)

by Veltro (Hermit)
on Mar 14, 2020 at 10:43 UTC ( [id://11114249]=note: print w/replies, xml ) Need Help??


in reply to Get code ready for a loop (and a little RFC)

Hello Lady Aleena

Maybe you want to use the sub that you eventually create to be used by several programs...

There is a technique that I sometimes use to be able to use common functionality that is needed by several programs. And I don't want to repeat myself over and over so I put those in libraries that are commonly accessible. E.g. I have a simple "CommonFunctions.pm" file containing functions like "trim", "trimleft", "trimright". I will share this example how to achieve this because I think it might be usefull to you. I have added use examples below under __END__.

MySVG.pm

package MySVG ; use strict ; use warnings ; use Exporter ; use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS ) ; @ISA = qw( Exporter ); @EXPORT = ( ) ; @EXPORT_OK = qw( gen_y family_y ) ; %EXPORT_TAGS = ( SVGFunc => [qw( &gen_y &family_y )] ) ; sub gen_y { my $gen = shift; my $multiplier = $gen - 1; my $gen_y = 36 + ( 76 * $multiplier); return $gen_y; } sub family_y { my ($gen, $two_parents, $one_parent) = @_; my $multiplier = $gen - 1; my $modifier = $two_parents ? 0 : $one_parent ? 18 : 38; my $family_y = (-18 + (-76 * $multiplier)) + $modifier; return $family_y; } 1; __END__ use examples (which should always work in case MySVG is in the same lo +cation as your program): - use MySVG qw( :SVGFunc ) ; - use MySVG qw( gen_y ) ; Ways to make perl find the lib in case it is somewhere else: - Add location of MySVG to PATH environment variable - use the perl -Idirectory switch - In case only your current project uses it and the pm file is in say +"lib\MySVG\" and you are working on a file in lib\somewhere-else\: use File::Basename qw( dirname ) ; use lib dirname( dirname __FILE__ ) . '/MySVG' ;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-24 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found