Question

I am trying to bless a string variable -- demonstrated in the code below. Bless only seems to work when I use a hash or array. Are you allowed to bless strings? If no, what can you bless? I have been debugging for a while, any help would be greatly appreciated. :-) If I making an error in my code please let me know what it is.

This is a perl file. The code is not finished, but it never reaches the "Page End" statement. So I have ceased to lengthen it. $FileInfo is an array define earlier read from a file but due to syntax gets garbled here.

here is the call to build ojbect reference

$page = new GeneratePages(0);

package GeneratePages;  
sub new  
{  
    my $class = shift;  
    my $pageContents = $FileInfo[shift];  
    bless $pageContents, $class;  
    return $pageContents;  
}
Was it helpful?

Solution

Bless only works on references. From perldoc bless:

This function tells the thingy referenced by REF that it is now an object in the CLASSNAME package.

So if you want to use a string as an object, you should pass a reference to it to bless:

my $s = "foo"; # $s is a scalar variable
my $o = bless \$s, $class; # $s is now an object in the $class package
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top