Perl and "->"

S

Steve

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

Jim Gibson

Steve said:
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.
 
S

sreservoir

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top