-4

I am trying to understand some code which is already written..

I am not able to understand

my $l = $0; $l =~ s-/[^/]+/[^/]+$-/lib/perl-; 

what is assigned to $0 ? what is - (Hyphen) operator in the expression ? whay $- is used in the expression?

Please explain the ablove 3Qs.

BEGIN {
# figure out our general library path
my $p;
my $l = $0; $l =~ s-/[^/]+/[^/]+$-/lib/perl-; 
push @INC,$l;
# add some platform components, based on files
push @INC,"$l/sunos"  if ( grep /solaris/,@INC );
push @INC,"$l/csw"    if ( grep /csw/,@INC );
push @INC,"$l/i386"   if ( grep /i386/,@INC );
push @INC,"$l/x86_64" if ( grep /x86_64/,@INC );
}
Jens
  • 60,806
  • 15
  • 81
  • 95

1 Answers1

2

You can find various Perl special variables in perlvar. $0 is explained there.

As mentioned in perlop, the substitution operator s/REGEX/REPLACEMENT/ can be written with different delimiters, e.g. dashes. It's handy when the regex or replacement contain slashes, as is the case here. Using paired delimiters might be more readable:

s{/[^/]+/[^/]+$}{/lib/perl}

or, using /x:

s{ / [^/]+ / [^/]+ $ }{/lib/perl}x
choroba
  • 200,498
  • 20
  • 180
  • 248