0

So I'm bangin my head against the wall on this one, and honestly think I'm missing something simple. I'm also thinking that my issue is either something wrong with the herdoc or something wrong with how I'm using STDIN.

Anywho, when I run the following script on the command line(yes running as root), instead of printing to the file, it just prints to STDOUT, which is confusing the hell out of me.

sub do_stuff {
    my $resp = <STDIN>;

    my $service_file = <<END_FILE;
[Unit]
Description = tc hack for perist across reboot
After = syslog.target network.target nss-lookup.target

[Service]
Type = simple
ExecStart = /etc/tc/tcconfig.sh
ExecReload = /bin/kill -HUP \${MAINPID}
ExecStop = /bin/kill -INT \${MAINPID}
TimeoutSec = 30
Restart = on-failure
LimitNOFILE = 32768

[Install]
WantedBy = multi-user.target

END_FILE
    my $service_path = '/etc/systemd/system/multi-user.target.wants/tc.service';

    open(my $sfile, ">", $service_path)
      || die "can't open file for write ($service_path) $!";
    print $service_file;
    close $sfile;

}

And the output of this to the command line is:

[Unit]
Description = tc hack for perist across reboot
After = syslog.target network.target nss-lookup.target

[Service]
Type = simple
ExecStart = /etc/tc/tcconfig.sh
ExecReload = /bin/kill -HUP ${MAINPID}
ExecStop = /bin/kill -INT ${MAINPID}
TimeoutSec = 30
Restart = on-failure
LimitNOFILE = 32768

[Install]
WantedBy = multi-user.target

The output is being printed to the command line rather than the file. No idea why. Any help much appreciated!

Cheers

Rooster
  • 9,954
  • 8
  • 44
  • 71
  • 1
    See `print FILEHANDLE LIST` syntax in [`perldoc -f print`](http://metacpan.org/pod/perlfunc#print) – mob Jun 02 '14 at 17:03

2 Answers2

4

You need to specify the correct FH:

print $sfile $service_file;
ikegami
  • 367,544
  • 15
  • 269
  • 518
Pierre
  • 1,204
  • 8
  • 15
3

You open a file handle but don't print to it:

open(my $sfile, ">", $service_path)
      || die "can't open file for write ($service_path) $!";
print $service_file;
close $sfile;

Should be:

open(my $sfile, ">", $service_path)
      || die "can't open file for write ($service_path) $!";
print $sfile $service_file;
close $sfile;
Kevin Richardson
  • 3,592
  • 22
  • 14