in reply to Re^3: url get with string
in thread url get with string

here is a working code, anyone can try

my $req = HTTP::Request->new(GET => 'https://www.google.com/finance/co +nverter?a=3&from=USD&to=EUR'); $req->content_type('application/json'); my $re = $ua->request($req); $re->content =~ m|<span class=bld>(.+?)</span>|i and $span = $1; $ct = substr($1, 0, -6); print $ct; prints 2.77 according to the currency usd to euro

but when you try to put amount from string $m = "3";

https://www.google.com/finance/converter?a=$m&from=USD&to=EUR

doesn;t work correctly

Replies are listed 'Best First'.
Re^5: url get with string
by huck (Prior) on Mar 23, 2017 at 12:43 UTC

    People have tried to explain but we are not getting thru to you, look at this

    use strict; use warnings; use LWP; my $ua = new LWP::UserAgent(); { # In this case you have 2 literal strings enclosed in single quotes # with the variable $m concatenated in the middle. # I like that way best because you are assured text is text and variab +les are variables. # my $m=3; my $uri = 'https://www.google.com/finance/converter?a='.$m.'&from=USD& +to=EUR'; my $req = HTTP::Request->new(GET => $uri); $req->content_type('application/json'); my $re = $ua->request($req); $re->content =~ m|<span class=bld>(.+?)</span>|i;# and $span = $1; my $ct = substr($1, 0, -6); print 'ct:'.$ct."\n"; print $uri."\n"; } { # Here you have one string, # but because it is in double quotes perl symbols/expressions get in +terpolated inside. # Some think it is cleaner, # but things you dont expect can change # because perl sees them as something to replace. # my $m=3; my $uri="https://www.google.com/finance/converter?a=$m&from=USD&to=EUR +"; print $uri."\n"; } { # a third way, as suggested by [haukex] # In this case many problems associated with special characters in the + uri are avoided. use URI; my $m=3; my $uri =URI->new('https://www.google.com/finance/converter'); $uri->query_form( a => $m, from => 'USD', to => 'EUR', ); print $uri."\n"; }
    Notice how all 3 printed uris are the same and what you want to see
    D:\goodies\pdhuck\down1\perl\monks>perl uri.pl ct:2.78 https://www.google.com/finance/converter?a=3&from=USD&to=EUR https://www.google.com/finance/converter?a=3&from=USD&to=EUR https://www.google.com/finance/converter?a=3&from=USD&to=EUR
    Do you get it yet? I only ran the first method but sine all 3 printed uris are the same any of the 3 methods would work the same.

      thanks huck. thanks everyone i understand it now