0

I am new to Perl and trying to make a script that takes input from the user and then get XML data from a website based on that input together with a url and then relay it back to the user.

But I have had some issues now with make a usable link based on the input from the user.

This is my code in full:

use strict;
use warnings;
use utf8;

my $row = 0;

use XML::LibXML;

print "\n\n\nOn what place do you need a weather report for? -> ";

my $ort = <>;

my $url1 = "http://www.yr.no/place/Sweden/Västra_Götaland/";
my $url2 = "/forecast_hour_by_hour.xml";

my $url = join('', $url1,$ort,$url2);

my $dom = XML::LibXML->load_xml(location => $url);

print "\n\nSee below the weather for ", $ort, ":\n\n";

foreach my $weatherdata ($dom->findnodes('//time')) {

    if($row != 10){ 

        my $temp = $weatherdata->findvalue('./temperature/@value');
        my $value = $weatherdata->findvalue('./@from');

        my $valuesub = substr $value, 11, 5;

        print "At ", $valuesub, " the temperature will be: ", $temp, "C\n";

        $row++;
    }
}

print "\n\n";

The crusial bit I assume is this one:

my $ort = <>;

my $url1 = "http://www.yr.no/place/Sweden/Västra_Götaland/";
my $url2 = "/forecast_hour_by_hour.xml";

my $url = join('', $url1,$ort,$url2);

my $dom = XML::LibXML->load_xml(location => $url);

When I run it I get this: enter image description here

It enters a line break and then the link is broken.

I think the issue lies with $ort cause when I try to print only $ort out with something comming after I get a line break aswell.

I hope I explained it clearly or you can perhaps tell me what needs improvement.

moiamserru
  • 71
  • 8
  • Also missing is `use open ':std', ':encoding(UTF-8)';`, plus some encoding when building the URL. – ikegami Jun 09 '18 at 04:51
  • Thanks for the tip. I would gladly take tips on how to sort out my encoding problem with the url if you have the time. The use you mentioned created a whole new type of errors to my code. When adding as input: Åmål I got this for response: utf8 "\x8F" does not map to Unicode at test4.pl line 11. utf8 "\x86" does not map to Unicode at test4.pl line 11. – moiamserru Jun 09 '18 at 07:11
  • Not enough info. – ikegami Jun 09 '18 at 07:12
  • Also gets Could not create file parser context for file "http://www.yr.no/place/Sweden/V├â┬ñstra_G├â┬Âtaland/\x8Fm\x86l/forecast_hour_by_hour.xml": No error at test4.pl line 18 – moiamserru Jun 09 '18 at 07:17
  • Using same code as above with your addition you posted 2 hours ago. – moiamserru Jun 09 '18 at 07:18
  • My addition? I didn't provide code for it. Not the place to be asking new questions! – ikegami Jun 09 '18 at 07:20
  • 1
    What? You suggested adding `use open ':std', ':encoding(UTF-8)';` to my code. you also said I was missing some encoding when building a URL. – moiamserru Jun 09 '18 at 07:21

1 Answers1

2

That newline comes from your terminal. You can use the chomp function to remove it:

my $ort = <>;
chomp($ort);

It is usual to see this alternatively written in one line, which gives the same result:

chomp( my $ort = <> );
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • Thank you! Worked like a charm! Now I only have some encoding issues to dig into :P – moiamserru Jun 09 '18 at 07:12
  • 1
    I’m happy it sorted out that part! About the encoding, check the following question. The answer by tchrist is a great collection and reference for the settings we often use: https://stackoverflow.com/questions/6162484 – sidyll Jun 09 '18 at 16:10