Reputation: 489
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
Upvotes: 0
Views: 184
Reputation: 126722
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";
Upvotes: 1
Reputation: 27423
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
Upvotes: 0