Assigning PWD last value to variable in perl module

227 Views Asked by At

i wanted to assign to variable $run in perl module and to have check in if condition. Basically the $run variable is assigned with last value of the current working directory and in if condition if the perl script is checking with value assigned with variable $run

part of perl module code script snippet where exact issue has been seen.

ex.pm
-----
 my $run = ${PWD##*/};
  if ( $2 ne $run)
  {
    die "$0 should be run in a $run directory.";
  }

with this code i am getting compilation issue. same thing if directly have if condition with last path value hardcoded its working good if ( $2 ne '4').

compilation issue messages

syntax error at bin/ex.pm line 44, near ")
  {"
syntax error at bin/ex.pm line 49, near "$sitepath "
BEGIN not safe after errors--compilation aborted at bin/TestConstants.pm line 72.
Compilation failed in require at bin/ex1.pm line 13.
BEGIN failed--compilation aborted at bin/ex1.pm line 13.
Compilation failed in require at ./bin/test.pl line 26.
BEGIN failed--compilation aborted at ./bin/test.pl line 26 (#1)
    (F) Probably means you had a syntax error.  Common reasons include:

        A keyword is misspelled.
        A semicolon is missing.
        A comma is missing.
        An opening or closing parenthesis is missing.
        An opening or closing brace is missing.
        A closing quote is missing.
1

There are 1 best solutions below

2
choroba On BEST ANSWER

You can't use bash syntax in Perl.

$run = ${PWD##*/};

The right hand side is a bash way of saying "Remove everything up to the last slash", which in Perl is written as

$run = $ENV{PWD} =~ s{.*/}{}r;

or

$run = substr $ENV{PWD}, 1 + rindex $ENV{PWD}, '/';