[Canberra-pm] how to tell if a library is available?
Andres Kievsky
akievsky at yahoo.com.au
Thu Jun 19 14:59:55 PDT 2008
Kim,
As you noticed, the problem is that perl is still compiling the code inside the if() block. I don't know of any way to stop perl from compiling it; It has to be valid perl, and when it's checked, the autoformat() sub has not been defined. You can circunvent this problem by adding parenthesis:
autoformat($data. {...});
instead of
autoformat $data. {...};
However, I think it'd be nicer to wrap the AUTOFORMAT sections in a plug-in that hides all this complexity and provides a single entry point for all code that might need to get its data formatted. Or why not just bundle Autoformat.pm? From what I can see, it's a single .pm file that can be copied to your source tree.
- ank
----- Original Message ----
From: Kim Holburn <kim at holburn.net>
To: Paul Fenwick <pjf at perltraining.com.au>
Cc: canberra-pm at pm.org
Sent: Wednesday, 18 June, 2008 7:26:03 PM
Subject: Re: [Canberra-pm] how to tell if a library is available?
Hi Paul,
Thanks very much for your reply.
I still have a niggle.
I tried your constant solution. It works fine when it can find the
library but produces a strange error when it can't. So there still
appears to be some compiling going on.
$ perl test3.pl
Useless use of single ref constructor in void context at test3.pl line
19.
no
Here is my script:
#!/usr/bin/perl -w
BEGIN { eval "use Text::Autoformat"; }
use constant AUTOFORMAT => $INC{'Text/Autoformat.pm'};
my $data = <<'EOM';
>> That bundle of copyrights relates to the very considerable
>> investment required to make a succession of feature films.
It's a kind of circular argument. People make an investment in a
movie in order to make a profit because they know they have these
copyrights and a bunch of people who earn their living helping them
protect those copyrights.
EOM
if (AUTOFORMAT) {
my $width = 72;
$data = autoformat $data, { right=>$width } ;
print "yes\n";
print $data;
}
else { print "no\n"; }
On 2008/Jun/17, at 10:39 AM, Paul Fenwick wrote:
> G'day Kim / p5p,
>
> Kim Holburn wrote:
>
>> It obviously works at run time as in the eval but what exactly it
>> does then I'm not sure. I think from the behaviour I'm seeing
>> that the use in the eval is not determined at compile time because
>> at compile time it's just a string. Therefore references to
>> variables in the library are not linked at compile time unless a
>> naked "use" or "require" statement appears somewhere.
>
> Ah! You may want something like this:
>
> BEGIN {
> eval "use Text::Autoformat";
> if ($@) {
> # Holy smokes Batman! Text::Autoformat isn't
> # available! Do something to set up the variables,
> # subroutines, and other things that Text::Autoformat
> # would normally provide for us
> }
> }
>
> By wrapping the code inside a BEGIN block, you force it to be
> executed at compile-time, before Perl tries to resolve the rest of
> the subroutines and variables in your code. If Text::Autoformat
> exists, then it gets loaded at compile time, and everything is
> fine. If it doesn't exist, then you'll need to do *something* to
> ensure that parts of your code trying to use Text::Autoformat have
> something to play with. I can't be more specific without seeing the
> warnings that's being generated; perhaps a piece of sample code may
> help here?
>
> > I guess what I'd like are some conditional compile directives but
> they
> > don't exist.
>
> If you want code that's simply not compiled at all if a module isn't
> installed, you can do something like this:
>
> BEGIN { eval "use Text::Autoformat"; }
>
> use constant AUTOFORMAT => $INC{'Text/Autoformat.pm'};
>
> # Later, in your main code...
>
> if (AUTOFORMAT) {
> # Do something that uses Text::AutoFormat...
> } else {
> # Do something else.
> }
>
> What we're doing here is checking for the module at compile-time,
> setting a constant based upon whether it's been found and loaded,
> and then conditionally compiling code based upon that constant.
> Because Perl knows that constants can't change, it can completely
> prune unwanted code from the parse tree at compile-time. The text
> that was pruned won't give you warnings, because as far as Perl is
> concerned, it wasn't there. ;)
>
> Of course, if you have to start pulling tricks like this, you'll
> probably find it becomes *much* easier to simply insist the modules
> you want are installed, or using PAR (the Perl Archiver) to bundle
> them up with your program. You can find our tutorial on that at:
>
> http://perltraining.com.au/tips/2008-05-23.html
>
> Cheerio,
>
> Paul
>
> --
> Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
> Director of Training | Ph: +61 3 9354 6001
> Perl Training Australia | Fax: +61 3 9354 2681
--
Kim Holburn
IT Network & Security Consultant
Ph: +39 06 855 4294 M: +39 3494957443
mailto:kim at holburn.net aim://kimholburn
skype://kholburn - PGP Public Key on request
Democracy imposed from without is the severest form of tyranny.
-- Lloyd Biggle, Jr. Analog, Apr 1961
_______________________________________________
Canberra-pm mailing list
Canberra-pm at pm.org
http://mail.pm.org/mailman/listinfo/canberra-pm
Get the name you always wanted with the new y7mail email address.
www.yahoo7.com.au/mail
More information about the Canberra-pm
mailing list