Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 888
sfactor Avatar asked Oct 18 '10 12:10

sfactor


People also ask

How do you assign default values to variables?

The OR Assignment (||=) Operator The logical OR assignment ( ||= ) operator assigns the new values only if the left operand is falsy. Below is an example of using ||= on a variable holding undefined . Next is an example of assigning a new value on a variable containing an empty string.

How do I assign a value to a variable in Perl?

Perl Variable DeclarationThe equal sign (=) is used to assign values to variables. At the left of (=) is the variable name and on the right it is the value of the variable.

What is default variable Perl?

There is a strange scalar variable called $_ in Perl, which is the default variable, or in other words the topic. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used.

How do I initialize a variable in Perl?

Creating Variables Perl variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.

Why does my Perl function use its default values?

The same can be the case for individual functions in perl that accept arguments. In case the arguments are not provided, the function will use its default values. Let's see how to set default values.

What is a variable in Perl?

Perl - Variables. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or strings in these variables.

What does $_$_ mean in Perl?

$_ the default variable of Perl. Prev. There is a strange scalar variable called $_ in Perl, which is the default variable, or in other words the topic. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used. In general, I'd say you should NOT see $_ in real code.

What are the basic data types in Perl?

We have learnt that Perl has the following three basic data types − Accordingly, we are going to use three types of variables in Perl. A scalar variable will precede by a dollar sign ($) and it can store either a number, a string, or a reference. An array variable will precede by sign @ and it will store ordered lists of scalars.


Video Answer


2 Answers

Please note: the original answer's approach ( $var = EXPRESSION || $default_value ) would produce the default value for ANY "Perl false values" returned from the expression, e.g. if the expression is an empty string, you will use the default value instead.

In your particular example it's probably the right thing to do (OS name should not be an empty string), but in general case, it can be a bug, if what you actually wanted was to only use the default value instead of undef.

If you only want to avoid undef values, but not empty string or zeros, you can do:

  • Perl 5.10 and later: use the "defined-or operator" (//):

    my $os_name = $ua->os_string() // 'No OS';
    
  • Perl 5.8 and earlier: use a conditional operator because defined-or was not yet available:

    my $os_string = $ua->os_string(); # Cache the value to optimize a bit
    my $os_name = (defined $os_string) ? $os_string : 'No OS';
    # ... or, un-optimized version (sometimes more readable but rarely)
    my $os_name = (defined $ua->os_string()) ? $ua->os_string() : 'No OS';
    

A lot more in-depth look at the topic (including details about //) are in brian d foy's post here: http://www.effectiveperlprogramming.com/2010/10/set-default-values-with-the-defined-or-operator/

like image 114
DVK Avatar answered Sep 25 '22 20:09

DVK


my $os_name = $ua->os_string() || 'No OS';

If $ua->os_string() is falsy (ie: undef, zero, or the empty string), then the second part of the || expression will be evaluated (and will be the value of the expression).

like image 38
cHao Avatar answered Sep 24 '22 20:09

cHao