Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

How can I define the subroutine to get the result of the appended string i.e. ATGCTTTT?

by supriyoch_2008 (Monk)
on Sep 25, 2012 at 16:17 UTC ( #995585=perlquestion: print w/replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

I have made a perl module with a single subroutine DNAappend in order to learn the steps of making a module. Then I have written a small script i.e. When I have tried to run the perl script, the command window does not show the expected result. I can't make out where I have made the mistake in the script. I know it could be a very silly question to the perl monks but I am at my wit's end to find the solution. I welcome any constructive suggestions from the perlmonks about the script and perl module.

Here goes the script

#!/usr/bin/perl use warnings; use strict; use DNAappend; my $a='ATGC'; my $b='TTTT'; print"\n\n"; print DNAappend($a,$b); print"\n\n"; exit;

I have written the as follows:

package DNAappend; use strict; use warnings; sub DNAappend { my ($head,$tail)=@_; return ($head.$tail) } 1;

The command window has shown the result:

C:\Users\x\Desktop> Undefined subroutine &main::Dnaappend called at C:\Users\x\Desktop\joi line 8. C:\Users\x\Desktop>

The correct result should look like:

  • Comment on How can I define the subroutine to get the result of the appended string i.e. ATGCTTTT?
  • Select or Download Code

Replies are listed 'Best First'.
Re: How can I define the subroutine to get the result of the appended string i.e. ATGCTTTT?
by toolic (Bishop) on Sep 25, 2012 at 16:22 UTC
    One way to do it is to call your function using the package name as a prefix DNAappend::DNAappend:
    use warnings; use strict; use DNAappend; my $a='ATGC'; my $b='TTTT'; print"\n\n"; print DNAappend::DNAappend($a,$b); print"\n\n"; exit;

    See also:

      Hi toolic,

      Thanks for your suggestions. I shall try it.

      With regards,

Re: How can I define the subroutine to get the result of the appended string i.e. ATGCTTTT?
by Marshall (Abbot) on Sep 25, 2012 at 16:31 UTC
    You need to export the DNAappend symbol. I would use DNAlibrary or some such name, presuming that you will add other DNA routines to this thing.
    #!/usr/bin/perl -w use strict; use DNAlibrary; my $a='ATGC'; my $b='TTTT'; print"\n\n"; print DNAappend($a,$b), "\n\n"; #ATGCTTTT
    #!/usr/bin/perl -w use strict; package DNAlibrary; #FILE name should be use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=1.0; our @ISA = qw(Exporter); our @EXPORT = qw(DNAappend);#these are the default exports our @EXPORT_OK = qw(); #these are export on request sub DNAappend { my ($head,$tail)=@_; return ($head.$tail) } 1;

      Hi Marshall,

      Thanks for your suggestions and prompt reply. I shall try both the scripts.


        I ran the code before posting - you should be fine. There is some more or less "standard boiler plate" for these things. You should read the tut's if you plan to make a lot of these things. This was just an example to get you started. You can go a long way with just a little hacking based upon this example, for first testing, you don't need to worry about ISA or Exporter actually means... just follow the example.

        There really isn't any such thing as a "private subroutine" in Perl - if you know the fully qualified name, you can call the function even if the sub's name is not exported. DNAlibrary::DNAappend(...); Exporting the symbol allows you to call it without having the package name prefix. If you use EXPORT_OK(...), then you need to specifiy the routines to import in the "use DNAlibrary" statement in the caller's program.

        There are various testing harnesses to do automated tests during installation, etc. Sometimes I don't fiddle with that and just put a sub called test() in the module which I don't export. Then I have a $DEBUG variable that I set to 1 when I am working the the .pm file => then some statement like: test() if $DEBUG; That way the .pm file will produce a result when you run it by itself. Sophistication varies, but you should have some sort of testing strategy so that you can validate the module's functions without some extra driver program. This will save a LOT of grief later on.

        You may not have seen an "our" variable before. A "my" variable does not go in the symbol table and is not eligible for export. An "our" variable does (it has "package scope", not "lexical scope". Since all of my modules have a $VERSION, exporting it makes no sense (name would conflict with other modules). But you can print it with print DNAlibrary::$VERSION; That wouldn't work if $VERSION was a "my" variable.

        Glad to help you get started. Let me know if there are troubles. For right now, put the .pm file in the same directory as the .pl file. After awhile, this will no longer be adequate but there are a number of ways to solve that problem - but that's a different subject!

        The goal here was go get you started and I think the Monks have done that. I would recommend buying the "Perl Cookbook" - it has lots of useful "recipes" for common problems. Of course there are lots and lots of details about this stuff.

Re: How can I define the subroutine to get the result of the appended string i.e. ATGCTTTT?
by Anonymous Monk on Sep 25, 2012 at 21:09 UTC
    Excellent question, supriyoch_2008. You concisely demonstrated what you're trying to accomplish. The post shows that your goal is to learn a new Perl technique. You did a good job of explaining what your expectation is, and what's happening instead. This is a nice step forward.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995585]
Approved by toolic
[stevieb]: Torturing one of my dev RPis to turn four 5v stepper motors at the same time (via Parallel:: ForkManager for 50k degrees (a whole lot of full turns!). Let's see if it burns out or not.
[stevieb]: My GPIO expander chips should arrive tomorrow, but I think I'll throw a shift register against them and see if all six I have will turn at once. Amps are near the breaking point, but I want to know max sustainable V and I output.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-03-22 22:16 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (286 votes). Check out past polls.