Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Merge the difference between two files

by hippo (Bishop)
on Jun 16, 2017 at 07:49 UTC ( [id://1192914]=note: print w/replies, xml ) Need Help??


in reply to Merge the difference between two files

#!/bin/perl -w use strict; use warnings; use File::Copy; use Cwd; my $dir = cwd; main(); sub main { # ... }

Aside from the first 3 lines here, I am intrigued by how you have structured this code. Did you learn this from a course or a book or are you translating from some other language?

Can you explain why you have used File::Copy? Or Cwd? Why do you set $dir and then never subsequently refer to it (and BTW the same for $name, $variable1, $variable2)? What is the purpose of encapsulating code in sub main() when there's nothing relevant outside that scope?

As you say you are new to Perl it would probably be beneficial for you to understand what the code you have already written is actually doing. If you cannot explain what you have written, start from code you do understand instead.

Good luck.

Replies are listed 'Best First'.
Re^2: Merge the difference between two files
by hopper (Novice) on Jun 16, 2017 at 16:35 UTC
    Thanks for taking your time to look over my code. Like I said, I am new to Perl and would like to learn how to do multiple tasks using Perl so I self taught myself and didn't learn this language from school or book. You are correct, I should remove the lines "used File::Copy? Or Cwd:" since I don't know what they mean. I was copy the template from other blogs and forgot to remove them. I want to use the "my $dir.." because I want to verify and make the files that I am working on are in the same directory as the Perl scrip. I used sub main because I need to the script to do multiple tasks after it is merging. Thanks gain.
      You are correct, I should remove the lines "used File::Copy? Or Cwd:" since I don't know what they mean.

      Perl is one of those long-established languages which has very extensive documentation. Your installation should have the perldoc command which will help but if it is not there you can use the online version instead (see how the word perldoc looks like a link? It is one!). The customary place to start is with perlintro but you can follow the "use" link in my previous post which explains in a rather dry way what that command does. You have lots of reading ahead of you!

      I was copy the template from other blogs and forgot to remove them.

      It's tempting when learning a new language to just copy code from somewhere else and hack it about to make it do something slightly different. However, it does pay to understand the code you are copying before you start to hack it about.

      I want to use the "my $dir.." because I want to verify and make the files that I am working on are in the same directory as the Perl scrip.

      Fine, but you are not using it in this example. The principle of an SSCCE is to reduce the example code to the minimum required (the first S stands for "Short"). It's a distraction to you and us so you may as well remove it for now. $dir isn't doing what you think anyway.

      I used sub main because I need to the script to do multiple tasks after it is merging.

      OK, but be careful about scoping. And try to avoid the particular word "main" since that's also a built-in package name in Perl and as a beginner you may get confused between warnings/errors which relate to either your "main" sub or the main package.

      Keep at it - you are at the beginning of a long, winding, fascinating and ultimately fulfilling path. We all started where you are now.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192914]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found