I am learning Perl, and I am trying to see what happens when I write some weird code. And I have some questions...
################################## sub AA{6} sub BB{{{3}}} sub L{AA+BB} # Now, here Perl thinks that I am # calling AA(+BB) so, instead of # adding 6+3, it simply returns 6 print "\nL=".L."\n"; print "\nL=".((AA) + BB) ."\n"; # Now I got it right. # Okay, I understand this. So far everything is clear. ################################## sub UNIQUE::print{print 'Hello World!'} UNIQUE->print(333); sub UNIQUE::print{ print "\nMuhahaha! @_ \n"; } UNIQUE->print(65.65.65.65); # Okay this is weird. # First of all, how does 65 become letter 'A' without using chr(65) ? # Secondly, Perl allows me to redefine my UNIQUE::print and now # this second definition overwrites the first one. # What happened to the first one??? # Why is my 'Hello World' never showing up at all? ################################## print "What on earth is this? >>> " . sub {5}; my $D = sub { print "What the Heck!"; }; # What am I doing here? # And why is this allowed in Perl? What's the point of # creating a noname sub? Okay, so if I wanted to, how # would I run my noname sub using the handle $D ? ################################## sub Red { sub LittleRed{'red'}; # sub within a sub LittleRed; # returns 'red' } print "\nPerl is getting confused here..." . Red; print "\n\n"; print LittleRed; # Wait. This is not supposed to work! # If I declare a sub within a sub, then the inner sub should # not be accessible from the outside. Is this right??? # I mean what's the point of declaring a sub within a sub # if it's the same as declaring it outside the sub? ################################### sub X{10} print "\nYES>".X; # This prints 10. Fine. sub Y{my$i=0;$i while($i++<10);}print"\nYES>".Y; # Now, why is this not printing 10?? # When the while loop ends, $i equals 10, so if I just # say "$i" then it should use that as a return value for # the sub. No? ########################### # Another question: Is there a way to # access previous return values? # Example: sub DDD { 0; 1; 2; 3; 4; 5; } print DDD; # Is it possible to do something like this # without using the = assignment operator?: # # sub DDD { # 4+4; # 5+(previous lines' result); # return previous line's result; # } # # So, DDD would return 13.

In reply to 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.