Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

subroutine function

by truthseeker66 (Novice)
on Oct 11, 2012 at 18:58 UTC ( #998515=perlquestion: print w/ replies, xml ) Need Help??
truthseeker66 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

1. '$first' and '$second' are 2 different variables. Why is
'$_[0] . $_1' used instead of '$_[0] . $_[0]' to combine
two strings? Doesn't the first '$_' for '$first' and the
second '$_' for '$second'?

2. In this code " . $_[0] . $_1 . "\n"; I understand why there
are two dots. But I don't understand why the third dot is
there.

Thank you,
#!/usr/bin/perl #prompt user for two strings. Remove newline and #store into variables print "\nEnter the first string: "; chomp($first = <STDIN>); print "\nEnter the second string: "; chomp($second = <STDIN>); sub combine{ print "\nThe combined strings are: " . $_[0] . $_[1] . "\n"; } &combine($first,$second);

C:\JPARK\JPERL>sub-ex.pl
Enter the first string: I go
Enter the second string: to school.
The combined strings are: I go to school.

Comment on subroutine function
Download Code
Re: subroutine function
by roboticus (Canon) on Oct 11, 2012 at 19:05 UTC

    truthseeker66:

    The dot operator combines two strings. So given:

    my $t = "apple"; my $u = "banana"; my $x = "Combined: " . $t . $u . "\n";

    Perl starts to combine the strings:<\p>

    my $x = "Combined: " . $t . $u . "\n"; # before we start = "Combined: " . "apple" . "banana" . "\n"; # expanded variables = "Combined: apple" . "banana" . "\n"; # combined first two = "Combined: applebanana" . "\n"; # then the next = "Combined: applebanana\n"; # then the last

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: subroutine function
by blue_cowdawg (Monsignor) on Oct 11, 2012 at 19:07 UTC

    First off fellow monk understand just what you are doing here.

    Those dots you are referring to are the string concatenation operator. Each operator has a left hand side and a right hand side. I won't even start getting into precedence here since it reall y doesn't matter all that much.

    my $thing = "Coca" . "Cola";
    In the above example when $thing is printed it will result in "CocaCola" being printed.

    As far as the variables them selves consider this code block:

    my ($left,$right)=@_;
    that will give you the same result as
    my $left=$_[0]; my $right=$_[1];
    which is the same as
    my $left = shift @_; my $right = shift @_;
    but not necessarily the same as
    # # NEVER ASSUME!!! my $right = pop @_; my $left = pop @_; # # You may not get the result you are after!!

    Clear as mud?


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Thank you

      my $left=$_[0]; my $right=$_[1];

      which is the same as

      my $left = shift @_; my $right = shift @_;

      It is not the same at all. The first version doesn't modify @_. The second one does modify it. The difference is often very important. (For example, if you're planning on using goto.)

      use Test::More; use Data::Dumper; sub func1 { my $left=$_[0]; my $right=$_[1]; goto \&Data::Dumper::Dumper; } sub func2 { my $left = shift @_; my $right = shift @_; goto \&Data::Dumper::Dumper; } my @args = qw( a b ); # Are they the same?? is( func1(@args), func2(@args), "they're the same!", ); done_testing();
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        The difference is often very important.

        And the difference often doesn't matter at all. As in the OP's example. In which case they are essentially the same. Similar. Whatever. Yes, I know what the difference is; I often pass @_ to other methods after shifting off the object. And often I don't.

      Regarding question# 2 i believe now you do understand about dot(.); so first dot(.) is to concatenate left side variable. and right side dot(.) is to concatenate right of that variable. If string or variable is not available then it will simply ignore.

        No. "If a string or variable is not available" then it is an empty string or undef and an empty string will be concatenated (not simply ignored).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998515]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-12-26 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (164 votes), past polls