How can I set a default value for a Perl variable?

Viewed 15515

I am completely new to Perl. I needed to use an external module HTTP::BrowserDetect. I was testing some code and tried to get the name of the OS from os_string method. So, I simply initialized the object and created a variable to store the value returned.

my $ua = HTTP::BrowserDetect->new($user_agent);
my $os_name = $ua->os_string();

print "$user_agent $os_name\n";

there are some user agents that are not browser user agents so they won't get any value from os_string. I am getting an error Use of uninitialized value $os_name in concatenation (.) or string

How do I handle such cases when the $os_name is not initialized because the method os_string returns undef (this is what I think happens from reading the module source code). I guess there should be a way to give a default string, e.g. No OS in these cases.

3 Answers
Related