http://qs321.pair.com?node_id=970415


in reply to Re: Understanding arguments to subroutines
in thread Understanding arguments to subroutines

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.

Replies are listed 'Best First'.
Re^3: Understanding arguments to subroutines
by cdarke (Prior) on May 14, 2012 at 12:45 UTC
    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.
Re^3: Understanding arguments to subroutines
by toolic (Bishop) on May 14, 2012 at 14:19 UTC
    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.

Re^3: Understanding arguments to subroutines
by Corion (Patriarch) on May 14, 2012 at 12:41 UTC

    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.

Re^3: Understanding arguments to subroutines
by DStaal (Chaplain) on May 14, 2012 at 15:11 UTC

    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.