develooper Front page | perl.perl6.users | Postings from September 2017

rw optional parameters

Thread Next
From:
Norman Gaywood
Date:
September 16, 2017 11:32
Subject:
rw optional parameters
Message ID:
CAHJ-_U7HY3FnKJcR6-cny4=gmZQKWgtBEVUANy9JtWOKi_p4dQ@mail.gmail.com
I have this:
#!/usr/bin/perl6
use v6;
sub MAIN( :$debug = False, :$verbose = False  ) {
    $verbose = True if $debug;
    say "verbose={$verbose}, debug={$debug}";
}
$ ./tt.p6
verbose=False, debug=False
$ ./tt.p6 --verbose
verbose=True, debug=False
$ ./tt.p6 --debug
Cannot assign to a readonly variable ($verbose) or a value
  in sub MAIN at ./tt.p6 line 6
  in block <unit> at ./tt.p6 line 4

So $verbose is read-only. I think the compiler could have told me this a
compile time?

I try to fix it:
...
sub MAIN( :$debug = False, :$verbose is rw = False  ) {
...
$ ./tt.p6 --debug
===SORRY!=== Error while compiling /home/ngaywood/./tt.p6
Cannot use 'is rw' on optional parameter '$verbose'.
at /home/ngaywood/./tt.p6:4

Not sure what the neat way of doing this is.

-- 
Norman Gaywood, Computer Systems Officer
School of Science and Technology
University of New England
Armidale NSW 2351, Australia

ngaywood@une.edu.au  http://turing.une.edu.au/~ngaywood
Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062

Please avoid sending me Word or Power Point attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About