Документация
HTML CSS PHP PERL другое

Section 9.1.  Call Syntax

 
Previous
Table of Contents
Next

9.1. Call Syntax

Call subroutines with parentheses but without a leading &.

It's possible to call a subroutine without parentheses, if it has already been declared in the current namespace:

    sub coerce;
    
    # and later...

    my $expected_count = coerce $input, $INTEGER, $ROUND_ZERO;

But that approach can quickly become much harder to understand:

    fix my $gaze, upon each %suspect;

More importantly, leaving off the parentheses on subroutines makes them harder to distinguish from builtins, and therefore increases the mental search space when the reader is confronted with either type of construct. Your code will be easier to read and understand if the subroutines always use parentheses and the built-in functions always don't:


    my $expected_count = coerce($input, $INTEGER, $ROUND_ZERO);

    fix(my $gaze, upon(each %suspect));

Some programmers still prefer to call a subroutine using the ancient Perl 4 syntax, with an ampersand before the subroutine name:

    &coerce($input, $INTEGER, $ROUND_ZERO);

    &fix(my $gaze, &upon(each %suspect));

Perl 5 does support that syntax, but nowadays it's unnecessarily cluttered. Barewords is forbidden under use strict, so there are far fewer situations in which a subroutine call has to be disambiguated.

On the other hand, the ampersand itself is visually ambiguous; it can also signify a bitwise AND operator, depending on context. And context can be extremely subtle:

    $curr_pos  = tell &get_mask(  );    # means: tell(get_mask(  ))
    $curr_time = time &get_mask(  );    # means: time() & get_mask(  )

Prefixing with & can also lead to other subtle (but radical) differences in behaviour:

    sub fix {
        my (@args) = @_ ? @_ : $_;    # Default to fixing $_ if no args provided

        # Fix each argument by grammatically transforming it and then printing it...
        for my $arg (@args) {
            $arg =~ s/\A the \b/some/xms;
            $arg =~ s/e \z/es/xms;
            print $arg;
        }

        return;
    }

    # and later...

    &fix('the race');    # Works as expected, prints: 'some races'

    for ('the gaze', 'the adhesive') {
        &fix;            # Doesn't work as expected: looks like it should fix($_),
                         # but actually means fix(@_), using this scope's @_!
                         # See the 'perlsub' manpage for details
    }

All in all, it's clearer, less ambiguous, and less error-prone to reserve the &subname syntax for taking references to named subroutines:


    set_error_handler( \&log_error );

Just use parentheses to indicate a subroutine call:


    coerce($input, $INTEGER, $ROUND_ZERO);

    fix( my $gaze, upon(each %suspect) );

    $curr_pos  = tell get_mask(  );
    $curr_time = time & get_mask(  );

And always use the parentheses when calling a subroutine, even when the subroutine takes no arguments (like get_mask( )). That way it's immediately obvious that you intend a subroutine call:


    curr_obj(  )->update($status);    
# Call curr_obj(  ) to get an object,
                                    # then call the update(  )method on that object

and not a typename:

    curr_obj->update($status);      # Maybe the same (if currobj(  ) already declared),
                                    # otherwise call update(  ) on class 'curr_obj'

    Previous
    Table of Contents
    Next
    © 2000- NIV