Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Questions about curious idiom seen in package

by SimonSaysCake (Sexton)
on Feb 17, 2019 at 23:41 UTC ( #1230054=perlquestion: print w/replies, xml ) Need Help??
SimonSaysCake has asked for the wisdom of the Perl Monks concerning the following question:

We use a package where I work, "WebService::NetSuite", and I happened to be looking at the code recently and noticed a very odd idiom being used:

undef my $hash_ref;

It's seems pretty clear that is't not a typo (unless it's the result of a bad cross-file seach-and-replace) as it is used in 16 different places across two files within that package. Noteworthy is that it is only used when initializing but not also assigning new variables (in those cases the author just issues EG:

my $super_epoch = time() * 1000;

which is what you'd expect).

I scanned all other packages in my Strawberry install and found no other packages where this is used.

My questions are, does this leading "undef" really do anything? If so, what exactly?

Thanks to anyone that can shed light/offer insight.

-s1m0n-

Replies are listed 'Best First'.
Re: Questions about curious idiom seen in package
by tobyink (Abbot) on Feb 18, 2019 at 00:27 UTC

    undef($variable) sets a variable to undef.

    undef(my $variable) declares a variable and sets it to undef in the same statement. However, scalar variables default to undef anyway so there's no point in it.

      More precisely, $var = undef; sets a var to undef. undef $var; also frees any buffers associated with the variable. This will usually result in a performance penalty.

Re: Questions about curious idiom seen in package
by AnomalousMonk (Chancellor) on Feb 18, 2019 at 00:35 UTC
    ... does this leading "undef" really do anything?

    It does nothing. It is a compulsive programming tick akin to
        my $scalar = undef;
        my @array = ();
        my %hash = ();
    In the latter two cases, I can barely see some occasional justification for the practice because it may relieve the onerous burden of having to write a comment like
        my @ra;  # this array will remain empty until something is put into it
    Otherwise...


    Give a man a fish:  <%-{-{-{-<

      “It does nothing”.

      Probably the best behavior for “something” (or how ever you may call it) in Perl.

      May be this is the best thread so far in this year. It made my day. Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1230054]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2019-03-26 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (125 votes). Check out past polls.

    Notices?