Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl and "->"

Reply
Thread Tools

Perl and "->"

 
 
Steve
Guest
Posts: n/a
 
      02-08-2010
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" );
}
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      02-09-2010
In article
<(E-Mail Removed)>,
Steve <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
sreservoir
Guest
Posts: n/a
 
      02-09-2010
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ActiveState Perl and MinGW [was: Perl 5.14 Windows Strawberry Perl 64 bits] Dilbert Perl Misc 0 11-10-2011 02:20 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments