Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Perl and "->" (http://www.velocityreviews.com/forums/t912379-perl-and.html)

Steve 02-08-2010 11:13 PM

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" );
}

Jim Gibson 02-09-2010 12:23 AM

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

sreservoir 02-09-2010 12:37 AM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57