How can both be true: $! == 10 && $! eq 'No child processes'?

Discussion in 'Perl Misc' started by Peter Valdemar Morch, Jun 22, 2004.

  1. I can see from perldoc perlvar about $!:

    "If used numerically, yields ..."
    and
    "If used as a string, yields ..."

    Is there any way I can do this for my own variables, say $a - to let
    it have one value if used numerically, and another if used as a
    string? It tastes a little like wantarray for subs, but... Is this
    very, very special for $!?

    Peter
     
    Peter Valdemar Morch, Jun 22, 2004
    #1
    1. Advertising

  2. On 22 Jun 2004, Peter Valdemar Morch wrote:

    >Is there any way I can do this for my own variables, say $a - to let
    >it have one value if used numerically, and another if used as a
    >string? It tastes a little like wantarray for subs, but... Is this
    >very, very special for $!?


    There are two ways to have it done, generally speaking:

    1. make $foo an object of a class that overloads stringification and
    numification

    2. muck with the internal representation of $foo so that Perl thinks it
    has a PV (string value) and an unrelated NV (numeric value)

    Here's a brief example of the first:

    package StrNum;
    use overload ( '""' => 'str', '0+' => 'num', fallback => 1 );
    sub new {
    my $class = shift;
    bless [ @_ ], $class;
    }
    sub str { $_[0][0] }
    sub num { $_[0][1] }
    1;

    It's used thus:

    use StrNum;
    my $both = StrNum->new("string", 100);
    if ($both eq "string" and $both == 100) { print "ooh!" }

    There is probably already a module on CPAN that employs this mechanism.

    The second way is faster to employ because it has nothing to do with
    objects and overloading, and works by fiddling with Perl's representation
    of your scalar. The Scalar::Util module includes a dualvar() function
    that does just that:

    dualvar NUM, STRING
    Returns a scalar that has the value NUM in a numeric context and
    the value STRING in a string context.

    $foo = dualvar 10, "Hello";
    $num = $foo + 2; # 12
    $str = $foo . " world"; # Hello world

    (Ok, so I had the order the other way around in my sample module above.)

    --
    Jeff Pinyan RPI Acacia Brother #734 RPI Acacia Corp Secretary
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Jun 22, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,163
    David Schwartz
    Dec 9, 2003
  2. ABCL
    Replies:
    0
    Views:
    569
  3. bdb112
    Replies:
    45
    Views:
    1,377
    jazbees
    Apr 29, 2009
  4. Marc Heiler
    Replies:
    1
    Views:
    183
    Robert Klemme
    May 24, 2009
  5. Luca Cerone
    Replies:
    13
    Views:
    149
    Tobiah
    Aug 5, 2013
Loading...

Share This Page