"David Storrs" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> I've got a function that takes several arguments, the first of which
> should be a scalar (specifically, a string). I'd like to have a
> precondition to verify that the argument is, in fact, a scalar. Is
> there a way to do that (preferably without using modules--I'm trying
> to write an entirely self-contained script).
>
> I could make it take a REFERENCE to a scalar, and then do "ref $_[0]
> eq 'SCALAR'", but that's not really what I'm looking for.
>
> The value may have been specified as a literal, so I can't do
> anything using the *{foo}{SCALAR} syntax (even if I had the name of
> the variable and not its value).
>
you could implement your own 'scalar' type and check for that:
#!/usr/bin/perl -w
package RScalar;
use strict;
#--
#-- min. for domenstrating purpose
#--
sub TIESCALAR{bless \my $i=>$_[0]}
sub FETCH{$$[0]}
sub STORE{$$[0] = $_[1]}
1;
package main;
tie my $v1,'RScalar';
tie my $v2,'RScalar';
tie my $v3,'RScalar';
check_var($v1);
check_var($v2);
check_var($v3);
check_var(*STDIN);
check_var([]);
check_var({});
sub check_var{
my $type = tied $_[0];
if($type && $type =~ /rscalar/i){
print "you give me the right thing!\n";
}else{
print "i don't want $_[0]\n";
}
}
__END__
prints:
you give me the right thing!
you give me the right thing!
you give me the right thing!
i don't want *main::STDIN
i don't want ARRAY(0x876fccc)
i don't want HASH(0x8771034)
there are many disadvantages (performance and flexibility being 2 of the
biggest) with this approach, of course. hopefully, it gives you a different
approach.
perldoc perldtie
david
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]