tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

I, unenlightened one, am trying to understand Aliasing Variables.

What is the meaning/purpose of code like

*z=\$x;
Is *var called the star sigil? Google doesn't seem to think so. Is this star stuff an artefact of an earlier version of perl? If there are still reasons to use *var with modern perl, where can I learn more about it?

And finally, for a concrete code example, how would I get this to compile without errors under use strict?

#inspired by http://www.perlmonks.org/index.pl?node_id=98721 use strict; #use warnings; my $x=2; *z=\$x; print $z;
Note: Without use strict, this prints 2.

Thanks for your wisdom!

Replies are listed 'Best First'.
Re: Trying to understand aliasing (*var)
by dave_the_m (Monsignor) on Feb 11, 2005 at 12:33 UTC
    Typeglobs, ie *foo are mostly an artifact of Perl 4, which didn't have references; they will be removed completely in Perl6

    A typeglob can be though of as a wildcard: *foo represents all of $foo, @foo, %foo etc. When you assign a reference to a typeglob, it replaces the slot in the typeglob corresponding to the the type of the reference, with that value; so

    *foo = [ 1,2,3 ]
    acts like
    @foo = (1,2,3)
    except that you are actually replacing @foo with with a new array rather than just replacing its contents.

    To get your code to compile, add an  our $z at the top.

    Dave.

      they will be removed completely in Perl6". So that might be a reason to avoid this particular type of magic,...

      By that token, you should also avoid $array[ 1 ] = 2;, because that won't work in p6 either.

      Nor will $hash{fred}=27;.

      Nor will do { n++; } while $n < 10;.

      Nor will print ( 'Hello, world!" );.

      Nor will while( <DATA> ) { my @array = split'\t'' };.

      Nor will keys %hash = 1000;.

      Nor will for( my i=0; $i < @array; i++ ) {...};

      Nor will $#array = 1_000_000;.

      Read more... (357kB)

      Nor will open FILE, '<', 'myfile' or die $!;.

      But then again, local *array = \$arrayRef; won't be needed, because the syntax for

      my @array; push @array, $somevalue;

      And

      my $array = []; push @array, $someValue;

      Will be the same...or so it was at some point in time.

      So, whilst P5 doesn't provide an alternative to the simplicity of

      local *array = $aRef; push @array, $someValue;

      it is a very useful construct to know.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
Re: Trying to understand aliasing (*var)
by gellyfish (Monsignor) on Feb 11, 2005 at 12:32 UTC

    It's called a typeglob. To get your example to work you will need a use vars qw($z) or specify the full package of the variable:

    use strict; my $x=2; *main::z=\$x; print $main::z;
    Not that this will not work with a lexical $z declared with my as it has to be a package variable.

    /J\

      It's called a typeglob. To get your example to work you will need a use vars qw($z) or specify the full package of the variable:
      or use our $z.
Re: Trying to understand aliasing (*var)
by blazar (Canon) on Feb 11, 2005 at 12:45 UTC
    What is the meaning/purpose of code like
    *z=\$x;
    it's... ehm, aliasing variables.
    Is *var called the star sigil? Google doesn't seem to think so. Is this star stuff an artefact of an earlier version of perl?
    Well, there are still cases in which it is useful to use typeglobs, but let's say that there are less than there used to be. As noted by someone else they won't be there in Perl6, but OTOH it will provide syntactical sugar to do (different kinds of) aliasing differently.
    If there are still reasons to use *var with modern perl, where can I learn more about it?
    Personally I think that it's useful to know about them, but if "there are reasons to" is up to you, as with a lot of other things in Perl. To do so I suggest perldata and the other documents referenced there.
Re: Trying to understand aliasing (*var)
by Errto (Vicar) on Feb 11, 2005 at 14:38 UTC
    I know of one particular example of where typeglobs are useful, that was shown to me by ChemBoy. One of his colleagues had a variable called %SEPERATOR and ChemBoy kept "mis"-typing it because it was spelled wrong. Rather than do a global search-and-replace on the source code, which is always dangerous, he wrote:
    use vars '%SEPARATOR'; *SEPARATOR = \%SEPERATOR;
    and went on his merry way using %SEPARATOR in the rest of his code.
      Rather than do a global search-and-replace on the source code, which is always dangerous

      Run the tests before, make the change, and run the tests after. Fix what breaks. It's pretty easy.

        Depends on the size of your code and how clean it is. IIRC, in this case the code was fairly old and poorly organized, with references to package globals spread across an unpleasantly large number of source files. I've never seen the code, but this is my strong impression. Though I am a strong supporter of good spelling among developers and people in general, I think in this case the risk-reward analysis didn't favor changing the existing code.

        In the above example, I think it's more reasonable to get the official correct spelling, and have everyone learn a bit of spelling, than to use aliasing.

        In the general case, you're assuming that a) there are tests to run, and, more importantly, b) that the tests have 100% coverage. While there may not be a lot of pity for the fool (apologies to Mr. T) in the first situation, the latter is much more realistic. Getting to 100% coverage is difficult, if not actually impossible once you've released a module and others may be using it beyond your ability to test/modify. Sometimes the alias is fixing what breaks. Then again, in this situation, search-and-replace doesn't really work, either.