Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Legacy code question

by Zenzizenzizenzic (Monk)
on May 13, 2019 at 14:05 UTC ( #1233696=perlquestion: print w/replies, xml ) Need Help??

Zenzizenzizenzic has asked for the wisdom of the Perl Monks concerning the following question:

Looking into some old code, I'm seeing the database query string being defined @sqltt =<<EOF; Is there any advantage to using an array over a string here? Or is this "It's old, don't copy!!" code?

Replies are listed 'Best First'.
Re: Legacy code question
by haukex (Bishop) on May 13, 2019 at 14:11 UTC
    Is there any advantage to using an array over a string here?

    I guess it depends on how the variable is used afterwards - if there are additional values being pushed onto the array, it might make sense. It's just assigning a single value to the array; personally I would have written my @sqltt = (<<EOF); to make it explicit that yes, I meant to assign a single value to this array. But if the code then goes on to use $sqltt[0] everywhere, and especially if it doesn't add elements to the array, then it would definitely have been better to just use a plain scalar in the first place.

    use warnings; use strict; use Data::Dump; my @sqltt = <<EOF; Hello, World! EOF dd @sqltt; __END__ "Hello,\nWorld!\n"

    Minor update to wording.

      Nothing is added to it, only use is as \@sqltt. I'm thinking this is a "do not do" thing. Thank you!
        Nothing is added to it, only use is as \@sqltt. I'm thinking this is a "do not do" thing.

        It still depends on the surrounding code - if it's being passed to a function that expects an arrayref, then some form of such is required, either my @sqltt = (<<EOF); ... func(\@sqltt), my $sqltt = [<<EOF]; ... func($sqltt), or my $sqltt = <<EOF; ... func([$sqltt]) would be needed (which one is a matter of taste), and "do not do" wouldn't necessarily apply. Context matters :-)

Re: Legacy code question
by davido (Cardinal) on May 13, 2019 at 15:20 UTC

    The code is using an array to hold a single element at this particular line. Later in the code it is possible that an array is desirable for some other reason; something gets added, or a subroutine called with a funky prototype, or it is otherwise convenient.

    Often when examining legacy code you'll find artifacts of the past. Perhaps an array is not all that useful at this point in the code as it exists today, but maybe seven years ago an array was exactly what was needed. Things that needed to change got changed, and things that continued to work didn't get changed.

    Or it's just dumb. That's usually the last explanation to assume, though. More often than not either it is this way for a reason now, or is this way because of the code's evolution over time. But dumb is a perfectly reasonable explanation too, and not necessarily an indictment on the developer. I find myself having to forgive myself sometimes, too. The best explanation I have is that I must have been focusing on some other aspect of the problem and didn't notice I had left an inelegant construct laying around as I iterated through the process of crafting a solution.


      While I agree philosophically with you and haukex, I have seen quite a fair bit of code in the wild “written” by users like bigup401. Code that never made sense, was written without use strict, often worked by side-effect or accident or only on a narrow subset of possible input, and is full of no-ops left-over from writing code by cut-and-paste testing.

        "...writing code by cut-and-paste testing."

        To be honest: This was the way i coded my Java stuff the last three years i was in office.

        A funny side-effect of this practice was my my best node ever on PM.

        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

      Very true. I've often seen posts online along the lines of "how on earth did this code ever make sense?", when it seems that a reasonable explanation would be that something simply got overlooked during refactoring.

Re: Legacy code question
by thanos1983 (Parson) on May 13, 2019 at 14:36 UTC

    Hello Zenzizenzizenzic,

    Just to add something minor here that maybe it will help you to understand a bit more EOF. The actual string EOF it is an indication string that helps you to understand that is the end of your file.

    You can define anything as a name e.g. (example of code taken from fellow Monk code haukex):

    #!/usr/bin/perl use strict; use warnings; use Data::Dump; my @sqltt = <<THANOS; Hello, World! THANOS dd @sqltt; __END__ $ perl "Hello,\nWorld!\n"

    Hope this minor part helps you understand more about EOF. In generally I prefer reading files line by line, but this is me.

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      The actual string EOF it is an indication string that helps you to understand that is the end of your file. ... I prefer reading files line by line ... [emphasis added]

      I think it's important to point out that the EOF identifier as used in the OPed code example has nothing to do with the end-of-file condition or reading an entire file into an array. Rather, it is an arbritrary identifier defined to mark the end of a here document. (I think you understand this perfectly well since you use the THANOS identifier in exactly this way in the code example you give, but I want to clarify this point for any novice monk who might happen upon this thread. :)

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

Re: Legacy code question
by LanX (Cardinal) on May 13, 2019 at 14:33 UTC
    It means $sqltt[0] = << "EOF" nothing else.

    Please fix your code tags.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1233696]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2021-05-18 00:51 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (171 votes). Check out past polls.