in reply to Re: GetOpt::Long usage style
in thread GetOpt::Long usage style
\(my $verbose='');
my returns what has been declared. (Although the behaviour of it as a function is a little interesting, I dont think it can be prototyped, and I can't find complete documentation for it off hand.) The parenthesis required because the precedence of the assignment operator causes
GetOptions(x=>my $x=1,y=>my $y);
to be parsed something like
GetOptions('x',(my $x=1,y=>my $y));
which is a syntax error. They aren't required when the variable isn't initialized as part of the my. Also I personally think that they add visual calrity when the reference to the variable is taken. Since the my returns the variable declared (as an lvalue) we can take a reference to it with \. The reference operator \ can technically go inside the parens or outside in this case as
\($x,$y,$z)
is shorthand for
(\$x,\$y,\$z)
Hope that clarifys things. ;-) Er, and yes. You analysed it correctly. :-)
A couple of related tricks are:
my $txt="Foo bar"; (my $clean=$txt)=~s/oo/u/g; open my $fh,"File" or die "File $!"; if (my ($x,$y,$z)=/A(..)(..)(..)Z/) { }
---
demerphq
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: GetOpt::Long usage style
by OneTrueDabe (Acolyte) on May 08, 2014 at 16:00 UTC | |
by tye (Sage) on May 09, 2014 at 00:37 UTC | |
by demerphq (Chancellor) on Apr 11, 2016 at 17:22 UTC |