Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: GetOpt::Long usage style

by demerphq (Chancellor)
on Mar 08, 2003 at 12:32 UTC ( #241374=note: print w/ replies, xml ) Need Help??


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



Comment on Re: Re: GetOpt::Long usage style
Select or Download Code
Replies are listed 'Best First'.
Re^3: GetOpt::Long usage style
by OneTrueDabe (Novice) on May 08, 2014 at 16:00 UTC

    I know this is ancient, but since it's high on the Google hit list, I wanted to mention one thing...

    This very cool trick does not work for @arrays. :-(

    This:

    use Getopt::Long; GetOptions( 'single=s' => \(my $single = ''), 'multi=s' => \(my @multi = ()), );
    does *NOT* DWIM. According to "perlref":
    As a special case, "\(@foo)" returns a list of references to the contents of @foo, not a reference to @foo itself.
    I tried all kinds of permutations, and the best I could come up with was:
    use Getopt::Long; GetOptions( 'single=s' => \(my $single = ''), 'multi=s' => do { use vars '@multi'; \@multi }, );
    which, isn't quite as clean, IMHO.

    (It *DOES*, however, keep the variable declarations and command-line options on the same line, at least, so you don't have to make changes in multiple places when adding new options. *shrug*)

      use Getopt::Long; GetOptions( 'single=s' => \(my $single = ''), 'multi=s' => \my @multi, );

      If you want to provide a non-empty default value, one way is:

      use Getopt::Long; GetOptions( 'single=s' => \(my $single = ''), 'multi=s' => grep { @$_ = ('a','b') } \my @multi, );

      - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2015-07-07 21:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls