Sponsored Content
Top Forums Shell Programming and Scripting Source functions from variable Post 302996642 by jim mcnamara on Friday 28th of April 2017 11:06:21 PM
Old 04-29-2017
Can I ask why you need to put functions into an "intermediate" container object? Doing what Don Cragun suggested is simpler and standard.

Your efforts will make code maintenance much harder. This may not apply to you, but don't confuse cool with good programming practice. The two are not always congruent.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use of functions

Hi my shell is tcsh can I have functions in my shell scripting? Is the below shell script correct. Can I have two functions and call one of them as required. ---------- echo "functions" f1 f1 () { echo "hello" } f2 () (1 Reply)
Discussion started by: amitrajvarma
1 Replies

2. UNIX for Dummies Questions & Answers

== vs -eq and functions

Hey I have a question.... what is the difference between using == vs -eq when testing in WHILE loops. I use the following test that only worked with == signs.... if why do i need == and not -eq? 2. I need to re-use some code in a couple places in this script. is functions my best... (5 Replies)
Discussion started by: danieldcc
5 Replies

3. Shell Programming and Scripting

Source environment variable in script

Hi, I construct a bash script to finish some tasks. One of the task is to set up some environment variables. There is already one script file to complete the environment variables setting work. I am not able to know the detail of how to set these variables. So, I may just need to call this... (4 Replies)
Discussion started by: gofortime
4 Replies

4. 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

5. UNIX for Dummies Questions & Answers

Overwrite a Source Script Variable Value

Hello All, How do i overwrite a sourced script variable value. Sourced Script: GEN_PARAM_LIST4=""$LOG_DIR"/dwh_GenerateXMLFile.lst" GEN_PARAM_LIST4_v2=""$LOG_DIR"/dwh_GenerateXMLFile.v2.lst" I am using below statement for replacing. Script2: &&... (1 Reply)
Discussion started by: Ariean
1 Replies

6. Shell Programming and Scripting

Share variable between functions

Hi, gtkdialog gui seperate buttons use seperate functions in their GUI. A variable (newFolderName) is being set in one function (funcA) and it needs to be called in another function (funcB). I thought using global variable (like in python) concept should work however it does not. ... (4 Replies)
Discussion started by: singhai.nish
4 Replies

7. Shell Programming and Scripting

Need help on Assigning a Array variable from Background Functions

I have a question on how can I assign a output of a function to a variable which is executed in background. Here is my example $ cat sample_program.sh #!/bin/ksh exec_func () { sleep 1 v=`expr $1 + 100` print $v } export OUT_ARR date for i in 1 2 do OUT_ARR=`exec_func $i` &... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

8. Shell Programming and Scripting

Local variable in functions (gawk)

Hi Everybody :) I need your help, because i know a local variable in a function for example k, it is different of other variable(with the same name k) this a global variable. Is that right? dgawk> run Starting program: 3238860128818202 3 4 7 11 12 13 17 22 23 32 35 37 41 48 49 55 63 ... (5 Replies)
Discussion started by: solaris21
5 Replies

9. Shell Programming and Scripting

Source functions from variable

so i found a way to do this. #!/bin/bash MYF=$(/home/jason/myfunctions) source <(printf '%s\n' "${MYF}") echo "${var1}" /home/jason/myfunctions is a script that outputs information..i.e. functions...etc that needs to be sourced. I have no control over /home/jason/myfunctions. i can... (3 Replies)
Discussion started by: SkySmart
3 Replies
Moose::Manual::BestPractices(3) 			User Contributed Perl Documentation			   Moose::Manual::BestPractices(3)

NAME
Moose::Manual::BestPractices - Get the most out of Moose VERSION
version 2.1202 RECOMMENDATIONS
Moose has a lot of features, and there's definitely more than one way to do it. However, we think that picking a subset of these features and using them consistently makes everyone's life easier. Of course, as with any list of "best practices", these are really just opinions. Feel free to ignore us. "namespace::autoclean" and immutabilize We recommend that you remove the Moose sugar and end your Moose class definitions by making your class immutable. package Person; use Moose; use namespace::autoclean; # extends, roles, attributes, etc. # methods __PACKAGE__->meta->make_immutable; 1; The "use namespace::autoclean" bit is simply good code hygiene, as it removes imported symbols from your class's namespace at the end of your package's compile cycle, including Moose keywords. Once the class has been built, these keywords are not needed. (This is preferred to placing "no Moose" at the end of your package). The "make_immutable" call allows Moose to speed up a lot of things, most notably object construction. The trade-off is that you can no longer change the class definition. Never override "new" Overriding "new" is a very bad practice. Instead, you should use a "BUILD" or "BUILDARGS" methods to do the same thing. When you override "new", Moose can no longer inline a constructor when your class is immutabilized. There are two good reasons to override "new". One, you are writing a MooseX extension that provides its own Moose::Object subclass and a subclass of Moose::Meta::Method::Constructor to inline the constructor. Two, you are subclassing a non-Moose parent. If you know how to do that, you know when to ignore this best practice ;) Always call the original/parent "BUILDARGS" If you "override" the "BUILDARGS" method in your class, make sure to play nice and call "super()" to handle cases you're not checking for explicitly. The default "BUILDARGS" method in Moose::Object handles both a list and hashref of named parameters correctly, and also checks for a non- hashref single argument. Provide defaults whenever possible, otherwise use "required" When your class provides defaults, this makes constructing new objects simpler. If you cannot provide a default, consider making the attribute "required". If you don't do either, an attribute can simply be left unset, increasing the complexity of your object, because it has more possible states that you or the user of your class must account for. Use "builder" instead of "default" most of the time Builders can be inherited, they have explicit names, and they're just plain cleaner. However, do use a default when the default is a non-reference, or when the default is simply an empty reference of some sort. Also, keep your builder methods private. Be "lazy" Lazy is good, and often solves initialization ordering problems. It's also good for deferring work that may never have to be done. Make your attributes "lazy" unless they're "required" or have trivial defaults. Consider keeping clearers and predicates private Does everyone really need to be able to clear an attribute? Probably not. Don't expose this functionality outside your class by default. Predicates are less problematic, but there's no reason to make your public API bigger than it has to be. Avoid "lazy_build" As described above, you rarely actually need a clearer or a predicate. "lazy_build" adds both to your public API, which exposes you to use cases that you must now test for. It's much better to avoid adding them until you really need them - use explicit "lazy" and "builder" options instead. Default to read-only, and consider keeping writers private Making attributes mutable just means more complexity to account for in your program. The alternative to mutable state is to encourage users of your class to simply make new objects as needed. If you must make an attribute read-write, consider making the writer a separate private method. Narrower APIs are easy to maintain, and mutable state is trouble. In order to declare such attributes, provide a private "writer" parameter: has pizza => ( is => 'ro', isa => 'Pizza', writer => '_pizza', ); Think twice before changing an attribute's type in a subclass Down this path lies great confusion. If the attribute is an object itself, at least make sure that it has the same interface as the type of object in the parent class. Don't use the "initializer" feature Don't know what we're talking about? That's fine. Use Moose::Meta::Attribute::Native traits instead of "auto_deref" The "auto_deref" feature is a bit troublesome. Directly exposing a complex attribute is ugly. Instead, consider using Moose::Meta::Attribute::Native traits to define an API that only exposes the necessary pieces of functionality. Always call "inner" in the most specific subclass When using "augment" and "inner", we recommend that you call "inner" in the most specific subclass of your hierarchy. This makes it possible to subclass further and extend the hierarchy without changing the parents. Namespace your types Use some sort of namespacing convention for type names. We recommend something like "MyApp::Type::Foo". We also recommend considering MooseX::Types. Do not coerce Moose built-ins directly If you define a coercion for a Moose built-in like "ArrayRef", this will affect every application in the Perl interpreter that uses this type. # very naughty! coerce 'ArrayRef' => from Str => via { [ split /,/ ] }; Instead, create a subtype and coerce that: subtype 'My::ArrayRef' => as 'ArrayRef'; coerce 'My::ArrayRef' => from 'Str' => via { [ split /,/ ] }; Do not coerce class names directly Just as with Moose built-in types, a class type is global for the entire interpreter. If you add a coercion for that class name, it can have magical side effects elsewhere: # also very naughty! coerce 'HTTP::Headers' => from 'HashRef' => via { HTTP::Headers->new( %{$_} ) }; Instead, we can create an "empty" subtype for the coercion: subtype 'My::HTTP::Headers' => as class_type('HTTP::Headers'); coerce 'My::HTTP::Headers' => from 'HashRef' => via { HTTP::Headers->new( %{$_} ) }; Use coercion instead of unions Consider using a type coercion instead of a type union. This was covered in Moose::Manual::Types. Define all your types in one module Define all your types and coercions in one module. This was also covered in Moose::Manual::Types. BENEFITS OF BEST PRACTICES
Following these practices has a number of benefits. It helps ensure that your code will play nice with others, making it more reusable and easier to extend. Following an accepted set of idioms will make maintenance easier, especially when someone else has to maintain your code. It will also make it easier to get support from other Moose users, since your code will be easier to digest quickly. Some of these practices are designed to help Moose do the right thing, especially when it comes to immutabilization. This means your code will be faster when immutabilized. Many of these practices also help get the most out of meta programming. If you used an overridden "new" to do type coercion by hand, rather than defining a real coercion, there is no introspectable metadata. This sort of thing is particularly problematic for MooseX extensions which rely on introspection to do the right thing. AUTHORS
o Stevan Little <stevan.little@iinteractive.com> o Dave Rolsky <autarch@urth.org> o Jesse Luehrs <doy@tozt.net> o Shawn M Moore <code@sartak.org> o XXXX XXX'XX (Yuval Kogman) <nothingmuch@woobling.org> o Karen Etheridge <ether@cpan.org> o Florian Ragwitz <rafl@debian.org> o Hans Dieter Pearcey <hdp@weftsoar.net> o Chris Prather <chris@prather.org> o Matt S Trout <mst@shadowcat.co.uk> COPYRIGHT AND LICENSE
This software is copyright (c) 2006 by Infinity Interactive, Inc.. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.18.2 2014-01-19 Moose::Manual::BestPractices(3)
All times are GMT -4. The time now is 02:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy