Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: my $1

by mltucker (Initiate)
on Apr 23, 2009 at 21:20 UTC ( #759671=note: print w/replies, xml ) Need Help??


in reply to Re: my $1
in thread my $1

Hmm, unless I have a different bug than I thought I did, this isn't working for me.

-Mark

Replies are listed 'Best First'.
Re^3: my $1
by ikegami (Pope) on Apr 23, 2009 at 21:50 UTC

    You're right. I still had this on the mind. It was posted only two hours before.

    Back to your problem. There isn't one if you don't use globals. Fix:

    if (my ($pre, $post) = m/(.+?)\+(.+)/) { return diff($pre)."+".diff($post); }

      Nice solution. Thanks! Time to do some perldoc sifting to sort this all out :-).

      -Mark

        Thanks. My earlier code explained:

        • my ($pre, $post) is short for (my $pre, my $post).

        • A list-like expression on the LHS of = (such as my ($pre, $post)) causes the list assignment (aassign) operator to be used instead of a scalar assignment (sassign) operator.

        • The list assignment operator evaluates its RHS operand in list context.

        • In list context, m// (no "g") returns

          • () if the match failed,
          • the captures if the match succeeded and the pattern contains captures, or
          • 1 (one) if the match succeeded and the pattern contains no captures.
        • The list assignment operator returns the number of elements returned by its RHS operand.

        • The if condition will be false when the match fails because the list assignment operator returned 0 (zero) because the match operator returned zero elements.

        • The if condition will be true when the match succeeds because the list assignment operator returned 2 (two) because the match operator returned two captures.

        • And finally, each level of recursion of the function get its own pad, which means my ($pre, $post) creates new variables for each level of recursion, which means the they keep their value even through a recursive call.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-12-17 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (72 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!