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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Home Made Modules Volontary Abort Compilation ? 1

Status
Not open for further replies.

NEVERSLEEP

Programmer
Apr 14, 2002
667
0
0
CA
hi just a question regarding home made modules :
lets my module is :

Code:
package somemodule;
sub somesub { print $_[0]; }
1;

so the script :

Code:
#!perl -W
use somemodule;
somemodule::somesub('hello');

will print hello

ok how can i make my module abort compilation if
in somesub $_[0] is not defined ?

i DONT mean something like :

Code:
sub somesub {
 print "ERROR" unless ($_[0]);
 print $_[0];
}

more like :

Code:
sub somesub {
 # check if something was passed to 'somesub'
 # if not abort compilation of script !
 # printing some custom message like
 # somemodule::somesub need a passed parameter
 print $_[0];
}

so in this 2nd script :

Code:
#!perl
use somemodule;
somemodule::somesub; # on no i made a error !

i want to get this error message :
"somemodule::somesub need a passed parameter at name.pl line 2"

instead of :
"use of unisialized value at somemodule.pm line 2"

i hope im clear enough if not TELL me !
ANY help/tips/sugjestion will be VERY apreciated
thanks alot
[atom]

---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
What's wrong with doing
Code:
print "Error" unless $_[0]
?

This is similar to that but it does what you are asking, I think. If not, be more specific.
Code:
sub somesub {

        my $var = shift or die "somemodule::somesub need a passed parameter at name.pl line 2\n";
        print "$var\n";
}

jaa
 
humm that not it ... but almost thanks justice
(how can the die capture the name of the .pl script ?)
(i tried $0 gives the modules name ...)
(how can i be sure witch line ?)

ok ill explain again

lets say i have a simple module like :

Code:
package somemodule;
# ...
sub somesub { print $_[0] . " " . $_[1]; }
1;

the thing is i work on a project
and my collegues uses the modules i create
they keep complaning that when they make errors like :

Code:
#!perl -W
use somemodule;
somemodule::somesub('bla'); # oups no $_[1] !
exit;

they get :
"error at somemodule.pm line x ";

they (and i) want to get this message instead :
"error at nameofscript.pl line 2"
+ my custom message ...

also i need to cancel compiling
here the erronous script will print:
bla use of unisi ....
i need :
error ar nameofscript.pl ...
compilation aborted due to errors


thanks alot!
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
by canceling compiling i mean :
like in the erronous example not passing $_[1]
is not fatal* but i want it to be ! how to ?

*not fatal :
like here, a "use of un.." msg will be print and script
continues ...
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Using function prototyping will give you a compile time error if the number of arguments passed is incorrrect. For example if exactly two argument must be passed to the function then change your subroutine to
Code:
sub somesub ($$) {

        my ($var1, $var2) = @_;
        # Do stuff here
        # print "$var1 : $var2\n";
}
The ($$) means that the function expects two scalers. The error produced is more informative: it gives the file and line number of the calling function, exactly what you want.

jaa
 
THAnkS aLoT Dude ! [2thumbsup] ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
can i ask another question ?

how can i make a sub a .pm
ONLY accedable from another specific sub within the same .pm

kidda a private class see below what i mean

Code:
package somemodule;
sub private ($) { $_[0]++; return $_[0]; }
sub public ($) { return private($_[0]); }
1;

#!perl -W
use somemodule;
print somemodule::public(1); # print 2
print somemodule::private(1); # NOT LEGAL !!! (i wish)

i know there no point with somemodule.pm but im sure u can
see a utility to this

how can i do this ?
thanks alot! ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
You can accomplish this by using a subroutine reference. If the variable that holds the sub ref is declared as a my() variable, then it is not accessable from outside the file scope.
Code:
package somemodule;

my $subref;
sub somesub ($) {

        my ($var1) = @_;
        print "$var1\n";
        &$subref($var1);
}

# the following subroutine cannot be called as &$somemodule::subref('hello')
# from a script that use()es this module.
$subref = sub {
        my $var = shift;
        print "Private sub: $var\n";
};

1;
The downside is that you cannot do prototyping with anonymous subref's.

jaa
 
very nice i mean ..VERY VERY NICE !

i dont understand the downside
what do u mean by "prototyping with anonymous" ?
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
can't you say, in a module,

my sub somesub($$){
# do something nice
}

???

I've not done this, but I'm given to understand that it will limit the scope of the sub
Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
I think they're planning to add that feature in perl6, but I just did a quick perl -e and it didn't work.
 
Yup, quite right. Compilation fails and it says:

"my sub" not yet implemented....

ho hum...
Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top