I am trying to get a block of code up to one line. I need a way to get the number of items in a list. Currently my code is as follows:
# Include the lib directory several levels up from this directory my @ary = split('/', $Bin); my @ary = @ary[0 .. $#ary-4]; my $res = join '/',@ary; lib->import($res.'/lib');
This is great, but I want to make one line, something like this:
lib->import( join('/', ((split('/', $Bin)) [0 .. $#ary-4])) );
But of course, the syntax of $#ary does not make sense in the line above.
Is there an equivalent way to get the number of items in an anonymous list?
Thanks!
PS: The reason for the consolidation is that it will be in the header of a bunch of perl scripts that are auxiliary to the main application, and I want this little spell to be more cut and pasted.
Thanks everyone
There seems to be no reduction in the number of items in the anonymous list. This is like surveillance. However, the proposed alternatives were good.
I'm going to:
lib->import(join('/', splice( @{[split('/', $Bin)]}, 0, -4)).'/lib');
But Ether suggested the following, which is much more correct and portable:
my $lib = File::Spec->catfile( realpath(File::Spec->catfile($FindBin::Bin, ('..') x 4)), 'lib'); lib->import($lib);
arrays perl slice
Nxt
source share