in reply to ref, no, maybe?
I say that the only valid use of ref() is doing something like ! ref($r). I think it is just fine to have string manipulating code that refuses to work on a reference (if you really wanted it to do that, then you can stringify the reference before passing it in). So your code would be fine with me if you wrote it either like this:
but I'd prefer this:if ( ref( $value ) ) { # <- that's a ref! # We have multiple values for this key foreach ( @$value ) { my $array_value = uri_escape( $_, $cgi_chars ); $query_string .= "$key=$array_value&"; } } else { $value = uri_escape( $value, $cgi_chars ); $query_string .= "$key=$value&"; }
but note that these will die fatally and rather confusingly if given a hash reference.if ( ! ref( $value ) ) { $value = uri_escape( $value, $cgi_chars ); $query_string .= "$key=$value&"; } else { # We have multiple values for this key foreach ( @$value ) { my $array_value = uri_escape( $_, $cgi_chars ); $query_string .= "$key=$array_value&"; } }
The correct substitute for ref() is UNIVERSAL::isa, as in:
if( UNIVERSAL::isa( $r, "ARRAY" ) ) { # access @$r here } elsif( UNIVERSAL::isa( $r, "HASH" ) ) { # access %$r here
The problem with "HASH" eq ref($r) is that it will fail if $r is a reference to a blessed hash. Why would you want to refuse to do useful hash stuff on something just because it is blessed?
I also don't approve of using:
as this could break if stringification is overloaded.if( ref($r) && "$r" =~ /(^|=)HASH/ ) {
Another misuse of ref() is the old:
I'd probably leave such a test out completely most of the time, but if you want to do that, then isa() is the right tool:sub method { my $self= shift; die "Invalid object" unless ref($self) eq "My::Package";
The awkward syntax is required because you can't call a method on an unblessed reference (and calling a method on a non-reference tries to treat it like a string containing a package name, which is only what you want here for a class method such as a constructor).die "Invalid object" unless UNIVERSAL::isa( $self, __PACKAGE__ ); # or die "Invalid object" unless eval { $self->isa(__PACKAGE__) };
By the way, how do you tell if a reference is blessed without using the fragile hack of:
? - tye (but my friends call me "Tye")if( ref($r) && "$r" =~ /=/ )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (tye)Re: ref, no, maybe?
by clintp (Curate) on Jan 11, 2001 at 05:35 UTC | |
by jeroenes (Priest) on Jan 11, 2001 at 14:04 UTC | |
by tye (Sage) on Jan 11, 2001 at 19:35 UTC | |
by tye (Sage) on Jan 11, 2001 at 05:45 UTC | |
|
Re: (tye)Re: ref, no, maybe?
by jlp (Friar) on Jan 11, 2001 at 06:33 UTC | |
by tye (Sage) on Jan 11, 2001 at 06:44 UTC | |
by chromatic (Archbishop) on Jan 11, 2001 at 07:07 UTC | |
by tye (Sage) on Jan 11, 2001 at 08:00 UTC | |
by jlp (Friar) on Jan 11, 2001 at 07:01 UTC | |
by tye (Sage) on Jan 11, 2001 at 10:30 UTC | |
|
Re (tilly) 2: ref, no, maybe?
by tilly (Archbishop) on Jan 11, 2001 at 06:08 UTC | |
by tye (Sage) on Jan 11, 2001 at 06:16 UTC |