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". ;-)
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
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". ;-)
| [reply] [d/l] [select] |
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,
| [reply] [d/l] |
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". ;-)
| [reply] [d/l] |
:D /(...)/ and print ... and exit | [reply] [d/l] |