5

I like Data::Alias. It seems to be broken in 5.12. Can it be fixed? Will it be fixed soon? Is there any nice alternative?

Borodin
  • 126,100
  • 9
  • 70
  • 144
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159

3 Answers3

11

Any version of Data::Alias built before Version 1.08 (Released October 22nd, 2010 BST) won't work with Perl 5.12 as Data::Alias prior to 1.08 is broken in Perl 5.12. Upgrade to the latest version (1.08 or newer) and it should work!

As an interesting side note, it seems like being able to do aliases may be coming to Perl as a language feature in the future, with the cleanup of := no longer meaning an empty attribute list. Look forward to it! :)

Robert P
  • 15,707
  • 10
  • 68
  • 112
  • 1
    I thought everything in CPAN was automatically tested against the 5.12 release candidates? Perhaps there wasn't a unit test that covered this case in the distribution, in which case reporting the bug will ensure that it never gets broken again. :) – Ether May 04 '10 at 21:13
  • @Ether: Judging by the thread on rt this isn't a bug; the module's XS code was broken by a change to the internals. As for testing release candidates against CPAN, I don't know but it wouldn't seem to useful unless you did a matrix comparison to the results for the previous version of perl on the same platform. Lots of modules on CPAN have failures, that doesn't mean that 5.12 broke them. :) – Michael Carman May 04 '10 at 21:47
  • 2
    rafl took a look at it for me a few weeks ago and said it wouldn't be too hard to fix (that's "too hard" for rafl). He's supposed to do that so Method::Signatures can work in 5.12. – Schwern May 04 '10 at 23:37
  • 7
    Testing everything in CPAN isn't the same as everything in CPAN passing its tests. :) – brian d foy May 05 '10 at 00:10
5

The module hasn't been updated since 2007 but you can always send a message to the author (Matthijs van Duin: [email protected]) or file a bug report as Robert mentioned in his answer.

Here are some alternatives:

  • As far as additional CPAN modules for aliasing that work in 5.12+:

    And searching for 'alias' on CPAN turns up a few more, none seem to provide the "do everything with aliases in this statement" feature of Data::Alias though. So until Data::Alias is fixed, you can use one of the above, or the following pure Perl methods:

  • Perl has built in support for aliasing any variable to variables that exist in the symbol table. This is done as follows:

    my $x = 1;
    our $y; # declare $y in the symbol table for the current package
    {
        local *y = \$x;  # make $y an alias of $x in the current scope
        $y++;
    }
    print $x;  # prints 2
    

    But as always, be aware of what dynamic scope / local actually does before using it.

  • A lexical scalar can be used as an alias within the scope of a for loop:

    my $x = 1;
    for my $y ($x) {
        $y++;
    }
    print $x;  # prints 2
    

    this type of lexical alias can even be passed out of the loop in a closure if needed

  • You can create array aliases using Perl's aliasing magic for subroutine argument lists:

    my $x = 1;
    my $alias = sub{\@_}->($x); # return a reference to its argument list, 
                                # which maintains its aliases
    $$alias[0]++;
    print $x;     # prints 2
    

    but that doesn't really give you any more functionality than references, just with a different syntax.

  • And an example using Perl's references:

    my $x = 1;   
    my $y = \$x;  # take a reference to $x
    $$y++;        # dereference $y
    print $x;     # prints 2 
    
Eric Strom
  • 39,821
  • 2
  • 80
  • 152
  • A reference isn't the same thing as an alias. The examples you show are a limited subset of what Data::Alias does. – brian d foy May 05 '10 at 00:12
  • @brian d foy => the reference example is there to show an alternative, the other two examples are the remainder of the pure Perl aliasing techniques that I know of. these are of course a limited subset otherwise Data::Alias probably wouldn't have been written. the OP asked for alternatives – Eric Strom May 05 '10 at 00:55
  • also, a reference is exactly the same thing as an alias, just in different clothes. to alias something, you place a reference to it into the appropriate slot in a variable's type glob. Whatever sigil you use with the typeglob label tells Perl to dereference it, so `$y` reads as "a scalar dereference of the typeglob labeled y". and there's always the `*foo{THING}` syntax if you want to be verbose about it, in this case `${*y{SCALAR}}++;` – Eric Strom May 05 '10 at 01:27
  • again, I NEVER said all the functionality of `Data::Alias` could be recreated without it. do you actually have a problem with anything i wrote in my answer? (also, the `for my $y ($x) {return sub {$y++}}` construct creates a lexical alias, admittedly in a limited context) – Eric Strom May 05 '10 at 12:54
  • It's not clear from the documentation of `Lexical::Alias` and `Variable::Alias` whether they support things like `alias my $x => $array[5]`. That is, can I declare variables as I alias them, and can I create aliases to elements of a collection? These are two of the nice features of `Data::Alias`. – Ryan C. Thompson May 09 '10 at 16:54
  • @Ryan Thompson => in modern versions of perl, that syntax should work with either module. Those modules flip the args though `alias $array[5] => my $y;` but you could always wrap it in a sub to flip it around. – Eric Strom May 09 '10 at 21:42
0

I just found another potential option: Scalar::Alias, which seems to work in Perl 5.12. Obviously, it only aliases scalars, but it doesn't require a fat comma in place of an equals sign.

Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159