in reply to Question regarding variable scope

This is being a little picky, but I noted you used:

sub find_pairing($white_player){ ... }

in the code snippet. This prototype will work, but isn't strictly correct. It would be better to say:

sub find_pairing($) { ... }

I also noted that some of the replies used:

sub find_pairing() { my $white_player = shift; ... }

This is bad. If you call this method after this declaration, then a call like find_pairing("Bob") will fail because the prototype will accept no arguments. It would be better to say nothing about the signature of the subroutine like:

sub find_pairing { ... }

Anyway, you're all probably aware of this, but I wanted to make sure the casual browser, who might not, knows the difference between these slight variations. I look forward to Perl 6, where this cruft will be cleaned up.

Replies are listed 'Best First'.
Re: Re: Question regarding variable scope
by MarkM (Curate) on Oct 17, 2003 at 05:36 UTC

    Better yet, avoid the use of prototypes altogether in Perl. They are broken when it comes to actual programmer expectations. $ means "force scalar context" not "expect a scalar". Note that find_pairing(@list) does not generate a compile time error, but will generate perhaps-unexpected runtime behaviour.