my $dirs = qx(echo \$PATH);
my @arr = $dirs ~= //(.*):;
I know the regex has to first find a "/" and then a ":". I thought my regex would do that; however, I keep getting that the search pattern is not terminated. Any help would greatly be appreciated.
There is no need to invoke the shell since Perl makes all environment variables available in its %ENV hash.
Use split to extract the colon-separated directory names from the environment variable:
my @dirs = split /:/, $ENV{PATH};
I keep getting that the search pattern is not terminated
Your code dirs ~= //(.*): is indeed bizarre: you start a regex with / and you immediately close it: //.
You could use m## to easily know where it starts and where it ends, like this :
my $dirs = qx(echo \$PATH);
my @arr = $dirs =~ m#([^:]+)(?::|$)#g;
(By the way, @toolic's answer is very good.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With