vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Is there any way to take alias of a perl special variable ?

Replies are listed 'Best First'.
Re: alias of a variable
by barns (Acolyte) on Feb 02, 2009 at 10:57 UTC

    If by alias you mean a second reference to the same variable, you can use standard perl references.

    You will probably want to read perlref to get an idea of how that works. There are lots of "special" variables (perlvar), and I cant think of many uses for this, but this certainly works:

    #!/usr/bin/perl use strict; use warnings; $_ = "Hello"; print '$_: ' . $_ . "\n"; my $test = \$_; print '$test: ' . $test . "\n"; print '$$test: ' . $$test . "\n"; print '$|: ' . $| . "\n"; $test = \$|; print '$test: ' . $test . "\n"; print '$$test: ' . $$test . "\n"; $|++; print '$|: ' . $| . "\n"; print '$test: ' . $test . "\n"; print '$$test: ' . $$test . "\n";

    This isn't a particularly pretty piece of code, but I've separated it all out so that you can see whats going on.

Re: alias of a variable
by linuxer (Curate) on Feb 02, 2009 at 11:49 UTC

    Could you be more specific with your question? I wonder, why you want an alias to a "perl special variable"?

    Why don't you use the special variable itself?

Re: alias of a variable
by cdarke (Prior) on Feb 02, 2009 at 12:30 UTC
Re: alias of a variable
by rir (Vicar) on Feb 02, 2009 at 14:52 UTC
    *main::foo = \$/; print ">$main::foo<";
    Some perl special variables may have magical qualities that interfere with using aliases, I don't know. perldoc perldata and see the section on Typeglobs and Filehandles.

    Be well,
    rir

Re: alias of a variable
by JavaFan (Canon) on Feb 02, 2009 at 13:35 UTC
    By making use of the Alias module perhaps?
    $ perl -MAlias -E 'alias foo => $!; for (1 .. 10) {$! = $_; say $foo}' Operation not permitted No such file or directory No such process Interrupted system call Input/output error No such device or address Argument list too long Exec format error Bad file descriptor No child processes