Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling pm and output 3

Status
Not open for further replies.

JohnLucania

Programmer
Oct 10, 2005
96
US
I put this in a modue.

sub trans {
my %translate = (
'uvx' => 'asn',
'uxx' => 'cya',
'yyu' => 'tis',
'xvu' => 'lis'
);
for( @SeqChar ){
tr/abce/uvxy/;
s/(...)/$translate{$1}/g;
print $_,"\n";
}
}

trans();
1;

How do you get the acutal str output on the screen?

When this module (Tchar.pm) is called ** use Tchar ('add'); **, it is not displaying the output from it.

I call it in .pl

print "The str translated are: \n";
trans();

jl
 
You may want to try a quick peruse of Mike's lovely faq219-1972 on do/require/use.

Also, check your local perldoc for perlmod, perlobj and expecially perltoot for some example on how you should structure a formal module, if that's really what you're trying to make.

- Andrew
Text::Highlight - A language-neutral syntax highlighting module in Perl
also on SourceForge including demo
 
It is a puzzle!!! @SeqChar contains bulks of strings. So, how do you pass the strings from .pm to .pl? If I put this sub trans { } in .pl, it displays the output from sub trans { }. If it is in .pm, it does not.

jl
 
Firstly, always always always "use strict" and "use warnings".
If you still have issues you don't understand then "use diagnostics".
I would suggest that your module does not "export" your subroutine and therefore your main code does not even know it exists. I also guess that you're not using warnings and so it can't warn you that it has not found the function.


Trojan.
 
I am using:

use strict;
use warnings;
use diagnostics;

but, no error or no warning returns.


I tried in .pl

#! /usr/bin/perl
use strict;
use warnings;

use Tchar;

bla ..............

and

#! /usr/bin/perl
use strict;
use warnings;

my @SeqChar ;
print "Enter sequences and 'enter' and then Ctrl + D: ";
chomp(@SeqChar = <STDIN>);

use Tchar;

bla ...........

but, it not getting the output strings.

jl
 
ok,

I have:

#! /usr/bin/perl
use strict;
use warnings;
use diagnostics;

use TChar;

BEGIN {
our @SeqChar;
print "Enter sequences and 'enter' and then Ctrl + D: ";
chomp(@SeqChar= <STDIN>);
}

and am getting these errors:

Global symbol "@SeqChar" requires explicit package name at TChar.pm line 144.
Compilation failed in require at ./TransChar.pl line 6.
BEGIN failed--compilation aborted at ./TransChar.pl line 6 (#1)
(F) You've said "use strict vars", which indicates that all variables
must either be lexically scoped (using "my"), declared beforehand using
"our", or explicitly qualified to say which package the global variable
is in (using "::").

Uncaught exception from user code:
Global symbol "@SeqChar" requires explicit package name at TChar.pm line 144.
Compilation failed in require at ./TransChar.pl line 6.
BEGIN failed--compilation aborted at ./TransChar.pl line 6.

in TChar.pm,

I have:

#! /usr/bin/perl

use strict;
use warnings;

use Exporter 'import';
our @EXPORT_OK = ( "add" );

sub ..... bla

How do you fix the errors?

jl
 
your main script:

Code:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

use TChar;#<-- should be in same folder as main script

print "Enter sequences and 'enter' and then Ctrl + D: ";
chomp(my @SeqChar= <STDIN>);
trans(@SeqChar);

Tchar.pm
Code:
my %translate = (
   'uvx' => 'asn',
   'uxx' => 'cya',
   'yyu' => 'tis',
   'xvu' => 'lis'
);

sub trans {
   my @SeqChar = @_;
   for( @SeqChar ){
      tr/abce/uvxy/;
      s/(...)/$translate{$1}/g;
      print "$_\n";
   }
}
1;
 
Or you could try this:

t.pl
Code:
#!/usr/bin/perl -w
use strict;

use Tchar qw(trans);

my @SeqChar ;
print "Enter sequences and 'enter' and then Ctrl + D: ";
chomp(@SeqChar = <STDIN>);

trans(@SeqChar);
Tchar.pm
Code:
#!/usr/bin/perl -w

package Tchar;
use strict;
use Exporter;
our @EXPORT_OK;
our @ISA;

@ISA       = qw(Exporter);
@EXPORT_OK = qw(trans);

sub trans {
   my %translate = (
     'uvx' => 'asn',
     'uxx' => 'cya',
     'yyu' => 'tis',
     'xvu' => 'lis'
   );
    for( @_ ){
           tr/abce/uvxy/;
           s/(...)/$translate{$1}/g;
           print $_,"\n";
    }
}

1;
As has been pointed out to you already though, your hash "translate" and the associated regex almost certainly do not do what you want.
Are you trying to translate batches of characters or complete words? The regex, as is, will try translate words.


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top