* Siegfried Heintze <[EMAIL PROTECTED]> [2005-05-06T01:02:59]
> I've been studying this sample code that someone gave to me in response to
> one of my earlier queries. Why is it not necessary to put a "&" in front of
> DBH in the statement DBH->prepare?
 
> sub DBH { [ ... ] }
> 
> my $sth = DBH->prepare ( qq { ... } );

Because, well... it isn't!  In Perl 5, it is more useful to start with
the assumption that sub calls don't need to be prefix with & and then
find the exceptions, rather than the reverse.  Do you have a specific
reason to think that a & should be required here?

Here are some of the reasons you'd need &:

        The subroutine has a prototype, and you want to circumvent it.
                sub foo($$) { ... }
                
                &foo(1);

        You want to pass @_ as the arguments to the called function.
                sub foo { &bar; } # passes the arguments to foo to bar
        
        You're calling a subroutine reference.
                $foo = sub { ... };
                &$foo;

If you don't need to use the &, avoid it.  It's noisy.

-- 
rjbs

Attachment: pgpJ6JjIWrO52.pgp
Description: PGP signature

Reply via email to