Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: subroutine function

by blue_cowdawg (Prior)
on Oct 11, 2012 at 19:07 UTC ( #998517=note: print w/ replies, xml ) Need Help??


in reply to subroutine function

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


Comment on Re: subroutine function
Select or Download Code
Re^2: subroutine function
by truthseeker66 (Novice) on Oct 11, 2012 at 19:52 UTC
    Thank you
Re^2: subroutine function
by tobyink (Abbot) on Oct 11, 2012 at 19:55 UTC

    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.

Re^2: subroutine function
by bimleshsharma (Beadle) on Oct 12, 2012 at 09:46 UTC

    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: note [id://998517]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-08-01 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls