Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: To & or not to & ?

by tachyon (Chancellor)
on Nov 11, 2004 at 23:00 UTC ( #407212=note: print w/replies, xml ) Need Help??


in reply to To & or not to & ?

$result = function(@args) would be usual syntax. You almost never need &. The main effects of & are that it passes @_ to the called function and you don't need () after the function name to pass strict. However do note that &::function and ::function are calling main::function(). This matters if you are in another package:

#package main; # this is implicit bar( qw( some args that were passed along ) ); foo( 'foo() stuff' ); ::foo( '::foo() stuff' ); &::foo( '&::foo() stuff' ); sub bar { &foo; } sub foo { print __PACKAGE__, " foo got '@_'\n" } foo; # bareword will work with no strictures after sub defn package Foo; foo( 'foo() stuff' ); ::foo( '::foo() stuff' ); &::foo( '&::foo() stuff' ); sub foo { print __PACKAGE__, " foo got '@_'\n" } __DATA__ main foo got 'some args that were passed along' main foo got 'foo() stuff' main foo got '::foo() stuff' main foo got '&::foo() stuff' main foo got '' Foo foo got 'foo() stuff' main foo got '::foo() stuff' main foo got '&::foo() stuff'

cheers

tachyon

Replies are listed 'Best First'.
Re^2: To & or not to & ?
by Aighearach on Nov 11, 2004 at 23:29 UTC
    Note that, & only passes @_ IF you leave off the parens.

    So that, if bar was implemented

    sub bar { &foo(); }
    then it wouldn't pass anything at all to foo. So, &FUNC should be considered a different way to call than &FUNC(), because only one of them does something different than FUNC()

    --
    Snazzy tagline here
      because only one of them does something different than FUNC()

      Actually, all of them do something different as shown below:

      > perl -le'@_=2;sub a($$) {shift or 1};print &a' 2 > perl -le'@_=2;sub a($$) {shift or 1};print &a()' 1 > perl -le'@_=2;sub a($$) {shift or 1};print a()' Not enough arguments for main::a at -e line 1, at end of line Execution of -e aborted due to compilation errors.

      Sorry for the pedantry.

        First, it's both immoral and illogical to claim to be sorry for actions you believe to be right or correct. Typing in the claim you are sorry, meaning you don't think you should have done it, right before doing it, is rather insulting. Perhaps because you are saying sorry for what you're doing, instead saying you're sorry if it bothers anyone. The meanings are very different.

        Second, yes there is a difference if you have prototypes. But, you don't (or shouldn't) if you're asking this sort of question. It would have been good to show bug free examples where correct behavior is different, instead of just showing you can make them error differently. Unless you just meant they can error differently, but no you must have had a deeper understanding to have constructed them.

        & is dereferenced only at runtime, and function prototypes are checked at compile time. Of course, in your example the use of prototypes is the only bug, though you did manage to get them to error out differently; two with logic errors and the other with a compile error.

        it should be noted that it is very rare where use of prototypes isn't a bug (born usually of the unfortunate label "prototypes" and the confusion with C prototypes).

        Here is one example where they are different and also work:

        @array = ( "one","two" ); foo( @array ); &foo( @array ); sub foo (\@@) { print "arg one is a ", ref $_[0] || "non-ref with value $_[0]" +; } __END__ output: arg one is a ARRAY arg one is a non-ref with value one
        So with the prototype working, $_[0] gets a ref to an alias of @array, whereas without the prototype, $_[0] just gets an alias to $array[0].

        But for the record I still say, newbies should be told they are the same. Maybe with a disclaimer about, "unless you're using prototypes" and then let them learn that when the time comes.


        --
        Snazzy tagline here

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://407212]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-05-26 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?