Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl regex with shell

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.

like image 700
trying_to_program Avatar asked Dec 07 '25 05:12

trying_to_program


2 Answers

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};
like image 53
toolic Avatar answered Dec 08 '25 21:12

toolic


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.)

like image 37
1111161171159459134 Avatar answered Dec 08 '25 21:12

1111161171159459134