Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Create a built-in-like sub?

by Juerd (Abbot)
on Feb 01, 2002 at 09:16 UTC ( [id://142641]=note: print w/replies, xml ) Need Help??


in reply to Create a built-in-like sub?

perldoc Exporter
use base 'Exporter' our @EXPORT = qw(compress);

2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Replies are listed 'Best First'.
Re: Re: Create a built-in-like sub?
by mirg drol (Initiate) on Feb 01, 2002 at 10:00 UTC
    I can't get that to work in my script. ;[ If i "require" a module from my main script, that module can't reach "compress" by simply calling "compress", only by calling "GLOBAL->compress". This is my test setup:

    main.pl
    #!/usr/bin/perl -w use GLOBAL; compress "main\n"; require "MODULE.pm"; MODULE->test;

    GLOBAL.pm
    package GLOBAL; use base 'Exporter' our @EXPORT = qw(compress); sub compress { print shift; } 1;
    MODULE.pm
    package MODULE; sub test { compress "test\n"; } 1;
    Did i just do something wrong is it impossible to use "compress" like this?
      Just as a note, your module should look like (at least in 5005_03):
      package GLOBAL use 'Exporter'; @ISA = qw(Exporter); #not always needed, but usually a good practice @EXPORT = qw(compress); ... 1; package MODULE use GLOBAL; use Exporter; @ISA = qw(Exporter); #again, usually a good practice. @EXPORT = qw(test); sub test { compress "test\n"; } 1;
      I may be incorrect in the new perl 5.6+ world since I'm still using 5.005_03. The only reason I added the use statement to the MODULE package is to make sure that the sub compress is pushed into it.

      Hope this helps, but YMMV.

      Theodore Charles III
      Network Administrator
      Los Angeles Senior High
      4650 W. Olympic Blvd.
      Los Angeles, CA 90019
      323-937-3210 ext. 224
      email->secon_kun@hotmail.com
        use base is new syntax, introduced with Perl 5.004_04, so it should work with your 5.005_03.
        You have to type less, and the intent is clear immediately.

        It will require the module if needed, and will push it onto @ISA. It will do some other things as well - read the manual.

        2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      First, there is an error in GLOBAL.pm

      package GLOBAL; use base 'Exporter'
      is missing ';'
      use base 'Exporter';
      our @EXPORT = qw(compress); sub compress { print shift; } 1;

      Second, add use GLOBAL; to MODULE.pm

      package MODULE; use GLOBAL; sub test { compress "test\n"; } 1;



      HTH,
      Charles K. Clarkson
      Exporting (through Exporter or otherwise) comes in two parts. The code to export needs to declare what it will export. That's what using Exporter and populating @EXPORT or @EXPORT_OK does for you.

      The code that wants to import also must do something -- namely, asking the other code to import stuff. If you require MODULE, you need to call MODULE->import(). If you use MODULE, Perl will call import() automatically.

      Where did import() come from in MODULE? It came from using Exporter. If you'd require()d Exporter, you'd have had to call Exporter->import().

      Does that help?

        thanks everybody, i know how to use Exporter now, and i see that i tried to use it in a way that wasn't very pretty.. ;] slowly i'm learning.. ;]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://142641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found