Hi harangzsolt33,

sub ccc{65} print "\n" . ccc.ccc.ccc.ccc; # this evaluates to numbers print "\n" . 65.65.65.65.65; # this evaluates to letters # Ok. It's a little weird, but it is what it is.

Well, Perl can tell the difference (on the side, a fun read), and if you know what v-strings are so can you :-) Note that whitespace is significant so that Perl can tell the difference between a v-string and numbers joined with the concat operator ".": print 65 . 65 . 65 . 65; prints "65656565".

But now, what happens to my anonymous subroutines? Do they simply disappear or stay in memory somewhere?

I'm not an expert on the internals but I believe they are subject to Perl's normal memory management, i.e. when the last reference to an anonymous sub goes away it is garbage-collected (the memory it used can be reclaimed by Perl). You normally don't need to worry about this, all you need to think about is that when you want a value to go away you make sure to discard all references to it, and that there are no circular references.

sub MY{ my $K = __SUB__; } # Why is this giving me an error?

See __SUB__: "This token is only available under use v5.16 or the current_sub feature. See feature."

sub P{(6,7,8)} # returns an array

This may seem nitpicky, but it's a common pitfall: that's not an array, that's a list. Note that return, including the implicit return value, passes along the context that the sub was called in. Your sub P, when called in list context (for example, when its return value is assigned to an array), will return the three values 6,7,8, and you won't be able to tell the difference between it and a sub Q { my @a = (6,7,8); @a } called in list context.

However, you will be able to tell the difference when you write my $x = P; my $y = Q;: $x will contain 8 (the last value from the list), and $y will contain 3 (the number of items in the array)*. The reason is that sub P being called in scalar context causes its return value to be evaluated in scalar context, and the Comma Operator in scalar context "evaluates its left argument, throws that value away, then evaluates its right argument and returns that value." In sub Q, the return value @a evaluated in scalar context returns the number of elements of the array, the normal behavior of an array in scalar context.

* By the way, a debugging tip: don't test code like this with arrays that contain numbers; use strings instead. You wouldn't have been able to tell the difference between $x and $y if the returned list had been (1,2,3).

Hope this helps,
-- Hauke D


In reply to Re^3: Trying to understand Perl :-) by haukex
in thread Trying to understand Perl :-) by harangzsolt33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.