Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(tye)Re: $! context behavior

by tye (Sage)
on Aug 01, 2001 at 21:49 UTC ( #101455=note: print w/replies, xml ) Need Help??


in reply to $! context behavior

You can use overload to simulate this behavior. But SetDualVar lets you build scalars the same way that $! is built, which it quite a bit less overhead (as if that matters). The module lets you set both the numeric and string value of a scalar without the two having to agree.

Note that tie can't distinguish string and numeric "sub-contexts" and so isn't enough by itself to accomplish this. But you can use tie in conjunction with either overload or SetDualVar to do this (or just use overload by itself).

And, if you really want to have it act just like $!, then a handy way to look up error strings from error numbers:

sub getErrorText { local( $! )= 0+shift(@_); return "$!"; }

But that also made me realize that you don't even need a module to create a dual-valued scalar that duplicates $! since

$errno= $!= 5; print 0+$errno," $errno\n";
prints something like "5 Input/output error".

Also note that getting the type of "copy semantics" you want may be the determining factor in which method to use.

        - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://101455]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-05-22 22:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?