in reply to Re: improve script
in thread improve script

print "$1\n" and exit if /.../

Funny to read, but not funny when printing fails for some reason:print and exit exists ONLY if print was successful.

>cat print-and-exit.pl #!/usr/bin/perl use strict; use warnings; close STDOUT if @ARGV; 1 and print "foo" and exit 0; die "Ooops, still here"; >perl print-and-exit.pl foo >perl print-and-exit.pl kaboom print() on closed filehandle STDOUT at print-and-exit.pl line 8. Ooops, still here at print-and-exit.pl line 9. >

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^3: improve script
by LanX (Saint) on Nov 28, 2015 at 21:36 UTC
    I must be too ignorant to see why STDOUT should be closed in a one-liner (???)

    Anyway it's true is that chaining with and is a lazy way to avoid the already mentioned scalar comma operator:

      print, exit if cond

    This seems to confuse many people, but has the same effect as

      do {print; exit} if cond

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

      I must be too ignorant to see why STDOUT should be closed

      Closing STDOUT was a simple way to make print fail, nothing more. There are several other ways how print can fail. A simple one is running out of disk space while STDOUT is redirected to a file. But I was too lazy to show creating, formatting, mounting, and filling a loop device just to show that print may fail. A third one is a disappearing block device - unplugging the wrong USB stick or harddisk. Number four: a failing disk. Number five: Disk quotas. Number six: Network disconnected while writing to a remote filesystem.

      in a one-liner (???)

      I never limited my post to one-liners. print and exit is problematic in one-liners as well as in scripts and modules, because it does not always exit, but it may look like it does. And, as posted in Re^5: improve script, the proper way to terminate a script with an error message is to use die. It writes the error message to where it belongs: STDERR.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^3: improve script
by choroba (Cardinal) on Nov 28, 2015 at 00:40 UTC
    So, what's your solution?
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      if ( somecondition ) { print "..."; exit; }

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Or just:

        print( "$1\n" ), exit if /.../;

        Or even:

        die "$!\n" if /.../;

        Which seems to capture the semantics perfectly.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
        It wouldn't print anything if STDOUT was closed, though.
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,