ayunt
ayunt

Reputation: 3

How to call shell from perl script

Perl script reads url from config file. In config file data stored as URL=http://example.com. How can I get site name only. I've tried

open(my $fh, "cut -d= -f2 'webreader.conf'");

But it doesn't work.

Please, help!

Upvotes: 0

Views: 96

Answers (2)

mpapec
mpapec

Reputation: 50637

You have to indicate with reading pipe -| that what follows is command which gets forked,

open(my $fh, "-|", "cut -d= -f2 'webreader.conf'") or die $!;

print <$fh>; # print output from command

Better approach would be to read file directly by perl,

open( my $fh, "<", "webreader.conf" ) or die $!;
while (<$fh>) {
    chomp;
    my @F = split /=/;
    print @F > 1 ? "$F[1]\n" : "$_\n";
}

Upvotes: 5

Yuras
Yuras

Reputation: 1

Maybe something like this?

$ cat urls.txt 
URL=http://example.com
URL=http://example2.com
URL=http://exampleXXX.com

$ ./urls.pl 
http://example.com
http://example2.com
http://exampleXXX.com

$ cat urls.pl 
#!/usr/bin/perl

$file='urls.txt';
open(X, $file) or die("Could not open  file.");

while (<X>) { 
    chomp;
    s/URL=//g;
    print "$_\n"; 
}
close (X); 

Upvotes: 0

Related Questions