Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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")

In reply to (tye)Re: $! context behavior by tye
in thread $! context behavior by eak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-05-19 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (72 votes). Check out past polls.

    Notices?