Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

GLOB reference problem

by jerryhone (Sexton)
on Oct 05, 2009 at 08:26 UTC ( #799196=perlquestion: print w/replies, xml ) Need Help??

jerryhone has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code snippet:

my($this) = FTP->new($RemoteHost, q(Timeout) => $Timeout ); bless($this); %{*$this} = ( %{*$this}, q(Rename) => $Rename, q(Recursions) => $Recursions, q(NoCheck) => $NoCheck, q(DirectCopy) => $DirectCopy, );

When the %{*$this} line is executed, I'm greeted with the following error...

Not a GLOB reference at line 85

This isn't my code, so any guidance on what this is telling me and what's needed to "fix" it would be appreciated.

Replies are listed 'Best First'.
Re: GLOB reference problem
by ELISHEVA (Prior) on Oct 05, 2009 at 08:47 UTC

    The contents of %{...} are expected to be a reference and instead you have a glob.

    Two changes I would make to the above code

    • Presumably FTP->new is returning an object reference, not a list, so my($this) should probably be simply my $this.
    • Assuming $this is an object reference and the object is implemented as a hash, %{*$this} should be simply %{$this} or even just %$this. See perlreftut for further information.

    However, it is generally not good programming practice to break the opacity of an object reference. I would look more carefully at the docs for the FTP module and rewrite the code so that the hash assignment is unnecessary.

    Best, beth

      A bit more background...this code was originally written to utilise Net::FTP. However, I've needed to modify it as I need to run the code without actually FTPing anything outside the server, so I've created my own dummy FTP package. FTP->new (mine) is returning a HASH reference, but it looks like Net::FTP->new returns a GLOB reference.

      What construction's needed in my 'new' subroutine to return a GLOB reference. I have:-

      sub new { my ($pkg, $server, $user, $timeout, @args) = @_; my $self = { %fields }; my $ftp = bless $self, $pkg; $ftp->server($server); $ftp->user($user); $ftp->timeout($timeout); $ftp->initialize(); return $ftp; }

        When you want to extend an existing class, you have two options: inheritance and aggregation. If you go the inheritance route your new method would look something like this:

        use base qw(Net::FTP); #5.8 #or for 5.10 and up #use parent qw(Net::FTP); sub new { my ($pkg, $server, $user, $timeout, @args) = @_; # some special class specific stuff # ... is params for super class structure my $self = $pkg->SUPER::new(...); $self->server($server); $self->user($user); $self->timeout($timeout); $self->initialize(); # some special class specific stuff # if SUPER::new is well behaved, $self will be a member # of the $pkg class even though it was built by the # the super class. return $self; }

        If you go the aggregation route you might do something like this:

        sub new { my ($pkg, $server, $user, $timeout, @args) = @_; # some special class specific stuff # ... is params for super class structure my $ftp = Net::FTP->new(...); $ftp->server($server); $ftp->user($user); $ftp->timeout($timeout); $ftp->initialize(); my $self = { ftpHandle => $ftp }; return bless($self, $pkg); }

        If you want your own class to be implemented using a hash, you would go with the aggregation approach since Net::FTP doesn't return a hash. If you want to return the same kind of implementation as Net::FTP, then you would follow the inheritance pattern. You might find the sections on inheritance and aggregation in perltoot worth a read.

        Best, beth

      The contents of %{...} are expected to be a reference and instead you have a glob.

      True, but that's not the cause of the error.

      The content of $this is expected to be a glob reference, but it's not.

Re: GLOB reference problem
by jethro (Monsignor) on Oct 05, 2009 at 12:32 UTC
    You can create a glob reference with the function gensym from Symbol like Net::FTP does (you can see that if you follow the chain of inheritance of the new() function in Net::FTP)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://799196]
Approved by broomduster
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2023-06-05 11:24 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (24 votes). Check out past polls.