Home: Perl Programming Help: Advanced:
new perl function


Jan 17, 2001, 10:56 AM

Views: 1495
new perl function

sorry if this repeats... I asked this in #perl dalnet

In the shell, you can write new aliases easily, which will be active for any new shell script... how is
this done in perl ? Evidently there is no easy way, but would this be a good new function to write ?

Short of that, could someone show me how to write a new function replacement name into perl
to compile ?

Enthusiast / Moderator

Jan 17, 2001, 1:04 PM

Views: 1486
Re: new perl function

You are talking about subroutines. Just create a file with all the "aliases" you want, and incorporate that into all the programs you want them in.

# aliases.pm 

package aliases;

use strict;

my @export = qw( fopen cls );

sub import {
my $pkg = caller;
no 'strict';
*{"${pkg}::$_"} = \&$_ for @_ ? @_ : @export;

sub fopen {
my $file = shift;
local *FH;
open FH, $file or die "can't open $file: $!";
return \*FH;

my $cls = `clear`;
sub cls { print $cls }


This module would be used like so:

use aliases;  # imports fopen() and cls() 

use aliases qw( cls ); # imports ONLY cls()

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author