9

I had a small question. I was reading some code and as my school didn't teach me anything useful about perl programming, I am here to ask you people. I see this line being used a lot in some perl programs:

$variable = &something();

I don't know what the & sign means here as I never say it in perl. And the something is a subroutine ( I am guessing). It usually says a name and it has arguments like a function too sometimes. Can someone tell me what & stands for here and what that something is all the time.

The variable takes in some sort of returned value and is then used to check some conditions, which makes me think it is a subroutine. But still why the &?

Thanks

2 Answers 2

29

Virtually every time you see & outside of \&foo and EXRP && EXPR, it's an error.


  • &foo(...) is the same as foo(...) except foo's prototype will be ignored.

    sub foo(&@) { ... }      # Cause foo to takes a BLOCK as its first arg
    
    foo { ... } ...;
    &foo(sub { ... }, ...);  # Same thing.
    

    Only subroutines (not operators) will be called by &foo(...).

    sub print { ... }
    print(...);         # Calls the print builtin
    &print(...);        # Calls the print sub.
    

    You'll probably never need to use this feature in your entire programming career. If you see it used, it's surely someone using & when they shouldn't.

  • &foo is similar to &foo(@_). The difference is that changes to @_ in foo affects the current sub's @_.

    You'll probably never need to use this feature in your entire programming career. If you see it used, it's surely someone using & when they shouldn't or a foolish attempt at optimization. However, the following is pretty elegant:

    sub log_info  { unshift @_, 'info';  &log }
    sub log_warn  { unshift @_, 'warn';  &log }
    sub log_error { unshift @_, 'error'; &log }
    
  • goto &foo is similar to &foo, except the current subroutine is removed from the call stack first. This will cause it to not show up in stack traces, for example.

    You'll probably never need to use this feature in your entire programming career. If you see it used, it's surely a foolish attempt at optimization.

    sub log_info  { unshift @_, 'info';  goto &log; } # These are slower than
    sub log_warn  { unshift @_, 'warn';  goto &log; } # not using goto, but maybe
    sub log_error { unshift @_, 'error'; goto &log; } # maybe log uses caller()?
    
  • $& contains what the last regex expression match matched. Before 5.20, using this causes every regex in your entire interpreter to become slower (if they have no captures), so don't use this.

    print $&        if /fo+/;     # Bad before 5.20
    print $MATCH    if /fo+/;     # Bad (Same thing. Requires "use English;")
    print ${^MATCH} if /fo+/p;    # Ok (Requires Perl 5.10)
    print $1        if /(fo+)/;   # Ok
    
  • defined &foo is a perfectly legitimate way of checking if a subroutine exists, but it's not something you'll likely ever need. There's also exists &foo is similar, but not as useful.

  • EXPR & EXPR is the bitwise AND operator. This is used when dealing with low-level systems that store multiple pieces of information in a single word.

    system($cmd);
    die "Can't execute command: $!\n"       if $? == -1;
    die "Child kill by ".($? & 0x7F)."\n"   if $? & 0x7F;
    die "Child exited with ".($? >> 8)."\n" if $? >> 8;
    
  • &{ EXPR }() (and &$ref()) is a subroutine call via a reference. This is a perfectly acceptable and somewhat common thing to do, though I prefer the $ref->() syntax. Example in next item.

  • \&foo takes a reference to subroutine foo. This is a perfectly acceptable and somewhat common thing to do.

    my %dispatch = (
       foo => \&foo,
       bar => \&bar,
    );
    
    my $handler = $dispatch{$cmd} or die;
    $handler->();
    # Same:  &{ $handler }();
    # Same:  &$handler();
    
  • EXPR && EXPR is the boolean AND operator. I'm sure you're familiar with this extremely common operator.

    if (0 <= $x && $x <= 100) { ... }
    
3
  • 1
    This should be & usage for dereferincing? perl -E '&{sub{ say pop }}( 4 )'
    – mpapec
    Sep 27, 2013 at 19:08
  • 1
    @mpapec, Yes, though I prefer sub { say pop }->( 4 ). Added.
    – ikegami
    Sep 27, 2013 at 19:54
  • Note: Sublanguages (e.g. prototypes, regex patterns) may also use &.
    – ikegami
    Nov 12, 2019 at 14:48
16

In older versions of perl & was used to call subroutines. Now this is not necessary and \& is mostly used to take a reference to subroutine,

my $sub_ref = \&subroutine;

or to ignore function prototype (http://perldoc.perl.org/perlsub.html#Prototypes)

Other than for referencing subroutines & is bitwise and operator,

http://perldoc.perl.org/perlop.html#Bitwise-And

15
  • oh so if I change it to without a & sign, it would still work?
    – ban
    Jun 18, 2013 at 16:11
  • okay. So I am not able to find the function it is pointing to. It must be in some other file. But can you tell me how they called a function from another file without using the package thing : package::sub()
    – ban
    Jun 18, 2013 at 16:17
  • I need to make one of my own...so I need some understanding
    – ban
    Jun 18, 2013 at 16:18
  • how does your script look like? you could take a reference to package method and call it via reference, my $f = \&package::sub; $f->();
    – mpapec
    Jun 18, 2013 at 16:22
  • 2
    @ikegami - from the .pl extension and the '&foo()' in the code, I'd expect it is very old code and not to be using packages, but doing something like this: file.pl sub foo { ... } 1; then require file.pl; foo()
    – plusplus
    Jun 18, 2013 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.