correct way to call a perl subroutine

  • Thread starter Parapura Rajkumar
  • Start date
P

Parapura Rajkumar

hey all

I am attaching a small script to illustrate my problem. The
attached script executes correctly.

But if I make Call 2 same as Call1 ie

do

WriteLine "Test2" ; instead of WriteLine( "Test2" );

I get an error

String found where operator expected at /Users/foobar/bin/stest.pl
line 26, near "WriteLine "Test2""
(Do you need to predeclare WriteLine?)
syntax error at /Users/foobar/bin/stest.pl line 26, near "WriteLine
"Test2""
Execution of /Users/foobar/bin/stest.pl aborted due to compilation
errors.

Is this by design, is there an workaround ?

Thanks in advance
Raj


-----------------------------------------------------

use strict;
use warnings;

package LogHelper;
use base 'Exporter';
our @EXPORT = ('WriteLine');

sub WriteLine
{
print @_ , "\n";
}

package main;
import LogHelper;

sub WriteLine2
{
print @_ , "\n";
}

#Call 1
WriteLine2 "Test1";

#Call 2
WriteLine( "Test2" );
 
P

Parapura Rajkumar

Quoth Parapura  Rajkumar <[email protected]>:


















This is by design, more-or-less. Calling a sub without parens requires
that the sub be defined, or at least declared, at the time the call is
compiled. (This is because of the many different things such a bareword
can mean in Perl.) When the call to WriteLine is compiled, the sub
&LogHelper::WriteLine exists, but the sub &main::WriteLine doesn't,
since the import hasn't run yet. This means that you have to write the
call with parens.

The 'workaround' (or rather, the correct way to do things) is to put
LogHelper in its own .pm file and pull it in with 'use'. Since this
calls import at compile time, &main::WriteLine will be visible at the
point where the call is compiled. If you have some particular reason for
doing the import at runtime, you just have to put up with needing the
parens on the call.

Ben

Thanks for the explanation. It does seem to work with 'use'. But
unfortunately using 'use' doesn't seem to be legal when you have all
your packages defined in the same file.

A bit disappointed with perl flexibility here :(

Raj
 
U

Uri Guttman

PR> Thanks for the explanation. It does seem to work with 'use'. But
PR> unfortunately using 'use' doesn't seem to be legal when you have all
PR> your packages defined in the same file.

then don't do that. use does 2 main things, it loads your module (as
does require) and calls the import method of the class name passed to
use. so how would use know to call the import of class names inside a
module with multiple package names?

PR> A bit disappointed with perl flexibility here :(

a bit better understanding of how use works would help you. rarely do
you want multiple package names in one file. and if you do, it should be
OO style and not procedural with exported names. so learn a better
coding style and improve your perl. it is easier than you think and it
is much harder to change perl in this area.

and if you really want this, then you can have the primary package (the
one with the same name as the file) have its own import method and it
can explicitly call the other packages import methods. it would also
have to export its own subs and such. you can have Exporter.pm do the
work for you or use other modules that support this. but as i said, that
is a poor design and will cause more pain down the road. just put the
packages into their own files or switch to an OO style.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top