Ockie has asked for the wisdom of the Perl Monks concerning the following question:

In my little piece of code I parse a XML, but since I need to do the same thing in 3 places, I would like to make a subroutine for each time... but I need to pass the current node I'm working in as an argument, being beyond my current (worthless) skills to access the current nodes child nodes.... here's and example of my code:
foreach $day ($doc->findnodes('/my/current/path')){ @atts = $day -> getAttributes(); foreach $at (@atts) { $na = $at -> getName(); $va = $at -> getValue(); if ($va eq "today"){ #------ my repeated code here ----- foreach $thing ($day->findnodes('child_nodes_im_looking_for')) +{ #----- do a lot of stuff } } if ($va eq "tomorrow"){ #same repeated code } if ($va eq "some_other_day"){ #same repeated code.... again } #for other days... do nothing }
how should I pass my current node to the subroutine so I can access its child nodes directly from the routine?

Replies are listed 'Best First'.
Re: Working with child nodes in subroutine (XML::LibXML)
by Eily (Monsignor) on Nov 04, 2014 at 13:46 UTC

    What do you know about subroutines? Because it looks like you're asking how to pass any argument to a subroutine, in which case, you may want to read perlsub. Anyway, you declare your subroutine like this:

    sub myRepeatedCode { my ($day, $argument2, $argument3) = @_; # Do something with $day }
    and you just call like: myRepeatedCode($day, "argument2", 3);

    But actually in your case, instead of "if today do something, if tomorrow same thing, if some other day still the same" you could actually write something like "if today, tomorrow or some other day, do something", which avoids the code repetition in the first place. To check that one of several conditions is met, you can put an OR operator between them. Or is often written || in most languages, but in perl in can also be written or.

    if ($va eq 'today' || $va eq 'tomorrow' || $va eq 'friday') { DoSomething(); }

    The difference between || and or is their precedence, see perlop on that point.

      d'oh... thanks for the tip... was so focused on needing to reuse the code I didn't stop to think about using the if today || tomorrow option... sigh. I should know better than that.
Re: Working with child nodes in subroutine (XML::LibXML)
by perlron (Pilgrim) on Nov 04, 2014 at 15:04 UTC
    Hey Ockie,
    Never mind the skill level, there are worse days for all of us :D
    update I apologize i did not see the caption as having the same module i was recommending. This text below then is redundant

    I just wanted to suggest you to u check the XML module mentioned in first reply to total noob question about xml parsing. This might be useful in the 'doing a lot of stuff' section of your code and overall make your script more readable for others
    The Thumb rule everyone keeps advising me is to reuse stuff already written efficiently by others and not make low level calls when it can be avoided.
    Parsing an xml document should be one of those cases.
    Cheers.

    The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
      Thanks anyway for caring! :)
      Problem fixed & working now...