Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl and "->"

1 view
Skip to first unread message

Steve

unread,
Feb 8, 2010, 6:13:15 PM2/8/10
to
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" );
}

Ben Morrow

unread,
Feb 8, 2010, 7:06:58 PM2/8/10
to

Quoth Steve <st...@staticg.com>:

> 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?

'->' is one of the dereference operators (see perlreftut and perlref).
One of its uses is to call methods (see perlboot). This form is often
spelled '.' in other languages (Java and JS, for instance).

> Take this sub routine for example:
>
> sub OnInit {
> my( $this ) = @_;
>
> my $frame = Wx::Frame->new( undef, -1, 'wxPerl',
> wxDefaultPosition, [ 200, 100 ] );

This is a call to the 'new' method of the Wx::Frame class. You would
need to look at the documentation for Wx::Frame to see what that does.

> $frame->{TXT}

This assumes $frame contains a ref to a hash, and returns the "TXT"
member of that hash.

> $frame->Show( 1 );

This assumes $frame contains an object, and calls the 'Show' method of
that object. You would need to look at the documentation for whatever
class $frame belongs to (Wx::Frame in this case) to see what that does.

Ben

Jim Gibson

unread,
Feb 8, 2010, 7:23:46 PM2/8/10
to
In article
<70d21c5d-dfe4-42de...@a5g2000prg.googlegroups.com>,
Steve <st...@staticg.com> wrote:

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

unread,
Feb 8, 2010, 7:37:40 PM2/8/10
to
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"

0 new messages