Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Because the symbol foo and the coderef are different things. *foo = ... assigns a new coderef to the symbol foo. It doesn't redefine the coderef for the original sub foo, which is kind of like a constant.

\&foo takes the address of a constant, not a variable. So it is analogous to \"Hello World" rather than \$some_var_holding_a_string. You can't reassign it any more than you can reassign the address of the string literal "Hello World".

Well, almost so. Sub "literals" are more complex than a string. Redefine a string and you get a different string. Redefine a sub and you get a different association between sub name and body. So if you *really*, *really* were set on redefining sub foo itself, you could, but (a) you have to nudge Perl into compiling code at runtime and (b) you will get a warning about redefining things that are not supposed to be redefined:

use strict; use warnings; sub sayHello { print "Hello, world!\n"; } sayHello(); eval q{sub sayHello { print "Bonjour, le monde!\n"; }}; sayHello();

which outputs

Hello, world! Subroutine sayHello redefined at (eval 1) line 1. Bonjour, le monde!

Best, beth

Update: added some explanation about difference between sub and string literals.


In reply to Re^3: Taking reference to function by ELISHEVA
in thread Taking reference to function by vinoth.ree

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found