--- Rob <[EMAIL PROTECTED]> wrote: > Thanks; I noticed you removed the () associated with the my portion, is > there a good rule to follow as to when the () are necessary? > > Thanks again(your one of the guys whos post i always read ;^) > > > Change > > > > my($nameIn) = $ARGV[0]; > > > > To > > > > my $nameIn = $ARGV[0] || '';
Rob, I use the parentheses around the variables I'm declaring if I am declaring more than one variable or if I need to force things to be evaluated in list context. With this: my($nameIn) = $ARGV[0]; If I have a scalar on the right, I prefer a scalar on the left, without parentheses. If you try to do complicated things here, you can get unexpected results: C:\>perl -e "($x)=3,1,2;print $x" 3 C:\>perl -e "($x)=(3,1,2);print $x" 3 C:\>perl -e "$x=(3,1,2);print $x" 2 C:\>perl -e "$x=3,1,2;print $x" 3 Now, someone please correct me if I get this wrong as it's a bit tricky. For the first two examples above, by putting the parentheses around $x, you're forcing the evaluation of the right side in list context. This will set the scalars on the left to the corresponding scalars on the right: C:\>perl -e "($x,$y)=(3,1,2);print $y" 1 It's important to remember, though, that a list is not an array. A list is simply a list of items. An array is a container for a list. If you use a scalar on the left, you get different results depending upon whether or not you have an array or a list on the right. A list will evaluate it's arguments in turn and return the last value of the list. An array will return the number of elements in the list (and a group of values separated by a comma will have you quickly discover that the equals sign(=) binds tighter than the comma!): C:\>perl -e "$x=(7,1,2);print $x" 2 C:\>perl -e "@a=qw/7 1 2/;$x=@a;print $x" 3 C:\>perl -e "$x=7,1,2;print $x" 7 For that last item, we don't have a list or an array. It's just a group of constant values separated by the comma operator. Since the equals sign binds tighter, this is equivalent to: ($x=7),1,2 This is one of the reasons we like to turn warnings on: C:\>perl -we "$x=7,1,2;print $x" Useless use of a constant in void context at -e line 1. 7 That gives you a nice clue as to what's going on. What all of that means is, if I have a scalar on the right, I want a scalar on the left. Putting parentheses around the variable on the left doesn't hurt, but I try very hard to ensure that I'm not getting bitten in unexpected ways, so unless I am explicitly trying to force a different context (my $count = @array), I skip the parentheses. Of course, here's a bug that I got bit by early on in my Perl career: sub foo { my $arg = @_; .... } We have a scalar on the left and an array on the right. That sets $arg equal to the number of elements in @_. Better would be this: sub foo { my ( $arg ) = @_; .... } *That's* why I would use parens around the left variable (though if I have a single argument, I would use (my $arg = shift;). If I want several arguments: sub foo { my ($bar, $baz) = @_; .... } In short, your code was not broken by the parentheses. I could have left them on and it would have been fine. However, by being careful about stating the exact context we want, we can avoid subtle bugs in the future. Here's the fun one. Jeff 'japhy' Pinyan posted this at Perlmonks (http://www.perlmonks.org/index.pl?node_id=113866): @list = 1, 2, 3, 4; Have fun (and read the thread that snippet is in). Cheers, Curtis "Ovid" Poe ===== Senior Programmer Onsite! Technology (http://www.onsitetech.com/) "Ovid" on http://www.perlmonks.org/ __________________________________________________ Do You Yahoo!? Check out Yahoo! Shopping and Yahoo! Auctions for all of your unique holiday gifts! Buy at http://shopping.yahoo.com or bid at http://auctions.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]