user2298995
user2298995

Reputation: 2163

Merging variables on Perl

I'm very new to perl. I get a very annoying problem trying to merge two variables. When I do it on a simple script like:

$arg1 = "hello";
$arg2 = " world";
print $arg1.$arg2;

It seems to work fine. But when I'm trying to make it a bit more complex (reading a file into array and then adding the variable), it seems to instead of adding the second variable, it's replacing the first characters of the first variable instead.

Here's the code:

#!/usr/bin/perl -w
use LWP::Simple;
use Parallel::ForkManager;
use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;
if ( @ARGV == 0 ) {
        print "Usage: ./$PROG [TARGET] [THREADS] [LIST] [TIMEOUT]\n";
    exit;
}
my $host = $ARGV[0];
my $threads = $ARGV[1];
my $weblist = $ARGV[2];
my $timeout = $ARGV[3];
my $pm = Parallel::ForkManager->new($threads);
alarm($timeout);
open(my $handle, "<", $weblist);
chomp(my @webservers = <$handle>);
close $handle;

repeat:
for $target (@webservers) {
        my $pid = $pm->start and next;
                        print "$target$host\n";
                        get($target.$host);
        $pm->finish;
}
$pm->wait_all_children;
goto repeat;

So if the text file (list) would look like this:

www.site.com?url=
www.site.net?url=
etc

And the host variable is domain.com. So instead of having something like: www.site.com?url=domain.com, I keep having this: domain.comsite.com?url=. It's replacing the first characters of the first variable with the second variable. I can't get my head around it.

Any kind of help would be appreciate as I'm sure I'm just missing a small thing that would make me feel stupid later.

Thanks ahead, have a good day!

Upvotes: 0

Views: 139

Answers (1)

choroba
choroba

Reputation: 241758

Your input file probably contains the carriage return \r character. You can remove it by s/\r//g, or convert the input form MSWin to *nix by dos2unix or fromdos.

Upvotes: 6

Related Questions