moiamserru
moiamserru

Reputation: 81

Getting unwanted line break when trying to build a string of multiple variables

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.

Upvotes: 0

Views: 75

Answers (1)

sidyll
sidyll

Reputation: 59297

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 = <> );

Upvotes: 2

Related Questions