zizu1985 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have question about diffirence between using colon vs => for arguments in subroutines. For example below two codes working both good. new soubroutine is contructor in my classic OO class.

sub new { my ($class,$args_for) = @_; my $self = bless {} => $class; $self->_initialize($args_for); return $self; } sub new { my ($class,$args_for) = @_; my $self = bless {} , $class; $self->_initialize($args_for); return $self; }

Regards,

Tom

Replies are listed 'Best First'.
Re: Difference between comma and => when calling subroutine
by Marshall (Canon) on Mar 14, 2017 at 22:48 UTC
    Adding to post from stevieb. There is nothing special about bless vs any other built in function.

    Here are some examples using print, to show the quoting ability before the =>

    #!/usr/bin/perl use strict; use warnings; print "abc" => "xyz\n"; #prints abcxyz print abc => "xyz\n"; #prints abcxyz print "abc", "xyz\n"; #prints abcxyz (most natural formulation) #print abc, "xyz\n"; #prints No comma allowed after filehandle at C +:\Projects_Perl\testing\junk1.pl line 7.
    I personally would highly recommend against using => where a comma "," is meant (i.e. your bless statement). And I would also highly recommend its use when defining elements of a hash or calling a function that expects a hash (occurs often in GUI code, -width => 5.0)

    update: When I see =>, I think hash, even if I do the double quoting on the left side explictly, e.g. "key with embedded space" => 32

      Very good points I completely forgot to mention. ++

Re: Difference between comma and => when calling subroutine
by stevieb (Canon) on Mar 14, 2017 at 22:30 UTC

    Welcome, zizu1985, to the Monastery!

    Please wrap your code within <code>code here</code> tags in your question. It makes it far more legible. As you can probably see, your code looks kind of indecipherable as is. See How do I post a question effectively? for details on formatting.

    When formatted, it'd look (something) like this:

    sub new { my ($class,$args_for) = @_; my $self = bless {} => $class; $self->_initialize($args_for); return $self; } sub new { my ($class,$args_for) = @_; my $self = bless {} , $class; $self->_initialize($args_for); return $self; }

    To answer your question. First, note you're speaking of a comma (,) not a colon (:).

    A => in Perl is often called a "fat" comma. It acts exactly the same way as a comma, with an additional feature. It will quote the left-hand-side of what's against it for you automatically. In your case above, it just acts like a normal comma, but here's an example of the additional feature in action:

    # this will fail, because 'a' is unquoted my %hash = (a, 1); # same thing, but with fat-comma, which quotes 'a'. # this won't break things my %hash = (a => 1);

    See the comma operator in perlop.

    It would also be nice if you could please edit your question's title and replace "colon" with "comma". The title (which may make searching difficult), along with no code tags will have your question Considered (What is consideration?) for editing by the janitors.