Re: I need help with get-options
[email protected] (ToddAndMargo via perl6-users) Wed, 2 Apr 2025 14:12:50 -0700
| Newsgroups | perl.perl6.users |
|---|---|
| Message-ID | <[email protected]> |
On 4/2/25 8:22 AM, Bruce Gray wrote:
>> my $CommandLine = CommandLineClass.new{
>> help => False,
>> debug => False,
>> UNC_BackupPath => Q[\\192.168.240.10\MyDocsBackup\backup1],
>> rotates => 2,
>> ParentDir => "/"
>> };
>
> The problem is with the syntax of the `new`.
> You need parenthesis instead of curly braces.
> With that change, your code works as expected.
And I have it right on every other .new in my code.
Even my keeper how to has it right. Mumble, Mumble.
Question: should the compiler have caught this? Or
is there some other purpose for the .new{} syntax?
>
> For even DRYer code, you can flatten `%opts` directly into the `new`
> constructor, like so:
> # use lib 'C:/NtUtil', 'C:/NtUtil/p6lib'; # use this one on
> customer machines
> use Getopt::Long;
> class CommandLineClass is rw {
> has Bool $.help = False;
> has Bool $.debug = False;
> has Str $.UNC_BackupPath = Q[\
> \192.168.240.10\MyDocsBackup\backup1];
> has Int $.rotates = 2;
> has $.ParentDir = '/';
> }
> my %opts = get-options( 'help', 'debug', 'UNC_BackupPath=s',
> 'rotates=i', 'ParentDir=s' ).hash;
> my CommandLineClass $CommandLine .= new( |%opts );
> say $CommandLine.raku if $CommandLine.debug;
Sweet!
>
> --
> Hope this helps,
> Bruce Gray (Util of PerlMonks)
Very much so! Thank you!