Paul Russell Paul Russell - 3 months ago 7x
Perl Question

How to check if the first variable passed into a method is a string. Perl

I have no idea how to check for this. My method(if condition in method) should only work (execute) if the first argument passed in is a string. I know how to check other types, but I can't seem to find anything for checking for a string.

For a hash I would do something like;

if(ref eq 'HASH') {...}

If someone could provide a simple example I'm sure I would be able to apply it to what I'm doing. I will put up the code for the method and an explanation for the whole operational details of the method if needed.

Added Information

This is a method for handling different types of errors in the software, here are the 3 possible input formats:

$class->new("error string message")
$class->new("error string message", code => "UNABLE_TO_PING_SWITCH_ERROR")
$class->new("error string message", code => "UNABLE_TO_PING_SWITCH_ERROR", switch_ip => $ip3, timeout => $timeout)

  • There will always be an error message string first.

  • With the 1st case there is also a hashref to an error hash structure that is located in a library,
    this method new will go into a template processing if the word "code" exists as an arg. where the longer detailed error message is constructed. (I already have the logic for this).

  • But I have to add logic so that the error message string is added to the hash, so the output is one hash, and not strings.

  • The second case is very similar to the first, where there are parameters eg. switch_ip , which are inserted into the string using a similar template processing logic, (already have this too).

  • So I think the first and second cases can be handled in the same way, but I'm not sure, so separated them in this question.

  • The last case is just can error message string by itself, which at the minute I just insert it into a one key message hash { message => "error string}.

So after all that how should I be checking or dividing up these error cases, At the minute my idea for the ones with code , is to dump the arguments into a hash and just use something like:

if(exists($param{code}) { doTemplateProcess()...}

I need to ensure that there is a string passed in first though. Which was my original question. Does any of my context information help? I hope I didn't go off the topic of my question, if so I'll open this a new question. Thanks.

Error hash - located in

use constant ERROR_CODE => {
category => 'Connection Error:',
template => 'Could not ping switch %s in %s minutes',
tt => {template => 'disabled'},
fatal => 1,
wiki_page =>,

From comments:

These will be called in the software's code like so

"Phase x this occured because y was happening:",
switch_ip => $ip3,
timeout => 30,


You may easily check only whether the error string is a simple scalar value or a reference. You would do that with ref, but you must consider what you want to do if the first parameter isn't a string

You should write your constructor in the ASC::Builder::Error package along these lines

sub new {
    my $class = shift;
    my ($error, %options) = @_;

    die if ref $error;

    bless { string => $error }, $class;

This example simply dies, and so kills the program, if it is called with anything other than a simple string or number as the first parameter

You may call it as




and all will be well. If you try

ASC::Builder::Error->new('message', 'code')

then you will see a warning

Odd number of elements in hash assignment

And you may make that warning fatal

If there is anything more then you should explain