Hi all

Have some general questions about subroutines in terms of good practices to follow to make the code as readable but also efficient as possible -
my $ticket = get_ticket($rest_client, $sdn_controller); if (! defined $ticket) { print "Could not get ticket from $sdn_controller\n"; exit; } more code ..... sub get_ticket { my ($rest_client, $sdn_controller) = @_; my $ticket; ........; return $ticket; }

so the subroutine connects to an SDN controller and sends a REST call, gets JSON back and extracts an authorisation ticket for subsequent requests. I have another subroutine for sending all the other requests because the initial ticket request is handled differently. But both subroutines take the same arguments, are doing error checking on the HTTP response code etc. so there is some code replication which leads to first question

1) should a subroutine do one thing and one thing well rather than trying to combine both subroutines into one because of the common code between them. In other words I could use one subroutine to use the common code but then would have to make it more complex and test whether I was asking for a ticket or a just requesting data from the controller.

2) the return value from the ticket and data subroutines are values, scalar for the ticket, and a reference to a data structure for the data subroutine, the exact type of structure depending on what JSON sent back. So if the ticket request or data request fails then the return value is undef. Each time i call the subroutine and get a return value i then have to do an if loop to check whether the value is defined or not. Is there a more efficient way of doing this because I am basically just repeating if statements after each call eg. if i didn't get a ticket I could just call exit in the subroutine but it seems more logical to me and easier to follow if I handle it outside the subroutine.

3) finally as you can see from above i use the same variable names in both main and the subroutine but use my to keep them separate. I can see how that might confuse someone else looking at the code later and also if I forget to use my how it might confuse me :) I just keep running out of names. Is there a good standard to follow that others have found works ?

Thanks for your time.

In reply to subroutine and good practices by jmsnet

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.