Hello Dunkellbusch,

Welcome to the Monastery. The monks have already provided you with solutions to your problem, but I would like to add one more. You can use the module File::Slurp/append_file, so you can minimize your code to simply one call in every time you need it, no closing file handles etc.

But to be honest I would combine the module with the nice solution of fellow monk Corion, executable sample bellow below (change grep from /enable/ to /smb\.service/):

#!/usr/bin/perl use strict; use warnings; use File::Slurp; my $filePath = 'test_1.txt'; my @services = `systemctl list-unit-files`; if( my @smb = grep { /enabled/ } @services ) { map { s/\s+\z// } @smb; # remover white space append_file( $filePath, "chkconfig smb: OK\n"); append_file( $filePath, map { "$_\n" } @smb); # append_file( $filePath, "\n"); # if you want to append multiple +times }

Update: Based on comments try to avoid File::Slurp, for simple implementations (read, write, etc) you can use File::Slurper, unfortunately there the author has not added the append proposal. I was looking online and I found the IO::All. Never used it before but I read that it is really really good.

Sample of updated solution, based on commends:

#!/usr/bin/perl use strict; use warnings; use IO::All -utf8; # Turn on utf8 for all io my $path = 'file.txt'; my @services = `systemctl list-unit-files`; if( my @smb = grep { /enabled/ } @services ) { s{ ^\s* | \s*$ }{}gx for @smb; io($path)->appendln(@smb); }

Output:

$ cat test_1.txt chkconfig smb: OK acpid.path enabled cups.path enabled accounts-daemon.service enabled anacron-resume.service enabled anacron.service enabled apparmor.service enabled autovt@.service enabled avahi-daemon.service enabled bluetooth.service enabled console-setup.service enabled cron.service enabled cups-browsed.service enabled cups.service enabled dbus-org.bluez.service enabled dbus-org.freedesktop.Avahi.service enabled dbus-org.freedesktop.ModemManager1.service enabled dbus-org.freedesktop.nm-dispatcher.service enabled dbus-org.freedesktop.resolve1.service enabled dbus-org.freedesktop.thermald.service enabled denyhosts.service enabled display-manager.service enabled dns-clean.service enabled friendly-recovery.service enabled getty@.service enabled gpu-manager.service enabled keyboard-setup.service enabled lightdm.service enabled ModemManager.service enabled network-manager.service enabled networking.service enabled NetworkManager-dispatcher.service enabled NetworkManager-wait-online.service enabled NetworkManager.service enabled ondemand.service enabled openvpn.service enabled pppd-dns.service enabled repowerd.service enabled resolvconf.service enabled rsync.service enabled rsyslog.service enabled setvtrgb.service enabled snapd.autoimport.service enabled snapd.core-fixup.service enabled snapd.service enabled snapd.system-shutdown.service enabled ssh.service enabled sshd.service enabled syslog.service enabled systemd-resolved.service enabled systemd-timesyncd.service enabled thermald.service enabled ufw.service enabled unattended-upgrades.service enabled ureadahead.service enabled vboxautostart-service.service enabled vboxballoonctrl-service.service enabled vboxdrv.service enabled vboxweb-service.service enabled acpid.socket enabled apport-forward.socket enabled avahi-daemon.socket enabled cups.socket enabled snapd.socket enabled uuidd.socket enabled remote-fs.target enabled apt-daily.timer enabled motd-news.timer enabled snapd.refresh.timer enabled

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Open Filehandle once (updated) by thanos1983
in thread Open Filehandle once by Dunkellbusch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.