[26900] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8888 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 26 14:05:16 2006

Date: Thu, 26 Jan 2006 11:05:05 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 26 Jan 2006     Volume: 10 Number: 8888

Today's topics:
        Check subroutine-specific requirements on module import <markm@chiark.greenend.org.uk>
    Re: Check subroutine-specific requirements on module im <uri@stemsystems.com>
        How to convert datetime format? <sonet.all@msa.hinet.net>
    Re: How to convert datetime format? <1usa@llenroc.ude.invalid>
    Re: Ordering string of commands in a file <tadmc@augustmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 26 Jan 2006 17:11:23 +0000 (GMT)
From: Mark Mackey <markm@chiark.greenend.org.uk>
Subject: Check subroutine-specific requirements on module import
Message-Id: <7Qd*UcK9q@news.chiark.greenend.org.uk>

Hi all.

I've got a simple problem that I can't work out an elegant way to solve.
I have a simple Perl module which is basically a wrapper for some
external binaries. I want the module to check for the existence of those
binaries on import, but only for the binaries which are actually going
to be used by the subroutines that are exported.

For example, I have Thingy.pm:

package Thingy;

require Exporter;
@ISA = qw(Exporter);
# symbols to export on request
@EXPORT_OK = qw(sub1 sub2 sub3);

use strict;

sub sub1 {
  print `/bin/ls`;
}

sub sub2 {
  print `/bin/true`;
}

sub sub3 {
  print `/bin/no_exist`;
}

BEGIN {
  my(@required)=qw( /bin/ls /bin/true /bin/no_exist );
  my(%required)=( sub1=>"/bin/ls", sub2=>"/bin/true", sub3=>"/bin/no_exist" );
  foreach (@required) {
    print "Checking $_\n";
    die "Can't find required utility $_" if (! -x $_);
  }
}
1;

Sub1 will only work if "/bin/ls" is present, sub2 will only work if
"/bin/true" is present, and sub3 will only work if "/bin/no_exist" is
present. With the current structure, all 3 are checked on import, so

use Thingy qw();

dies telling me that "bin/no_exist" doesn't exist :).

What I want is to be able to check the relevant binaries' existence only
for the methods that are imported, so in this case you could do

use Thingy qw(sub1);

and everything would be happy, but

use Thingy qw(sub1 sub2 sub3);

would bail out and inform me that 'sub3' depends on "/bin/no_exist" and
hence it can't continue.

The reason I'd like this check is that this module is used in lots of
programs, each of which uses a different subset of its subroutines. Some
of the programs have very long runtimes, so I don't really want them to
run for hours and then suddenly bail out when sub3() finally gets
called. I suspect the answer involves fiddling around with import()
methods, but I don't completely understand the documentation for
Exporter.pm about this.

Help?

-- 
Mark Mackey   http://www.swallowtail.org/
code code code code code code code code code code code code code bug code co
de code code code bug code code code code code code code code code code code
code code code code code code code code code code code code code code code c





------------------------------

Date: Thu, 26 Jan 2006 13:26:46 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Check subroutine-specific requirements on module import
Message-Id: <x7irs6hml5.fsf@mail.sysarch.com>

>>>>> "MM" == Mark Mackey <markm@chiark.greenend.org.uk> writes:


  MM> require Exporter;
  MM> @ISA = qw(Exporter);

those two lines are better done as:

use base 'Exporter'

  MM> # symbols to export on request
  MM> @EXPORT_OK = qw(sub1 sub2 sub3);

  MM> use strict;

put the strict line before all the others. then use our before
@EXPORT_OK. the use base line will still work correctly too.

  MM> What I want is to be able to check the relevant binaries' existence only
  MM> for the methods that are imported, so in this case you could do

  MM> The reason I'd like this check is that this module is used in lots of
  MM> programs, each of which uses a different subset of its subroutines. Some
  MM> of the programs have very long runtimes, so I don't really want them to
  MM> run for hours and then suddenly bail out when sub3() finally gets
  MM> called. I suspect the answer involves fiddling around with import()
  MM> methods, but I don't completely understand the documentation for
  MM> Exporter.pm about this.

you just need to declare your own import() method. it is called with the
class name and the arguments passed on the use line so you will get
'sub1', etc. do your checks then and die or whatever as needed. if all
the requested features are supported, then you can pass the import
request on to Exporter with: (untested)

sub import {

	my( $class, @features ) = @_ ;

# do your testing of the requested features in @features
# and die as desired.

# at this point everything wanted is found

	$class->SUPER::import( @features ) ;
}

since you are subclassing Exporter, it will get the same call as if it
was handled directly without your intervening import method.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Fri, 27 Jan 2006 00:40:44 +0800
From: "sonet" <sonet.all@msa.hinet.net>
Subject: How to convert datetime format?
Message-Id: <drau2m$snk$1@netnews.hinet.net>

26/01/2006, 06:19 PMSIN
Fri, 27 Jan 2006 12:38:24 SGT

 ....

http://www.taiwan.cnet.com/rss/today_news.htm






------------------------------

Date: Thu, 26 Jan 2006 17:19:48 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How to convert datetime format?
Message-Id: <Xns97577D8056B22asu1cornelledu@127.0.0.1>

"sonet" <sonet.all@msa.hinet.net> wrote in news:drau2m$snk$1
@netnews.hinet.net:

> 26/01/2006, 06:19 PMSIN
> Fri, 27 Jan 2006 12:38:24 SGT

What have you done so far? Please read the posting guidelines for this 
group to maximize your chances of getting useful responses.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



------------------------------

Date: Thu, 26 Jan 2006 10:25:04 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Ordering string of commands in a file
Message-Id: <slrndthtv0.fmb.tadmc@magna.augustmail.com>

Raghuramaiah Gompa <rgompa@steel.ucs.indiana.edu> wrote:

>>for my $idx (
>>     sort {
>>	    lc $items{$a}{Title} cmp lc $items{$b}{Title}
>>	} keys %items)

> Now I am trying to modify the above code to print the 
> hash without the sort command.  Could some one help me 
> with this?


   for my $idx ( keys %items)


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8888
***************************************


home help back first fref pref prev next nref lref last post