Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: NEWBIE Brain Teaser #2, by nysus

by Ri-Del (Friar)
on Apr 16, 2001 at 00:58 UTC ( #72694=note: print w/ replies, xml ) Need Help??


in reply to NEWBIE Brain Teaser #2, by nysus

Instructions for Newbies:
Part A
What do you think the output of the code below is?

I figured that the result would be "In the beginning there
was nothing, in the end there will be nothing."

Part B
Give an accurate explanation for your answer.

The reason I assumed this, was because I thought that @_ = qw(alpha, omega); assigned the values "alpha" and "omega" to an array called "_". While, $_ = qw(nothing, nothing); ends up assigning the value "nothing" to a scalar variable called "_". Which I think is related somehow to the manner in which we call the array "_". Hmm, perhaps we are actually altering something with the array by assigning a value to the $_ variable? I've just started learning perl and I seem to remember reading something about that.

Part C What happens when you replace "subroutine ();" in line 7 with "&subroutine;"? Why?

Well, I had to go look it up, because quite frankly I had no clue whatsoever. However, it appears that by placing an '&' in front of the "subroutine ();" call is really no different than not putting it in front because of the fact that the '&' prefix is considered to be optional. According to the second edition of the camel book, on page 114, it seems that when one names a subroutine, or if one is attempting to get a reference to a subroutine, or if one wishes to "do an indirect subroutine call with a subroutine name or reference using the &$subref() or &{$subref}() constructs..." then the '&' is not optional.

Another Question: When I attempted to add "-w" and then correct the "Use of uninitialized value in concatenation (.) at brain2.pl line 10." I think I realized that in fact we are using something that really isn't a variable or an array. So um.. I thought maybe @_ and $_ are actualling something else, so that is when I decided to go to the index of my trusty Camel II book. I think I realize where I went wrong and it is related to the error I recieved when I added "-w". Apparently $_ and @_ are actually global variables. But my question is, what exactly are they? Are they the arguments that are passed in when I run the script?


Comment on Re: NEWBIE Brain Teaser #2, by nysus
Select or Download Code
Re: Re: NEWBIE Brain Teaser #2, by nysus
by nysus (Deacon) on Apr 16, 2001 at 01:28 UTC
    Well, let's see...for Part A you guessed that the result would be "In the beginning there was nothing, in the end there will be nothing." But is this the result you got when you ran the script?

    If you run the script through Perl, you'll actually get "In the beginning there was , in the end there will be ." But why?

      Yes that is exactly what I got. "In the beginning there was , in the end there will be ." As to the reason, the only thing I thought at first was that as far as I have learned "$_" and "@_" are actually similar to 'argv' in C. Since, we had not passed any parameters to the script nothing was printed out. So I tried that passing two parameters to the script when I ran it. However, as I am sure you know, nothing changed. I have considered that maybe the word 'nothing' is similar to declaring something null, however, I can not find anything to back this up. Does not @_ mean the same thing as @ARGV, that these are the arguments passed to a script? So that if I pass two arguments they will reside in $_[0] and $_[1]? So in effect about the only thing I can conclude at the moment (and I don't particularly like my answer) is that when we state @_ = qw(alpha omega); we are overwriting the first two parameters (if any were passed) and then in the second line, $_ = qw(nothing nothing); setting them both to null. So in effect we set two values, set them to null and then print them out. Am I close?
      You are over complicating some things. First, answer this question and keep your answer as simple as possible:

      How do you pass an argument to a subroutine?

      Yes, there are a few different ways, but there is one way that is most commonly used nowadays and that's the answer I'm looking for.

      Because @_ is localised for the subroutine. Since you called subroutine with no parms, the @_ it's operating from has undef.

      As for &subroutine, @_ is not localised and the existing one is passed in. $_ need not apply outside the sub in any event -- the subroutine deals with @_.

      --
      Me spell chucker work grate. Need grandma chicken.

      Give the man above a see-gar...well maybe half of one.

      You explanation is correct for Part B. But Part C yields "In the beginning there was nothing, in the end there will nothing." (At least it does in my version of Perl 5.005.) I'm not sure I quite follow your explanation for this result.

Re: Re: NEWBIE Brain Teaser #2, by nysus
by nysus (Deacon) on Apr 16, 2001 at 01:38 UTC
    As for Part B, which asks "why?"...reading perlsub will give you a better understanding of how to pass variables to a subroutine. If you scope :-) it out very closely you may be able to figure out the answer to Part B. However, if you have a copy of "Learning Perl" by merlyn handy, you may get a friendlier explanation for what this bit of code does.
Re: Re: NEWBIE Brain Teaser #2, by nysus
by nysus (Deacon) on Apr 16, 2001 at 01:55 UTC
    I'm going to leave Part C alone for now. It's a bit of a bear.

    I will tell you that "&" is optional only when you call a subroutine with parentheses. For example, subroutine();. In this case, no "&" is needed. But try changing line 7 to plain old "subroutine;" and you will see that the script fails with an error.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-11-24 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (135 votes), past polls