Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Adding directories to the end of @INC

by Anonymous Monk
on Jul 01, 2011 at 09:55 UTC ( [id://912315]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

Is there something that does the same thing as 'lib' but appends the directories to the end of @INC instead of the beginning. I can do BEGIN { push @INC, ... } but I lost the extra stuffs that 'lib' does (e.g. appending arch/version/auto dirs).

Currently my trick is to do:

use lib "/foo", "/bar"; @INC = (@lib::ORIG_INC, splice(@INC, 0, @INC-@lib::ORIG_INC));

But it's a bit cryptic.

Replies are listed 'Best First'.
Re: Adding directories to the end of @INC
by mykl (Monk) on Jul 01, 2011 at 10:33 UTC

    lib.pm isn't very large or complicated - how about making a copy with a new name (lib::append, maybe?) and replacing the unshift(@INC,...)s with pushes? And perhaps add a couple of reverses if you want duplicate-removal to leave the last instance of a directory instead of the first:

    # remove trailing duplicates ## @INC = grep { ++$names{$_} == 1 } @INC; # remove leading duplicates @INC = reverse grep { ++$names{$_} == 1 } reverse @INC;

    --

    "Any sufficiently analyzed magic is indistinguishable from science" - Agatha Heterodyne

Re: Adding directories to the end of @INC
by Anonymous Monk on Jul 01, 2011 at 10:37 UTC

    but I lost the extra stuffs that 'lib' does (e.g. appending arch/version/auto dirs)

    lib.pm is open source, so Use The Source Luke

    But wanting to do this almost doesn't make sense :)

Re: Adding directories to the end of @INC
by Khen1950fx (Canon) on Jul 01, 2011 at 17:35 UTC
    But it's a bit cryptic...

    Let's make it a little less cryptic.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper::Concise; my $foo; my $bar; BEGIN { $foo = '/foo'; $bar = '/bar'; } my(@dirs) = ($foo, $bar); use lib @dirs; push @INC, @dirs; print Dumper(@dirs), "\n", join("", @INC), "\n"; no lib @dirs; exit;
    Update: Used lib::tiny and made a few changes.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper::Concise; my $foo; my $bar; BEGIN { $foo = '/foo'; $bar = '/bar'; } my @dirs = ($foo, $bar); use lib::tiny @dirs; push @INC, @dirs; foreach my $dir(@dirs) { print Dumper(join("", @INC)), Dumper($dir), "\n"; } no lib::tiny @dirs; exit;
Re: Adding directories to the end of @INC
by sundialsvc4 (Abbot) on Jul 01, 2011 at 13:14 UTC

    And, if you are really determined to do something that is rather odd, “@INC is, after all, just an ordinary variable.”   You can push and shift it to your heart’s content until you feel better...   (Impertinent comment about laxatives omitted.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://912315]
Approved by marto
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2025-02-18 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found