Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Readonly error on $_

by merlyn (Sage)
on Jan 05, 2001 at 20:37 UTC ( [id://50096] : note . print w/replies, xml ) Need Help??

in reply to Readonly error on $_

Not commenting on the larger task of what you're trying to do, the smaller task you're trying to do is clobbering $_. I'd rewrite that as:
sub selfcontrol { my $self = shift; defined $_ and length $_ and not (/REF/ or /SCALAR/ or /ARRAY/ or /HASH/ or /CODE/ or /GLOB/) a +nd return $_ for ref $self; carp "diem"; }
But it's probably simpler to use
sub selfcontrol { my $self = shift; return $self if eval {$self->can("can")}; # Only objects can dance! carp "diem"; }

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Readonly error on $_
by jeroenes (Priest) on Jan 06, 2001 at 02:02 UTC
    Thanks, merlyn!

    Nice rewrites. Very nice use of for. I think I should that construction a much more many times in my code!!

    The second one, with the can, requires some more study from my side. Will dive into those perldocs! Or maybe you mean to use a real method instead of can? I wouldn't think that will work, as I plan to use this for more classes. Update: immediately found can in perlobj. It's all clear now.

    Thanks again,

    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

(tye)Re: Readonly error on $_
by tye (Sage) on Jan 11, 2001 at 19:38 UTC

    Note that your second example doesn't test for an object but for an object or a package name and Perl considers most strings that don't contain control characters to be package names. This is easy to fix:

    sub selfcontrol { my $self = shift; return $self if ref($self) && eval {$self->can("can")}; carp "diem"; }

            - tye (but my friends call me "Tye")