I have the following class :
package Link;
sub FETCH {
my $this = shift;
return $this->{"site"};
}
sub STORE {
my ($self,$site) = @_;
$self->{"site"} = $site;
}
sub print_method {
my $self = shift;
print $self->{"site"};
}
sub TIESCALAR {
my $class = shift;
my $link = shift;
my $this = {};
bless($this,$class);
$this->{"site"} = $link;
return $this;
}
1;
and the following code :
use Link;
tie my $var,"Link","http://somesite.com";
$var = "http://anothersite.com";
$var->print_method;
print $var;
Running the code above results in a runtime error : Can't call method "print_method" without a package or object reference at tietest.pl line 4. . I think I understand why : when fetched , the method returns a string , which doesn't respond to "print_method" . I would like to be able to do the following things : if fetched , print the site's address , if stored , change the address . I would also like to be able to call methods on the object . I am aware that if I return $self from fetch , I will accomplish the "call methods objective" , but that will invalidate the first objective ( fetch ) . Is there a way I could accomplish all of them ?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|