![]() |
Perl and "->"
Can anyone please explain to me exactly how this "->" works and what
it's for? I've relatively new to perl, and I have a decent understanding of it, but not sure what that means. I'm guessing it's used to pass values to modules... or something? Take this sub routine for example: sub OnInit { my( $this ) = @_; my $frame = Wx::Frame->new( undef, -1, 'wxPerl', wxDefaultPosition, [ 200, 100 ] ); $frame->{TXT} = Wx::TextCtrl->new( $frame , -1, ''); $frame->Show( 1 ); download( $frame, "http://cpan.org/modules/ 01modules.index.html" ); } |
Re: Perl and "->"
In article
<70d21c5d-dfe4-42de-b296-cc1cb2603ccd@a5g2000prg.googlegroups.com>, Steve <steve@staticg.com> wrote: > Can anyone please explain to me exactly how this "->" works and what > it's for? I've relatively new to perl, and I have a decent > understanding of it, but not sure what that means. I'm guessing it's > used to pass values to modules... or something? > > Take this sub routine for example: > > sub OnInit { > my( $this ) = @_; > > my $frame = Wx::Frame->new( undef, -1, 'wxPerl', > wxDefaultPosition, [ 200, 100 ] ); > $frame->{TXT} = Wx::TextCtrl->new( $frame , -1, ''); > $frame->Show( 1 ); > download( $frame, "http://cpan.org/modules/ > 01modules.index.html" ); > } The '->' is a way of dereferencing a reference. It can be used in several ways (that I can think of): 1. Deferencing a reference to a hash or array: $hashref->{key} is equivalent to ${$hashref}{key} 2. Dereferencing a call to a subroutine: my $subref = sub { print "$1\n"; }; $subref->('Print this'); 3. Calling object methods (objects are blessed references): $frame->Show(1); 4. Callign package functions: my $frame = Wx::Frame->new( undef, ... ); which is (almost) equivalent to: my $frame = Wx::Frame::new( Wx::Frame, undef, ... ); i.e., the package is the first argument passed to the new() function. Your example has 3 of these 4 uses. -- Jim Gibson |
Re: Perl and "->"
On 2/8/2010 7:23 PM, Jim Gibson wrote:
> which is (almost) equivalent to: > > my $frame = Wx::Frame::new( Wx::Frame, undef, ... ); er, prefer Wx::Frame::new(Wx::Frame::, ...) if you must. Wx::Frame might refer to function Frame in package Wx. The trailing :: also implicitly quotes, iirc. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe" |
| All times are GMT. The time now is 11:35 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.