Thread Warum kein $@ statt $! (29 answers)
Opened by rosti at 2014-09-23 13:04

GwenDragon
 2014-09-23 11:29
#177472 #177472
User since
2005-01-17
14748 Artikel
Admin1
[Homepage]
user image
2014-09-23T08:03:16 rosti
Was mich ein bischen irritiert ist, dass der Autor der Lib zur Fehlerbehandlung $! durchreicht und das nicht auf $@ umlegt. Gibt es einen guten Grund dafür?
Weil der Programmierer nicht Perl so missbrauchen sollte.

perldoc zu $@:
Quote
$OS_ERROR
$ERRNO
$!

When referenced, $! retrieves the current value of the C errno integer variable. If $! is assigned a numerical value, that value is stored in errno . When referenced as a string, $! yields the system error string corresponding to errno .

Many system or library calls set errno if they fail, to indicate the cause of failure. They usually do not set errno to zero if they succeed. This means errno , hence $! , is meaningful only immediately after a failure:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
    if (open my $fh, "<", $filename) {
                # Here $! is meaningless.
                ...
    }
    else {
                # ONLY here is $! meaningful.
                ...
                # Already here $! might be meaningless.
    }
    # Since here we might have either success or failure,
    # $! is meaningless.


Here, meaningless means that $! may be unrelated to the outcome of the open() operator. Assignment to $! is similarly ephemeral. It can be used immediately before invoking the die() operator, to set the exit value, or to inspect the system error string corresponding to error n, or to restore $! to a meaningful state.

Note that when stringified, the text is always returned as if both use locale and use bytes are in effect. This is likely to change in v5.22.

Mnemonic: What just went bang?


perldoc zu $@:
Quote
$EVAL_ERROR
$@

The Perl syntax error message from the last eval() operator. If $@ is the null string, the last eval() parsed and executed correctly (although the operations you invoked may have failed in the normal fashion).

Warning messages are not collected in this variable. You can, however, set up a routine to process warnings by setting $SIG{__WARN__} as described in %SIG.

Mnemonic: Where was the syntax error "at"?


Genauer: Es wird $! verwendet, weil in $! der Fehler aus der Systemvariable errno steht und Net::SSH2 verwendet die System-Bibliothek libssh.



@rosti
Warum willst du eval verwenden?
Last edited: 2014-09-23 11:31:26 +0200 (CEST)

View full thread Warum kein $@ statt $!