Perl and "->"

Discussion in 'Perl Misc' started by Steve, Feb 8, 2010.

  1. Steve

    Steve Guest

    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" );
    }
     
    Steve, Feb 8, 2010
    #1
    1. Advertising

  2. Steve

    Jim Gibson Guest

    In article
    <>,
    Steve <> 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
     
    Jim Gibson, Feb 9, 2010
    #2
    1. Advertising

  3. Steve

    sreservoir Guest

    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"
     
    sreservoir, Feb 9, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,842
  2. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    323
    PerlFAQ Server
    Jan 23, 2011
  3. PerlFAQ Server
    Replies:
    0
    Views:
    713
    PerlFAQ Server
    Feb 3, 2011
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    317
    PerlFAQ Server
    Feb 27, 2011
  5. Dilbert
    Replies:
    0
    Views:
    859
    Dilbert
    Nov 10, 2011
Loading...

Share This Page