Loading ...
Sorry, an error occurred while loading the content.
 

Ampersand sign pre-fix on calling of a sub-routine

Expand Messages
  • Frank Kleinburg (el Oso de Tejas)
    Hello oh keepers of perl knowledge, I have a simple question for which an explanation has eluded me so far in my searching in any of the perl books I do have..
    Message 1 of 2 , Feb 5, 2006
      Hello oh keepers of perl knowledge, I have a simple question for which an
      explanation has eluded me so far in my searching in any of the perl books
      I do have..

      In reading a lot of perl scripts here at work for which I am tasked to
      maintain, I see a lot of ampersand signs (&) in front of called subroutine
      names.. For example from some recent code at which I looked:

      # --------------------------------------------------------#
      &ReadConfigFile();
      &ReadProductionDatabases();
      &CheckPrevDbChk();
      &printScreen($Name, $Purpose, $ManualCheck) if ! $PROFILE_NAME;
      &parseOratab();
      &checkDatabases();
      &myexit;

      sub myexit
      [snip]

      Will someone take a moment and explain this?? Thanks in advance.. flk k
    • John J. Francini
      According to the Perl Cookbook (O Reilly), Chapter 10: Calling a function as $x = &func; does not supply any arguments, but rather provides direct access to
      Message 2 of 2 , Feb 5, 2006
        According to the Perl Cookbook (O'Reilly), Chapter 10:

        "Calling a function as $x = &func; does not supply any arguments, but
        rather provides direct access to its caller's @_ array! If you omit
        the ampersand and use either func() or func, then a new and empty @_
        is provided instead."

        There is nothing I can recall in either the Perl Cookbook,
        Programming Perl (the Camel Book), or anywhere else that says that an
        ampersand is used to differentiate a user-defined function from a
        pre-defined one.

        An additional statement of the & operator is given in the Camel Book,
        Chapter 6, Subroutines:

        "The official name of a subroutine includes the & prefix. A
        subroutine may be called using the prefix, but the & is usually
        optional, and so are the parentheses if the subroutine has been
        predeclared. However, the & is not optional when you're just naming
        the subroutine, such as when it's used as an argument to defined or
        undef or when you want to generate a reference to a named subroutine
        by saying $subref = \&name. Nor is the & optional when you want to
        make an indirect subroutine call using the &$subref() or &{$subref}()
        constructs. However, the more convenient $subref->() notation does
        not require it. See Chapter 8, "References" for more about references
        to subroutines."


        Hope this helps!

        John Francini



        At 21:48 -0600 2/5/06, Frank Kleinburg (el Oso de Tejas) wrote:
        >Hello oh keepers of perl knowledge, I have a simple question for which an
        >explanation has eluded me so far in my searching in any of the perl books
        >I do have..
        >
        >In reading a lot of perl scripts here at work for which I am tasked to
        >maintain, I see a lot of ampersand signs (&) in front of called subroutine
        >names.. For example from some recent code at which I looked:
        >
        > # --------------------------------------------------------#
        > &ReadConfigFile();
        > &ReadProductionDatabases();
        > &CheckPrevDbChk();
        > &printScreen($Name, $Purpose, $ManualCheck) if ! $PROFILE_NAME;
        > &parseOratab();
        > &checkDatabases();
        > &myexit;
        >
        > sub myexit
        > [snip]
        >
        >Will someone take a moment and explain this?? Thanks in advance.. flk k
        >
        >
        >
        >
        >Unsubscribing info is here:
        >http://help.yahoo.com/help/us/groups/groups-32.html
        >Yahoo! Groups Links
        >
        >
        >
        >

        --
        ----
        John Francini <mailto:francini@...>
        +---------------------------------------------------------------------------+
        | "I have come to the conclusion that one useless man is called a disgrace; |
        | that two are called a law firm; and that three or more become a Congress.|
        | And by God I have had _this_ Congress!" |
        | -- John Adams |
        +---------------------------------------------------------------------------+

        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.