in reply to Re^4: BEGIN and compile-time
in thread BEGIN and compile-time
what do you think a subsequent strict->VERSION() or $strict::VERSION would report?? Note where ".." is placed in @INC. There's no two ways around this, 1.04 it is as ../strict.pm is loaded.BEGIN { push @INC, ".." } use strict 1.04;
How about now?? no difference at all.BEGIN { use lib ".."; # or unshift @INC, ".." } use strict 1.04;
As you can see, there's No function redefinition errors, no 'magic' invocation of the 'second' use; (outside of the second eval too). The 'second' use is invoked conditionally in a straight-forward, perlish try-catch construct, when and only when the first eval(use ...;) fails. The arrangement of @INC doesn't have an effect on ../strict.pm being overridden either (unless another claiming to be version 1.04 is found before it in @INC, the odds are errm, quite neglible) but again i should emphasize on pushing to @INC not the otherway.#!/usr/bin/perl -lW # catch all bleeping errors BEGIN { push @INC, ".." } my @TAGS = qw| pedantics refs |; my $ideal_version = 1.04; eval " use strict $ideal_version @TAGS; # fails, strict's VERSION==1.03 "; if ($@ =~ /version $ideal_version required.*BEGIN failed/ism) { warn qq|use strict $ideal_version qw(@TAGS); failed, falling back|; shift @TAGS; # Unknown 'strict' tag 'pedantics' eval "use strict qw|@TAGS|"; # which strict is this?? unless ($@) { print qq|second eval, using strict $strict::VERSION qw(@TAGS) fr +om module |, $INC{"strict.pm"}; } } print qq|main now using strict $strict::VERSION qw(@TAGS) from module +|, $INC{"strict.pm"}; if ( grep /pedantics/, @TAGS ) { # newer 'pedantics' related code here # will this run under strict v1.03?? not a chance } else { # workaround for backward compat. # sub pedantics {...}; } if ( grep /refs/, @TAGS ) { # usual 'refs' related code here } else { # quite unlikely to be reached } __END__ __output__ use strict 1.04 qw(pedantics refs); failed, falling back second eval, using strict 1.03 qw(refs) from module /usr/share/perl/5. +8/strict.pm main now using strict 1.03 qw(refs) from module /usr/share/perl/5.8/st +rict.pm
into simpler expressions..BEGIN { eval { require Foo::Bar; if ($Foo::Bar::VERSION eq 5.01) { import Foo::Bar qw| ... |; } }; revert() if $@; }
I favour use; for it's advantage in compile time invocation (preemption again) as opposed to require; which is invoked at run-time, the difference is subtle yet not-so-subtle at the same time, not-so-subtle in terms of longterm keyboard wear-and-tear and premature grey hair, go figure.eval " use Foo::Bar 5.01 qw| ... | "; revert() if $@;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: BEGIN and compile-time
by ikegami (Patriarch) on Nov 10, 2006 at 19:01 UTC | |
by tye (Sage) on Nov 10, 2006 at 19:17 UTC |