0

I am relatively new to Perl. I am trying to store a URL in a variable. Here's my code:

my $port = qx{/usr/bin/perl get_port.pl};
print $port;
my $url = "http://localhost:$port/cds/ws/CDS";
print "\n$url\n";

This gives me the below output:

4578
/cds/ws/CDS

So the get_port.pl script is giving me the port correctly but the URL isn't getting stored properly. I believe there's some issue with the slash / but I am not sure how to get around it. I have tried escaping it with backslash and I have also tried qq{} but it keeps giving the same output.

Please advise.

Output for perl get_port.pl | od -a

0000000 nl 4 5 7 8 nl 0000006

9
  • perl yourscript.pl | hexdump -C might be helpful. (and how about qx{}?) Commented Apr 18, 2015 at 7:04
  • Please post the actual code of an example that we can run. The code in the question contains a fatal syntax error (the qx is not terminated), so it is not the code you ran to produce your results (because it won't run, period). Also, even if we could run it, we don't have your get_port.pl program, so we would get different results for reasons completely unrelated to your question. Commented Apr 18, 2015 at 7:55
  • Thanks for pointing that out Dave. I have modified it as per latest suggestions and it still doesn't work. I will post the get_port.pl code shortly. It's making some calls to an internal API after downloading java & setting a couple of env variables. Commented Apr 18, 2015 at 8:05
  • Can you show the result of /usr/bin/perl get_port.pl | od -c ? Commented Apr 18, 2015 at 8:43
  • I'm sorry the get_port.pl has a lot of internal code which I don't think I should post here. However, @Borodin's answer worked for me. Commented Apr 18, 2015 at 16:48

2 Answers 2

1

There is noithing wrong with your $url string. The problem is almost certainly that the $port string contains carriage-return characters. Presumably you are working on Windows?

Try this code instead, which extracts the first string of digits it finds in the value returned by get_port.pl and discards everything else.

my ($port) = qx{/usr/bin/perl get_port.pl} =~ /(\d+)/;
print $port, "\n";
my $url = "http://localhost:$port/cds/ws/CDS";
print $url, "\n";
Sign up to request clarification or add additional context in comments.

Comments

0

As @Сухой27 I think was trying to point out, you can use other character besides '/' with qx, to simplify syntax and then you don't have to escape the slashes.

I also added a default port 8080 in case get_port.pl does not exist.

This seems to work properly.

#!/usr/bin/perl -w
# make 8080 the default port
my $port = qx{/usr/bin/perl get_port.pl} || 8080;
print $port;
my $url = "http://localhost:$port/cds/ws/CDS";
print "\n$url\n";

output

paul@ki6cq:~/SO$ ./so1.pl 
Can't open perl script "get_port.pl": No such file or directory
8080
http://localhost:8080/cds/ws/CDS

2 Comments

Nope. Doesn't work for me. Still getting the same output. I do not understand how changing the code for getting port would affect how the url is being stored. Thanks anyways!
running perl -v here yields v5.20.2. However, the behavior reported isn't making much sense to me. It should be printing "http://..." instead of omitting the beginning and providing only the file path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.