scalar polymorphism
jeteve
created: 2006-08-02 11:13:44
Hi Wise monks.

I've got one question about scalar polymorphism:

How do I do to set up a scalar value eg '$foo' which has a numeric value in a numeric context and has a string value in a string context ?

For instance, I'd like to do something like that:


  my $err = ???? ;

  my $num = $err + 0 ;
  print $num ; # Will print lets say the number '1' ;
  print $err ; # Will print the string    'ERROR 1' ;

I saw this type of behaviour in BerkeleyDB module and I wonder how it works.

Thanks for your precious help.

-- Nice photos of naked perl sources here !

Re: scalar polymorphism
created: 2006-08-02 11:20:12

You probably want dualvar from Scalar::Util

/J\

Re: scalar polymorphism
created: 2006-08-02 11:21:31

Use "dualvar" from Scalar::Util.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Re: scalar polymorphism
created: 2006-08-02 12:22:41
I have a suggestion: use an object instead. It's a lot less sneaky. If you want an error object, try Exception::Class.
Re^2: scalar polymorphism
created: 2006-08-02 15:48:58

++

Absolutely. I'm not a fan of doing funky things that you can't immediately see in code. It's a maintenance headache. Sometimes it's fun to do, but if you can avoid it, that's a good thing. It's one of the reasons I rarely use tied variables.

Cheers,
Ovid

New address of my CGI Course.

perlmonks.org content © perlmonks.org and davorg, gellyfish, jeteve, Ovid, perrin

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03