in reply to Re: Dereferencing code refs with & vs. ->()
in thread Dereferencing code refs with & vs. ->()

&{ Block returning a code reference }

Are you saying that your code is littered with

&{do { # stuff here sub { ... }; }};
Because I'm not sure I like that syntax. It looks very obfuscated, to my eyes.

$$hash_ref{key} = $$array_ref[1];

Yes, I see that a lot in older code. The problem is that this syntax only works because of how tightly $ binds vs. {} or []. You're depending on the Perl parser always guessing correctly. Maybe I'm paranoid, but I prefer not making the parser guess my intention.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^3: Dereferencing code refs with & vs. ->()
by Anonymous Monk on Sep 23, 2005 at 14:34 UTC
    The official way of coding that is:
    ${$hash_ref}{key} = ${$array_ref}[1];
    You may leave of the braces if the content of the block is a simple variable. Perhaps you find that obfuscated, but that's actually the same rule as the first (optional) argument of print. The file handle is passed as a block, and taken as the result, but if the block is a simple variable, you may leave off the braces. If that isn't obfuscated, why is the code I presented obfuscated? Or is it that you aren't used to it, and call anything you aren't used to obfuscated?

    If you want to take a slice of a hash or an array, and all you have is a reference to it, how do you do it without being "obfuscated"? I write it as:

    @$hash_ref{$key1, $key2, $key3};
    which follows naturally from accessing a single value:
    $$hash_ref{$key1};
    But
    @hash_ref->{$key1, $key2, $key3};
    doesn't work and nor does
    $hash_ref->{$key1, $key2, $key3};

    And no, my code isn't littered with

    &{do {... sub {...}}};