Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Taking reference to function

by tilly (Archbishop)
on Apr 16, 2009 at 05:43 UTC ( #757844=note: print w/replies, xml ) Need Help??

in reply to Taking reference to function

Other than the huge difference of strict complaining, the differences are almost non-existent. But not totally so, as the following code sample shows:
sub foo {print "world,\n"} my $func_name = "foo"; my $func_ref = \&foo; # Replace foo *foo = sub {print "Hello "}; &$func_name(); $func_ref->();

Replies are listed 'Best First'.
Re^2: Taking reference to function
by Freezy (Scribe) on Apr 16, 2009 at 06:01 UTC
    This behavior confuses me. EG:
    my $string = "Hello."; my $string_ref = \$string; $string = "Bacon!"; print $$string_ref;
    The above prints "Bacon!", as my intuition expects. Why does the coderef point to the old block?

      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.

      Why does the coderef point to the old block?

      That's not a code reference, it's a scalar reference.
      $string_ref is a reference to the scalar, $string. Doing a print $$string_ref; will print out whatever's *presently* in $string.

      Update: Oops ... looking at Freezy's post in the context of the entire thread, I think I might've missed the point somewhat in my reply.

Re^2: Taking reference to function
by perlsaran (Sexton) on Apr 16, 2009 at 08:09 UTC
    \&thefunc is a good option.. It reduces memory consumption in symbol table Using symbolicreferences confuses the programmer.. normally ppl will think $$variable = dereferenicing a scalar variable..if it is unavoidable then use ${"$variable"}

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2017-06-27 16:50 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (611 votes). Check out past polls.