Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 105121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:23:21+00:00 2026-05-11T01:23:21+00:00

I’ve wrapped Perl’s Net::SSH::Expect with a small module to reduce the boilerplate code needed

  • 0

I’ve wrapped Perl’s Net::SSH::Expect with a small module to reduce the boilerplate code needed to write a new configuration script for use with our HP iLO cards. While on one hand I want this wrapper to be as lean as possible, so non-programmer colleagues can use it, I also want it to be as well-written as possible.

It’s used like so:

my $ilo = iLO->new(host => $host, password => $password); $ilo->login;  $ilo->command('cd /system1'); $ilo->command('set oemhp_server_name=$system_name', 'status=0'); 

and this is iLO::command():

sub command {     my ($self, $cmd, $response) = @_;      $response = 'hpiLO-> ' unless defined($response);      # $self->{ssh} is a Net::SSH::Expect object     croak 'Not logged in!\n' unless ($self->{ssh});      $self->{ssh}->send($cmd);     if ($self->{ssh}->waitfor($response, $self->{CMD_TIMEOUT}, '-re')) {         return {             before => $self->{ssh}->before(),             match => $self->{ssh}->match(),             after => $self->{ssh}->after(),         };     } else {         carp 'ERROR: '$cmd' response did not match /$response/:\n\n',             $self->{ssh}->before()),             '\n';         return undef;     } } 

I have two related queries. First, how should I deal with responses that don’t match the expected response? I guess what I’m doing now is satisfactory — by returning undef I signal something broke and my croak() will output an error (though hardly gracefully). But it feels like a code smell. If Perl had exceptions I’d raise one and let the calling code decide whether or not to ignore it/quit/print a warning, but it doesn’t (well, in 5.8). Perhaps I should return some other object (iLO::response, or something) that carries an error message and the contents of $ilo->before() (which is just Net::SSH::Expect’s before())? But if I do that — and have to wrap every $ilo->command in a test to catch it — my scripts are going to be full of boilerplate again.

Secondly, what should I return for success? Again, my hash containing more-or-less the response from Net::SSH::Expect does the job but it doesn’t feel ‘right’ somehow. Although this example’s in Perl my code in other languages emits the same familiar smell: I’m never sure how or what to return from a method. What can you tell me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:23:22+00:00Added an answer on May 11, 2026 at 1:23 am

    If you’re familiar with exceptions from languages like Java, then think of die as throw and eval as try and catch. Instead of returning undef, you could do something like this:

    if ($self->{ssh}->waitfor($response, $self->{CMD_TIMEOUT}, '-re')) {     return {         before => $self->{ssh}->before(),         match => $self->{ssh}->match(),         after => $self->{ssh}->after(),     }; }  die 'ERROR: '$cmd' response did not match /$response/:\n\n'  . $self->{ssh}->before(); 

    Then, in your calling code:

    eval {      $ilo->command('set oemhp_server_name=$system_name', 'status=0'); };  if ( my $error = $@ ) {      # handle $error here } 

    Just like exceptions in other languages, this allows you to bail out of a submethod at any point without having to worry about propagating return values up the call stack. They’ll be caught by the first eval block that finds them. Additionally, you can die again to rethrow an exception you can’t deal with back up the stack.

    Even better, you can use die to throw an object, which your exception handler can interrogate for useful information and error messages. I like to use Exception::Class for this purpose. The Error module provides some syntactic sugar for doing Java-like try/catch blocks, as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The way you solve the sniffing problem is that you… May 11, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer Have you tried the java.text.DecimalFormat class? System.out.println(new DecimalFormat().format(1.0)); outputs: 1… May 11, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer First check to see if it can be cast. if… May 11, 2026 at 8:23 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.