Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Understanding arguments to subroutines

by uday_sagar (Scribe)
on May 14, 2012 at 12:28 UTC ( #970411=perlquestion: print w/replies, xml ) Need Help??

uday_sagar has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Here is a simple code, Which i am unable to interpret and understand.

if ($@ || $opt{'h'}) { &pod2usage(-verbose => 2) }

I couldnt understand the link between -verbose and the number 2 in "&pod2usage(-verbose => 2)"


Replies are listed 'Best First'.
Re: Understanding arguments to subroutines
by Corion (Pope) on May 14, 2012 at 12:31 UTC
      It seems like the doc is incomplete with regard to the dash character:
      The => operator is a synonym for the comma except that it causes its left operand to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores. This includes operands that might otherwise be interpreted as operators, constants, single number v-strings or function calls. If in doubt about this behavior, the left operand can be quoted explicitly.

      => treats the dash as a string, although the doc does not mention it:

      use warnings; use strict; use Data::Dumper; my %h = (-v => 5); print Dumper(\%h); print "$h{-v}\n"; __END__ $VAR1 = { '-v' => 5 }; 5

        I think you can explain it without a change to the documentaiton, and B::Deparse supports that:

        > perl -MO=Deparse -e "print for -verbose => 2" print $_ foreach (-'verbose', 2); -e syntax OK

        ... and the unary minus, when applied to a string, returns "-", prepended to that string, as documented in perlop for Symbolic Unary Operators.

      I know the use of fat comma, in the context of a hash, but here, hyphen (-) is also used with "verbose" which confuses me a bit.
        The hyphen is a bit of a throw-back to olden-times. This originally meant that the caller did not need to quote the keys although that has not been required since version 5.6 and that functionality passed to the fat comma. The hyphen is not stripped off by perl, it is passed in @_ so the subroutine has to remove it.
        Take a look at the source code perldoc -m Pod::Usage:
        sub pod2usage { local($_) = shift; my %opts; # ... ## Now determine default -exitval and -verbose values to use if ((! defined $opts{'-exitval'}) && (! defined $opts{'-verbose'}) +) { $opts{'-exitval'} = 2; $opts{'-verbose'} = 0; }

        -verbose is used verbatim as a hash key.

        You can ask Perl directly to show its interpretation:

        > perl -MO=Deparse -e "-verbose" -'verbose'; -e syntax OK

        The minus is an unary minus, also documented in perlop.

        It makes it easier to differentiate 'argument tags' from 'argument values', especially in mixed contexts. It doesn't necessarily mean anything to perl, but it means something to the subroutine.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://970411]
Approved by Corion
Front-paged by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2021-12-04 22:32 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (30 votes). Check out past polls.