bug-gnupod
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnupod] mktunes.pl creates corrupt iTunesDB ?


From: Jacinta Richardson
Subject: Re: [Bug-gnupod] mktunes.pl creates corrupt iTunesDB ?
Date: Thu, 02 Jul 2009 10:30:04 +1000
User-agent: Icedove 1.5.0.14eol (X11/20080724)

H. Langos wrote:

> Speaking of readability and code style. Is there a consensus among the
> experienced and community-minded folks in regard to the passing of 
> parameters to subs? Especially when writing subs in modules?
> 
> I nowadays prefer to pass parameters as one hashref
>   subname({foo => "x", bar => "y"});
> instead of as a naked list:
>   subname("x", "y");
> or (heaven help)
>   subname(foo => "x", bar => "y");
> which looks like a hash but is only a list.
> 
> I took a look at perlstyle(1) and perlsub(1) but I didn't see any hints in
> regard to this. 

This practice is called named arguments and is a great way of calling
subroutines because it removes the confusion of which order the arguments need
to go in.  It also allows you to set and use defaults, for example:

        subname({foo => "x", bar => "y"});
        subname({foo => "x"});
        subname({bar => "y"});
        subname();

        ...

        my %defaults = (
                foo => 'a',
                bar => 'b',
        };

        sub subname {
                my ($args_ref) = @_;

                state %defaults = (     # 5.10+ only, else "my %defaults"
                        foo => 'a',     # or a closure
                        bar => 'b',
                );

                $args_ref = { %defaults, %{$args_ref} };

                # Now you know for sure that "foo" and "bar" have values
                # for all the above calls.
                
        }

It's not discussed in perlstyle, and perlsub because the maintainers of those
files haven't really decided on a one-true way, or because they've been too lazy
to write it...  Damian Conway wrote bunch of good ideas about good Perl style in
his book called  Perl Best Practices, which is what most people point to when
they talk about Perl style now.  These practices have been turned into
requirements by the Perl::Critic system too, so you can get automated feedback
on your code quality by using the Perl::Critic programs.

Anyway, Damian advises:

        Use a hash of named arguments for any subroutine that has more than 3   
        parameters

and agrees entirely with your preference:

        subname({foo => "x", bar => "y"});

rather than the alternatives you give.

This has the advantage that mistakes in how you call the subroutine may be
spotted at compile time rather than run time:

        subname({foo => "x", bar => "y", cols => 1..4});

        Odd number of elements in anonymous hash at demo.pl line 2

He also says it's okay to mix positional and named arguments if you don't have
many but some are always mandatory.  For example:

        padded($string, {cols => 20, centered =>1, filler=>$SPACE});

        sub padded {
                my ($text, $args_ref) = @_;

                # pad the string.
        }

Hope this helps.

        J


-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | address@hidden |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




reply via email to

[Prev in Thread] Current Thread [Next in Thread]