Hi Nora, On Friday 21 May 2010 07:43:07 HACKER Nora wrote: > Hi Shlomi, > > Thanks for your reply. > > > From a cursory look at your program, it seems that you can just set up > > $mode > > as a global variable, and keep it at that instead of just passing it > > around. > > Alternatively, maybe you'd like to define a class and keep the mode > > variable > > there as a property of the class' instances. > > Obviously, my point wasn't clear enough, I'm sorry for that. The > variable in the code extract that I'm really bothering about is the > $sdb: It is needed in the SWITCH of a sub-function, but this construct > only works if I pre-declare the $sdb in the calling function backup(), > even if it isn't needed for a $mode eq "b" but only for "r".
Well, like Thomas said, you can simply omit passing it altogether and it will get assigned as undef(). > > I still have a (meta) question referring to your advice to set it up as > a global variable: I read that "Global variables are bad [...] > (http://perldoc.perl.org/perlfaq7.html) - so when is it ok to use global > variables and when not? So far, I tried to use as many private variables > as reasonable/possible, even if I had to pass them around between > (sub-)functions, sometimes even between to "levels". What the FAQ seems to mean is that global *package-scope* variables are usually bad - i.e the ones you declare using <<use vars (...)>> or <<our>>. However, I was referring to doing <<my $mode = shift(@...)>> as a lexical variable that is in the lexical "global" scope and is visible by several subroutines. Such variables are not too bad (at least not in Perl) and if you're looking for a cleaner solution, you can opt to define a class with objects. > > > > my $mode = ARGV[0]; > > > > That should be "my $mode = $ARGV[0];" or preferably "my $mode = > > shift(@ARGV);" > > or "my ($mode) = @ARGV;". > > Sorry, typo - I indeed use $ARGV[0]. OK, good. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Optimising Code for Speed - http://shlom.in/optimise God considered inflicting XSLT as the tenth plague of Egypt, but then decided against it because he thought it would be too evil. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/