http://www.perlmonks.org?node_id=1186015

Sometimes you get spoiled by IDE's that have F5 to save and run what you have scripted so far... so... what can you do if you have 2 xterms (one for vi, the other for the output)?

perl -E 'while(-f $ARGV[0]){ $now=(stat(_))[9]; system($^X,@ARGV) if($ +now-$prev); $prev=$now; sleep 1}' /home/user/test.pl foo bar

with test.pl having:

#! env perl my $p1 = $ARGV[0]; my $p2 = $ARGV[1]; print "param1=$p1 param2=$p2\n";

yields:

param1=foo param2=bar

Tested to work under Win10 and Linux

Of course, there are better implementations. inotifywait or auditd if available on your system...

any perl golfers?

Update: we now incorporate the improvement made by haukex. Feel free to add more parameters if you need these

Replies are listed 'Best First'.
Re: oneliner: autorun script when I save it in the editor
by haukex (Archbishop) on Mar 27, 2017 at 07:59 UTC

    Nice! The only small change I'd suggest is using system("perl",$f) (or even system($^X,$f)) instead of say `perl "$f"`, since the former will allow for live output (although you may still need $|=1; in the called script).

      excellent idea. Hmm... seems to work without flushing...
Re: oneliner: autorun script when I save it in the editor
by LanX (Saint) on Mar 26, 2017 at 23:29 UTC
    Do you still get all errors in an extra pane of your editor such that you can click and jump on them?

    > Sometimes you get spoiled by IDE's that have F5 to save and run what you have scripted so far... 

    Spoiled? Shouldn't that be standard?

    update

    Sorry I'm no vi user and not sure what I'm missing here. ..

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Well, I was relegated to AIX systems through putty... and not many rights to change the system. The X worked, so you could run an editor but the delay in speed was frustrating, so commandline was better (changes of 1 byte instead of updates of pixel areas). Now, you could work locally, in say, notepad (or word, but notepad is better), then paste it in the xterm, dos2unix it (remove ^M) and run it... that was also tested by me, and a not so good idea.

      If you have an IDE (codeblocks, padre, notepad++, code, sublime) you have so many possibilities... but I'm sure they can not save remotely. I had to work like this: Linux laptop (with nice IDE's) -> Windows VM (with nice IDE's) -> Citrix VPN -> remotedesktop -> putty (or notepad/wordpad) -> aix-server

      So the first time, you program locally, sure grep -p (grep a paragraph) should work on Linux... ehrm... nope, there are ugly -A and -B's... and the perl on AIX is ancient (and no internet -> no CPAN...)

      On another note: Here is my padre on my debian system (and the reason I code in Notepad++ /Wine):

      fbrm@debian:~$ padre 09:56:22 PM: Warning: Cannot set locale to language "English (U.K.)". 09:56:22 PM: Error: locale 'en_GB' cannot be set. DBD::SQLite::db do failed: Safety level may not be changed inside a tr +ansaction at (eval 1905) line 37. Perl exited with active threads: 1 running and unjoined 0 finished and unjoined 0 running and detached
        I'm not sure if I understand everything but emacs can do both

        • safe remotely (via tramp node)
        • be used via ssh in an xterm/putty ( -nw )

        and it has a decent vim emulation (evil mode)

        See [EMACS] "Emacs as Perl IDE" - Abstract for YAPC::EU 2016 (slides, video , discussion)

        I don't know much about modern emacs binaries for AIX usable without root rights. (Though your remarks about ancient Perl there is irritating me)

        Concerning IDEs YMMV, as usual. :)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

        PS I did the same talk for London Perl Workshop, but I have problems locating the video.

Re: oneliner: autorun script when I save it in the editor
by reisinge (Hermit) on Mar 30, 2017 at 06:06 UTC

    Nice one! I modified it a bit to compile and run a C program:

    perl -E 'while(-f $ARGV[1]){ $now=(stat(_))[9]; system("@ARGV"), print + "-" x 80, "\n" if ($now-$prev); $prev=$now; sleep 1 }' gcc a.c \&\& +./a.out
    I think every Unix/Linux sysadmin should know shell (sh or bash) plus one of Perl, Ruby, Python. It doesn't matter which. -- Tom Limoncelli (http://everythingsysadmin.com/2012/06/salang.html)
      The delimiter line is a nice touch...

      And using $ARGV[1] instead of $ARGV[0] is good thinking! I now use it more as a perl -wc thingy... so I save, and know if the added code is syntax-error free (of course, most of the times it is still buggy)