1

I want to fetch the clientroot of perforce in a perl script.

I wrote the code as :

my $workspace_root=system("p4 client -o | findstr /b Root");
print $workspace_root;

Instead of printing the path, it prints 0

Could anyone please tell what I am missing here?

serenesat
  • 4,611
  • 10
  • 37
  • 53
Vishal Khemani
  • 171
  • 2
  • 13

2 Answers2

1

Change system to backticks:

my $workspace_root = `p4 client -o | findstr /b Root`;
print $workspace_root;

If you want store the output of your command, use backticks. If you don't care about the output, use system.

See What's the difference between Perl's backticks, system, and exec? to understand system and backticks.

Community
  • 1
  • 1
serenesat
  • 4,611
  • 10
  • 37
  • 53
  • @serenesat: Please don't solicit up-votes and acceptances – Borodin Jul 19 '16 at 08:37
  • @Borodin: I don't find it wrong, as OP said it was helpful and it worked. http://meta.stackexchange.com/a/14999/282626 – serenesat Jul 19 '16 at 09:23
  • @serenesat: It is fair to leave a question at least a day or two in case another, better answer is written. It is a little shabby to press the OP to accept your own answer when the question is only an hour or two old. If you feel that someone may have simply got their answer and walked away without properly tidying up then you can post a link to [*What should I do when someone answers my question?*](http://stackoverflow.com/help/someone-answers) beneath the original question. – Borodin Jul 19 '16 at 09:27
  • @serenesat: Amongst other things, that links says *"Accepting an answer is not mandatory; do not feel compelled to accept the first answer you receive. Wait until you receive an answer that answers your question well."* – Borodin Jul 19 '16 at 09:29
  • @Borodin: I am not forcing OP to accept my answer. It was just a gentle reminder. Generally I don't care about upvotes and acceptance. Anyway I will keep your words in mind. :) – serenesat Jul 19 '16 at 09:39
1

I would do this:

my $workspace_root = `p4 -Ztag -F %Root% client -o`;
print $workspace_root;

so that the script will work on other platforms and won't be fooled by clients with "Root" in their name, description, View, etc.

Samwise
  • 68,105
  • 3
  • 30
  • 44