Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Overriding Built-in Functions - Page 5

June 25, 2001

Another way to predeclare subroutines is with the use subs pragma. This not only predeclares the subroutine, but also allows us to override Perl's existing built-in functions and replace them with our own. We can access the original built-in function with the CORE:: prefix. For example, here is a replacement version of the srand function, which issues a warning if we use srand in a version of Perl of 5.004 or greater without arguments (see Appendix C for more on the srand function):

#!/usr/bin/perl
# srandcall.pl
use warnings;
use strict;
use subs qw(srand);

sub srand {
  if ($] >= 5.004 and not @_) {
    warn "Unqualified call to srand redundant in Perl $]";
  } else {
   # call the real srand via the CORE package
    CORE::srand @_;
  }
}

Now if we use srand without an argument and the version of Perl is 5.004 or greater, we get a warning. If we supply an argument we are assumed to know what we are doing and are supplying a suitably random value.

Subroutines like this are generally useful in more than one program, so we might want to put this definition into a separate module and use it whenever we want to override the default srand:

#!/usr/bin/perl
# mysrand.pm

package mysrand;

use strict;

use vars qw(@ISA @EXPORT @EXPORT_OK);
use Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(mysrand);
@EXPORT_OK = qw(srand);

sub mysrand {
  if ($] >= 5.004 and not @_) {
    warn "Unqualified call to srand redundant in Perl $]";

  } else {
  # call the real srand via the CORE package
  CORE::srand @_;
  }
}

use subs qw(srand);
sub srand {&mysrand;};
# pass @_  directly to mysrand

This module, which we would keep in a file called mysrand.pm to match the package name, exports the function mysrand automatically, and the overriding srand function only if we ask for it.

use mysrand; # import 'mysrand'
use mysrand qw(mysrand); # import and predeclare mysrand;
use mysrand qw(srand); # override 'srand'

We'll talk about packages, modules, and exporting subroutines in Chapter 10.

Strict Subroutines and the 'use strict subs' Pragma - Page 4
Professional Perl Programming
The Subroutine Stack - Page 6


Up to => Home / Authoring / Languages / Perl / ProPerl




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers